Psalm: substr() with class-string

Created on 16 Nov 2020  路  3Comments  路  Source: vimeo/psalm

could psalm infer the class-string in this case and therefore handle it like a string literal which would then know that substr on this literal will not be false?

https://psalm.dev/r/d16b9d5084

bug

Most helpful comment

substr(A\B::class, 4) returns false in PHP 7.4.

It returns the empty string in PHP 8, and Psalm has been updated to match: 42802e11d

All 3 comments

I found these snippets:


https://psalm.dev/r/d16b9d5084

<?php
/**
 * @psalm-template T
 * @psalm-param T $value
 * @psalm-return (T is Stringable ? string : T)
 *
 * @throws InvalidArgumentException
 *
 * @return mixed
 */
function rex_escape($value)
{
     return $value;
}

class my_class_name {
    public static function getHiddenFields():string
    {
        $class = static::class;

        if (self::class === $class) {
            throw new BadMethodCallException(__FUNCTION__.' must be called on subclasses of "'.self::class.'".');
        }

        // remove the `rex_api_` prefix
        $name = substr($class, 4);

        return sprintf('<input type="hidden" name="%s" value="%s"/>', 'xyz', rex_escape($name));
    }
}
Psalm output (using commit 4e8fb9c):

ERROR: PossiblyFalseArgument - 28:78 - Argument 3 of sprintf cannot be false, possibly false value provided

Valid class-strings are probably never empty, so it should be possible.

substr(A\B::class, 4) returns false in PHP 7.4.

It returns the empty string in PHP 8, and Psalm has been updated to match: 42802e11d

Was this page helpful?
0 / 5 - 0 ratings