Psalm: Suppress errors from e.g "float|int" vs "string" assignments / comparisons?

Created on 15 Oct 2020  路  4Comments  路  Source: vimeo/psalm

Hi, our legacy project are using MySQL, and MySQL returns type string for nearly everything, our Request / Ajax classes also using type string very often because everything from _GET / _POST is a string or an array of strings. BUT our ActiveRows are using different type-hints, automatically generated from the database via PHP CS Fixer.

e.g.:

    /**
     * @BaseValue
     *
     * <p>Type from DB: decimal(10,3)</p>
     *
     * @var string
     */
    public $preis1 = '0.000';

Now I have many errors reported by psalm, I could change all phpdocs via PHP CS Fixer at once, but decimal is string and "baseline" is also not helping because every day there is new code with the same problem.

For phpstan I did it like this e.g.:
'#expects string, float|int given#'

Question

Is there a config option for this use-case or do I need to fake all phpdocs or is there a different way (without changing nearly every method in the project)?

Error:

InvalidPropertyAssignmentValue - App/modules/Menukalkulation/factory/Menurezept.php:1027:33 - $this->preis1 with declared type 'string' cannot be assigned type 'float|int' (see https://psalm.dev/145)

$this->preis1 = $this->getKosten();

PS: sorry for the long text && THANKS for your work, it's really amazing how good PHP code can be, if there is someone (something) that cares about the quality of the code. :D

Most helpful comment

@orklah Thanks for the hint, this is working for me. :+1:

  /**
     * @BaseValue
     *
     * <p>Type from DB: decimal(10,3)</p>
     *
     * @var string
     * @psalm-var numeric
     */
    public $preis1 = '0.000';

All 4 comments

Hey @voku, can you reproduce the issue on https://psalm.dev ?

Did you try documenting your variables as numeric-string? You should have something like if(is_numeric($GET[$input_name])) somewhere to make sure every input you get from your ajax or database is a numeric-string, that way, Psalm will let you use those as strings and as numbers.

In your case, the type may need to be numeric-string|numeric to allow float|int

EDIT: numeric contains numeric-string, so numeric only should be enough

Did you try documenting your variables as numeric-string? You should have something like if(is_numeric($GET[$input_name])) somewhere to make sure every input you get from your ajax or database is a numeric-string, that way, Psalm will let you use those as strings and as numbers.

In your case, the type may need to be numeric-string|numeric to allow float|int

EDIT: numeric contains numeric-string, so numeric only should be enough

Thanks I will try it. 馃槉

@orklah Thanks for the hint, this is working for me. :+1:

  /**
     * @BaseValue
     *
     * <p>Type from DB: decimal(10,3)</p>
     *
     * @var string
     * @psalm-var numeric
     */
    public $preis1 = '0.000';
Was this page helpful?
0 / 5 - 0 ratings