Skip to content

function.alreadyNarrowedType and function.impossibleType after updates to 2.1.15 #13023

@olelis

Description

@olelis

Bug report

After updates to version 2.1.15 from 2.1.14, phpstan started to report errors on the previously correct code.

Example code:

class SomeClass{    use MyTrait;}
class SomeClass2{    use MyTrait;}

trait MyTrait
{
    public function getRandom(): int
    {
        $value = random_int(1, 100);
        if (is_a($this, SomeClass::class)) { // Here we have an "error"
            return $value * $value;
        }
        return $value;
    }
}

Errors are:

 Call to function is_a() with $this(SomeClass2) and 'SomeClass' will always evaluate to false.  
         🪪 function.impossibleType     
Call to function is_a() with $this(SomeClass) and 'SomeClass' will always evaluate to true.
         Error Identifier: function.alreadyNarrowedType

Previously there was no errors and code runs without an issues.

Code snippet that reproduces the problem

https://phpstan.org/r/1638d012-268e-49ba-afeb-2924a69571c3

Expected output

no errors

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
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions