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?
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
Most helpful comment
substr(A\B::class, 4)returnsfalsein PHP 7.4.It returns the empty string in PHP 8, and Psalm has been updated to match: 42802e11d