Skip to content

Wrong return type for getenv when using named arguments as of 2.3.6 #15789

Done

Description

@hemberger

Bug report

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.

Code snippet that reproduces the problem

https://phpstan.org/r/5d237de0-676b-4787-96a3-d53cbef8edcd

Expected output

No errors expected.

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!

Activity

  1. staabm commented on Nov 22, 2026

    @staabm
    Contributor

    We should check when/how this regressed

  2. added this to the Easy fixes milestone on Nov 22, 2026
  3. Amoifr commented on Nov 24, 2026

    @Amoifr

    I had a look at when this regressed. It comes in two steps:

    1. 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.
    2. 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.

  4. staabm commented on Nov 24, 2026

    @staabm
    Contributor

    This should be fixable by adding a DynamicFunctionReturnTypeExtension for getenv.

    feel free to try it in a PR

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

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions