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
BenevolentUnionType with generics produce confusing errors #9732
Seems like this is something to do with the BenevolentUnionType used for the keys of globals. The same also happens with $_SERVER and other superglobals.
Again, I'd be happy to work on a fix if I can get a pointer where to start looking for the problem.
Looks like the problem is actually the type itself, rather than the superglobals. I assumed that $GLOBALS would be considered to have type array<string, mixed>. When using an array with array-key for the key, the problem is the same.
It appears that the real issue here is BenevolentUnionType is suppressing the errors that would make this problem understandable.
When using a non-benevolent type like array<int|string, mixed> it reports two errors, the second of which makes it very clear where the problem is coming from (see line 34 errors): https://phpstan.org/r/a24f065e-16d5-46a4-bc4f-11db8605284c
@@ @@
16: No error to ignore is reported on line 16.
-23: Unable to resolve the template type TKeyType in call to method static method HelloWorld::stringifyKeys()+23: Unable to resolve the template type TKeyType in call to static method HelloWorld::stringifyKeys()
Full report
Line
Error
16
No error to ignore is reported on line 16.
23
Unable to resolve the template type TKeyType in call to static method HelloWorld::stringifyKeys()
@@ @@-26: Unable to resolve the template type TKeyType in call to method static method HelloWorld::stringifyKeys()+26: Unable to resolve the template type TKeyType in call to static method HelloWorld::stringifyKeys()
34: Parameter #1 $array of static method HelloWorld::stringifyKeys() expects array<string, mixed>, array<int|string, mixed> given.
-34: Unable to resolve the template type TKeyType in call to method static method HelloWorld::stringifyKeys()+34: Unable to resolve the template type TKeyType in call to static method HelloWorld::stringifyKeys()
Full report
Line
Error
26
Unable to resolve the template type TKeyType in call to static method HelloWorld::stringifyKeys()
Activity
dktapps commentedon Sep 21, 2023
Seems like this is something to do with the BenevolentUnionType used for the keys of globals. The same also happens with
$_SERVERand other superglobals.Again, I'd be happy to work on a fix if I can get a pointer where to start looking for the problem.
ondrejmirtes commentedon Sep 21, 2023
I'd look what happens in
ArrayType::inferTemplateTypes()first.dktapps commentedon Sep 21, 2023
Thanks, I'll take a look.
dktapps commentedon Sep 21, 2023
Looks like the problem is actually the type itself, rather than the superglobals. I assumed that
$GLOBALSwould be considered to have typearray<string, mixed>. When using an array witharray-keyfor the key, the problem is the same.It appears that the real issue here is
BenevolentUnionTypeis suppressing the errors that would make this problem understandable.When using a non-benevolent type like
array<int|string, mixed>it reports two errors, the second of which makes it very clear where the problem is coming from (see line 34 errors):https://phpstan.org/r/a24f065e-16d5-46a4-bc4f-11db8605284c
[-]Generic key inference from $GLOBALS fails[/-][+]BenevolentUnionType with generics produce confusing errors[/+]dktapps commentedon Sep 21, 2023
Related PR: phpstan/phpstan-src#2446
phpstan-bot commentedon Sep 26, 2025
@dktapps After the latest push in 2.1.x, PHPStan now reports different result with your code snippet:
Full report
No error to ignore is reported on line 16.Unable to resolve the template type TKeyType in call to static method HelloWorld::stringifyKeys()phpstan-bot commentedon Sep 26, 2025
@dktapps After the latest push in 2.1.x, PHPStan now reports different result with your code snippet:
Full report
Unable to resolve the template type TKeyType in call to static method HelloWorld::stringifyKeys()Unable to resolve the template type TKeyType in call to static method HelloWorld::stringifyKeys()Fix BenevolentUnionType with generics producing false positive errors
Fix phpstan/phpstan#9732: BenevolentUnionType with generics produce c…
phpstan-bot commentedon Mar 2, 2026
Closed via merging phpstan/phpstan-src#5070
Updated PHPStan to commit 0a4583df557c63266808465d2f7b9ac518e6814f
Updated PHPStan to commit dcfce1012b16e49d56e1bd1b0abe39ae97b4ec36
github-actions commentedon Apr 3, 2026
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.
Fix phpstan/phpstan#9732: BenevolentUnionType with generics produce c…