Skip to content

False Positive of function.alreadyNarrowedType (function call variable assignment) #13268

@zanbaldwin

Description

@zanbaldwin

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions