You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Value of a variable is forgotten in case where it ought to be known with certainty #11705
I have encountered a scenario where the value of a variable appears to be 'forgotten' by phpstan in a case where it ought to be known with certainty, and as a result, phpstan fails to narrow a type and so reports an incorrect error.
Did PHPStan help you today? Did it make you happy in any way?
Phpstan doesn't eliminate the need for testing to check that the code does what it is supposed to do, of course, but the code becomes more precisely specified and so dependable, which makes it easier to maintain. Developing or working with a code base that has been raised to a high phpstan strictness level is a pleasure, since phpstan will quickly warn you if you attempt to make a change of dubious quality. Very happy about this!
changed the title [-]Value and type of variable are forgotten in case where they ought to be known with certainty[/-][+]Value of a variable is forgotten in case where it ought to be known with certainty[/+]on Sep 17, 2024
I don't think we can fix this in ArrayKeyExistsFunctionTypeSpecifyingExtension because at that point we only know $tag will be 'name'|'owners'. via switch we later on narrow $tag to 'name'.
I think the fix would be to foget the remembered expression for $theInput[$tag], as soon as $tag gets narrowed.
that way scope-type-resolving would pickup the type from $theInput's constant array type.
Activity
[-]Value and type of variable are forgotten in case where they ought to be known with certainty[/-][+]Value of a variable is forgotten in case where it ought to be known with certainty[/+]ondrejmirtes commentedon Sep 18, 2024
Problem simplified to: https://phpstan.org/r/d62a02ab-388d-4fb8-81c6-a246ff6d831b
The problem lies in array_key_exists at the beginning, removing it fixes it: https://phpstan.org/r/1e120370-b233-467a-9eda-531745a0b920
Fix array dim fetch type not reflecting narrowed variable key
Fix phpstan/phpstan#11705: variable type forgotten for array dim fetc…
staabm commentedon Mar 12, 2026
the problem is that we have 2 different information for
$theInput[$tag].in Scope we remembered
$theInput[$tag]isstring|array<int,string>while for
$theInput-type we knowarray{name: string, owners: array<int, string>}see https://phpstan.org/r/4ebcaacf-37a1-4c65-ae95-a49c8fbc1cec
I don't think we can fix this in
ArrayKeyExistsFunctionTypeSpecifyingExtensionbecause at that point we only know$tagwill be'name'|'owners'. via switch we later on narrow$tagto'name'.I think the fix would be to foget the remembered expression for
$theInput[$tag], as soon as$taggets narrowed.that way scope-type-resolving would pickup the type from
$theInput's constant array type.wdyt @ondrejmirtes ?
Fix phpstan/phpstan#11705: move fix from MutatingScope to ArrayKeyExi…
Fix phpstan/phpstan#11705: variable type forgotten for array dim fetc…
Fix phpstan/phpstan#11705: move fix from MutatingScope to ArrayKeyExi…
Fix phpstan/phpstan#11705: recompute ArrayDimFetch type in MutatingSc…
phpstan-bot commentedon Apr 24, 2026
@staabm After the latest push in 2.2.x, PHPStan now reports different result with your code snippet:
Full report
Dumped type: array{name: string, owners: array<int, string>}phpstan-bot commentedon Sep 20, 2026
@staabm After the latest push in 2.3.x, PHPStan now reports different result with your code snippet:
Full report
Dumped type: array{name: string, owners: array<int, string>}