Skip to content

"sidzIgnoreMagicNumbers" is not working for numeric-strings #23

@voku

Description

@voku

Hi, and thanks for this phpstan addon. I use it in a legay php project and step-by-step the magic numbers will be replaced with constants that I can understand. 👍


The situation: I updated the php application from 7.4 to 8.3 and now I use vesion 0.4.3 but I see many new errors from numerc-strings. For now I will use sidzIgnoreNumericStrings: true but I think ignoring should be work also for numeric-strings.

The problem: ignoreNumber() will check the type of the config value but numeric-strings are not allowed here, I see messages like this: The item 'parameters › sidzIgnoreMagicNumbers › 2' expects to be number, '0' given. if I try to add '2' or "2" in for sidzIgnoreMagicNumbers.

Code:

    /**
     * @param LNumber|DNumber $scalar
     */
    private function ignoreNumber(Expr $scalar): bool
    {
        return in_array($scalar->value, $this->ignoreMagicNumbers, true);
    }

Possible fix: ?

    /**
     * @param LNumber|DNumber $scalar
     */
    private function ignoreNumber(Expr $scalar): bool
    {
        $ignoreMagicNumbersStrings = array_map(function($obj) {
            return (string) $obj;
        }, $this->ignoreMagicNumbers);

        return in_array($scalar->value, $this->ignoreMagicNumbers, true) || in_array($scalar->value, $ignoreMagicNumbersStrings, true);
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions