Skip to content

Broken parameter type evaluation inside a closure for a by-ref variable #13614

Closed

Description

@speller

Bug report

Broken parameter type evaluation inside a closure for a by-ref variable.

Code:

<?php declare(strict_types = 1);

use function PHPStan\dumpType;
use function PHPStan\Testing\assertType;

enum FooEnum: string
{
    case BAR = 'bar';
    case BAZ = 'baz';
}

$var = [FooEnum::BAR];

$closure = function () use (&$var): array {
	$var = array_map(fn (FooEnum $item): string => $item->value, $var);
	return [
		'var' => array_flip($var),
	];
};

It reports the following error:

Parameter #1 $callback of function array_map expects (callable('bar'|FooEnum::BAR): mixed)|null, Closure(FooEnum): 'bar' given.
Type #1 from the union: Type FooEnum of parameter #1 $item of passed callable needs to be same or wider than parameter type FooEnum::BAR|string of accepting callable.
argument.type

At the same time, if I remove the closuer, this code doesn't trigger anything:

<?php declare(strict_types = 1);

use function PHPStan\dumpType;
use function PHPStan\Testing\assertType;

enum FooEnum: string
{
    case BAR = 'bar';
    case BAZ = 'baz';
}

$var = [FooEnum::BAR];

$var = array_map(fn (FooEnum $item): string => $item->value, $var);
return [
	'var' => array_flip($var),
];

No errors snippet: https://phpstan.org/r/3ca9bc47-a5e9-4d6f-86e3-164180021ab9

Code snippet that reproduces the problem

https://phpstan.org/r/5482060c-ec17-4566-b36f-e965cfc02a21

Expected output

No errors.

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

Always

Activity

  1. ondrejmirtes commented on Feb 5, 2026

    @ondrejmirtes
    Member

    This is expected. If you call the closure twice, it fails: https://3v4l.org/MtVgo

  2. github-actions commented on Mar 9, 2026

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

  3. locked as resolved and limited conversation to collaborators on Mar 9, 2026
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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions