-
-
Notifications
You must be signed in to change notification settings - Fork 946
Closed
phpstan/phpstan-src
#4237Labels
Milestone
Description
Bug report
Call to function is_string() with non-empty-string|false will always evaluate to true.
🪪 function.alreadyNarrowedType
Code to Reproduce
<?php declare(strict_types = 1);
class HelloWorld
{
public function dumpToLog(mixed $dumpToLog): ?string
{
if (is_string($dumpToLog)) {
if (file_exists($dumpToLog) || is_string($dumpToLog = realpath($dumpToLog))) {
throw new \RuntimeException('Refusing to overwrite existing file: ' . $dumpToLog);
}
}
return null;
}
}The exact cause of the error is because $dumpToLog gets reassigned inside the is_string() function. Removing the assignment (or assigning to a differently named variable) makes the error go away.
Code snippet that reproduces the problem
https://phpstan.org/r/7856e3b2-805f-4edb-996e-e3042642315e
Expected output
No error, the type is not sufficiently narrowed (is_string(non-empty-string|false) should return bool not true).
Did PHPStan help you today? Did it make you happy in any way?
No response
Reactions are currently unavailable