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
Wrong return type for getenv when using named arguments as of 2.3.6 #15789
According to the documentation (https://www.php.net/manual/en/function.getenv.php#refsect1-function.getenv-returnvalues), getenv can only return array when its $name argument is null, otherwise it returns string|false. As of PHPStan 2.3.6, this type narrowing appears to be circumvented when using named arguments (either for $name or $local_only), which causes getenv to return string|array|false regardless of whether $name is null.
Did PHPStan help you today? Did it make you happy in any way?
I recently got a ~25-year old codebase I maintain to level 10 after years of chipping away at legacy design problems that got in the way of static analysis. I finally feel confident developing new code for this software, thanks to the amazing work of the PHPStan authors!
I had a look at when this regressed. It comes in two steps:
Since a16fa0bb7 ("Resolve named arguments of multi-variant built-ins against their php-src signature"), a call with any named argument is resolved against [$nativeSignature] only, so it gets the php-8-stubs return type string|array|false and the two getenv variants from the function map are never considered. That's why getenv(local_only: true) is affected as well.
Before that, getenv(name: 'FOO') was already imprecise (array<string, string>|string|false), because functionMap_php80delta.php still calls the parameter varname, so the named argument fitted no variant and they were all combined.
Results on the same snippet (6 named calls, plus positional ones that are fine everywhere):
wrong types
2.3.5
4 (name: calls)
a16fa0bb7~1
4
a16fa0bb7 / 2.3.x
6 (all named calls, now array|string|false)
A possible fix would be to keep resolving the names against the native signature, then put the arguments back in positional order and select among the function map variants as for a positional call. ParametersAcceptorSelector is shadowed by the turbo extension though, so it would need the same change in the C++ mirror. Happy to send a PR if that direction sounds right to you, or to leave it to you if you'd rather handle it alongside a16fa0bb7.
Activity
staabm commentedon Nov 22, 2026
We should check when/how this regressed
Amoifr commentedon Nov 24, 2026
I had a look at when this regressed. It comes in two steps:
[$nativeSignature]only, so it gets the php-8-stubs return typestring|array|falseand the twogetenvvariants from the function map are never considered. That's whygetenv(local_only: true)is affected as well.getenv(name: 'FOO')was already imprecise (array<string, string>|string|false), becausefunctionMap_php80delta.phpstill calls the parametervarname, so the named argument fitted no variant and they were all combined.Results on the same snippet (6 named calls, plus positional ones that are fine everywhere):
name:calls)array|string|false)A possible fix would be to keep resolving the names against the native signature, then put the arguments back in positional order and select among the function map variants as for a positional call.
ParametersAcceptorSelectoris shadowed by the turbo extension though, so it would need the same change in the C++ mirror. Happy to send a PR if that direction sounds right to you, or to leave it to you if you'd rather handle it alongside a16fa0bb7.staabm commentedon Nov 24, 2026
This should be fixable by adding a DynamicFunctionReturnTypeExtension for
getenv.feel free to try it in a PR