Skip to content

By-reference variables in use clauses for anonymous functions are assumed to not change value #14848

Description

@fredricj

Bug report

When using an anonymous function with a reference in use , the value checked is the value at the time of function declaration and ignores that the value may change.
https://phpstan.org/r/920ed53d-18a7-477d-b478-accfc6f8e8be

<?php

$tmpdir = "";
$myfunc = function () use (&$tmpdir) {
	if ($tmpdir !== "" && file_exists($tmpdir)) {
		echo $tmpdir;
	}
};

$tmpdir = "/tmp/my/useful/tempdir";
$myfunc();

Gives

Result of && is always false.
Strict comparison using !== between '' and '' will always evaluate to false.

Code snippet that reproduces the problem

https://phpstan.org/r/920ed53d-18a7-477d-b478-accfc6f8e8be

Expected output

no error

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