Skip to content

Doctrine and float and string #71

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
danielspk opened this issue Sep 4, 2019 · 3 comments
Closed

Doctrine and float and string #71

danielspk opened this issue Sep 4, 2019 · 3 comments

Comments

@danielspk
Copy link

Hi, is it feasible for the plugin to detect Doctrine's behavior with respect to floats being handled as a string? Example:

/**
 * @Entity
 */
class Demo
{
    /** @Column(type="decimal", precision=10, scale=2) */
    private $price;

    /**
     * Set price.
     *
     * @param string $number
     *
     * @return Demo
     */
    public function setPrice($number)
    {
        $this->price = $number;

        return $this;
    }
}

$demo = new Demo();
$demo->setPrice(0.0);

Error output: Parameter #1 $number of method Demo::setPrice() expects string, float given

Note: The Doctrine orm: generate-entities console command generates the method with its parameter as string.

Thanks.

@ondrejmirtes
Copy link
Member

Hi, all types should be explicitly referenced in the code. If the Doctrine's decimal type accepts both string and float, you should have string|float in your setter parameter and as the property type.

But maybe you should stick to only strings, because floats are lossy? Or use something like https://github.com/moneyphp/money for this usecase.

See also reasoning behind https://github.com/Roave/no-floaters.

@danielspk
Copy link
Author

Thank you. At the moment I am going to modify the signature of the DocBlock to float|string since I do not require floating point precision.

@github-actions
Copy link

github-actions bot commented May 1, 2021

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 1, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants