Skip to content

Incorrect inference of instance variable modified from callback #8926

@jbafford

Description

@jbafford

Bug report

When an array_filter filter has a side-effect (modifying a variable outside the callback's internal scope), and that side-effect modifies an instance member, phpstan makes an incorrect inference as to its possible values, which then can cause it to complain that an if condition is always false.

The issue is not limited to array_filter. In the generic case, an immediately-invoked function expression exhibits the same behavior.

Code snippet that reproduces the problem

array_filter: https://phpstan.org/r/2bc499c5-ce35-4ab6-a8e6-f8be4314b9de

IIFE: https://phpstan.org/r/6efc8f30-3e01-4cd9-9bc9-920eb67d54e6

Expected output

There should be no error. This is demonstrated with function success: phpstan correctly infers that the local $test can be mutated, and can be either true or false.

However, in the second case, function error, phpstan does not recognize that $this->test has the possibility to be mutated in the filter/IIFE, and so goes on to complain that, If condition is always false., even though $this->test can in fact be true.

Did PHPStan help you today? Did it make you happy in any way?

Found this error while doing some refactoring work, which phpstan always makes much easier than it would otherwise be.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions