Skip to content

PHPStan insists a use variable by reference cannot change types  #7751

Closed
phpstan/phpstan-src
#6610

Description

@neildaniels

Bug report

When writing a Closure with a use reference variable, PHPStan assumes the Type of that variable is always what it was at the place where the code is written.

Code snippet that reproduces the problem

$foo = false;

$test = function() use (&$foo) {
	if (is_array($foo)) {
		echo 'array';
	}
	else {
		echo 'not array';	
	}
};

$foo = [];

$test();

The code above actually prints array, even though PHPStan insists that:

Call to function is_array() with false will always evaluate to false.

https://phpstan.org/r/6967bdde-8c8f-476d-adcd-c94db723cd8b

Expected output

No reported error.

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

Yes! Love having a another pair of eyes on my code!

Activity

  1. ondrejmirtes commented on Aug 6, 2022

    @ondrejmirtes
    Member

    References are tricky for static analysis. Use this workaround: https://phpstan.org/r/d0a6e763-62c0-42f2-a99f-6817809609bd

  2. deleted a comment from phpstan-bot on Mar 31, 2023
  3. deleted a comment from phpstan-bot on Mar 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions