Skip to content

Select the functionMap variant of a named call to a multi-variant built-in by putting its arguments in php-src signature order - #7155

Open
phpstan-bot wants to merge 1 commit into
phpstan:2.3.xfrom
phpstan-bot:create-pull-request/patch-42irhqi
Open

phpstan-bot wants to merge 1 commit into
phpstan:2.3.xfrom
phpstan-bot:create-pull-request/patch-42irhqi

Conversation

@phpstan-bot

Copy link
Copy Markdown
Collaborator

Summary

Since a16fa0b, a call to a multi-variant built-in that uses any named argument has been resolved only against its php-src signature. The functionMap variants were never considered, so the return type was the wide php-src one. getenv() already has its own GetenvFunctionReturnTypeExtension (a79395e). The same regression also affected every other multi-variant built-in, for example mb_detect_order(encoding: 'UTF-8') (array|bool instead of bool) and strtok(string: $s, token: ' ') (string|false instead of non-empty-string|false).

This PR fixes the regression in the selector itself, as suggested in the issue thread. It puts named arguments in the php-src signature's positional order, selects the functionMap variant the same way as for a positional call, and uses that variant's return type.

Changes

  • src/Reflection/ParametersAcceptorSelector.php: new selectFromNamedTypes() (and private getPositionalTypes()), used by selectFromArgs() for named calls. The returned acceptor keeps the native parameters (so named arguments still match by name) and takes its return / PHPDoc / native return types from the positional variant selected over the reordered types. It falls back to the old behaviour on unpacking, a name that matches no parameter, a variadic parameter's name, a skipped parameter with an unknown default, or anything other than one ExtendedParametersAcceptor named variant.
  • src/Analyser/ArgumentsHandler.php: selectArgsAcceptor() uses the same method, so the pre-resolved acceptor agrees with selectFromArgs().
  • turbo-ext/src/ParametersAcceptorSelector.cpp, turbo-ext/src/ArgumentsHandler.cpp, turbo-ext/src/support.h, turbo-ext/src/generated/ParametersAcceptorSelector.h: native mirror of the above.
  • turbo-ext/tests/type-family.php: differential coverage of selectFromNamedTypes(): reordering, default filling, unknown name, unknown default, variadic name, unpack, and the fallbacks.
  • Analogous cases: methods (MethodCallReturnTypeHelper), new, and first-class callables of built-ins called with named arguments use the same selector, so they are covered too. First-class callables were checked explicitly. sscanf(..., vars: $x) was probed and left alone: PHP rejects named arguments into an internal variadic.

Root cause

Php8SignatureMapProvider returns [$nativeSignature] as the only "named arguments variant" of a multi-variant built-in. selectFromTypes() then saw a single acceptor with the php-src return type. Even before that, selectFromTypes() could not compare named argument types against the variants, because it only looks up types by integer position. Converting the named types to positions using the php-src signature, which is what PHP itself does, lets the existing positional selection work.

Test

  • tests/PHPStan/Analyser/nsrt/bug-15789-variants.php: mb_detect_order() and strtok() called with named arguments in various orders, mixed with positional ones, and through first-class callables. It fails without the fix.
  • The existing bug-15789.php (getenv) also passes through the new path with the dedicated extension disabled.
  • turbo-ext: the smoke and type-family differential, signature parity, and identical analysis output with and without the extension.

Fixes phpstan/phpstan#15789

🤖 Generated with Claude Code

@VincentLanglet

Copy link
Copy Markdown
Contributor

…lt-in by putting its arguments in php-src signature order

- Add ParametersAcceptorSelector::selectFromNamedTypes(): named argument
  types are mapped to the position of their parameter in the php-src
  signature (skipped optional parameters filled with their known default),
  the functionMap variants are selected like for a positional call, and the
  result keeps the native parameters (names) with the selected variant's
  return type. Falls back to the previous behaviour for unpacking, unknown
  names, variadic names and unknown defaults.
- Use it from selectFromArgs() and ArgumentsHandler::selectArgsAcceptor().
- Port both to the turbo-ext C++ mirrors (ParametersAcceptorSelector.cpp,
  ArgumentsHandler.cpp), with differential coverage in type-family.php.
- Since a16fa0b named calls resolved only against the php-src signature, so
  getenv(name: ...), mb_detect_order(encoding: ...) and
  strtok(string: ..., token: ...) lost their variant-specific return types.
  getenv() was already patched by a dedicated extension; this fixes the
  whole family, including first-class callables. Built-in methods go through
  the same selector.
@staabm
staabm force-pushed the create-pull-request/patch-42irhqi branch from 210adea to 0d0cd54 Compare November 30, 2026 09:51

@staabm staabm left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we still need the extension added with #7139 after this PR? or can the extension be deleted without loosing functionality?

@staabm staabm left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in case this fixes phpstan/phpstan#15894 - add a regression test

@VincentLanglet

Copy link
Copy Markdown
Contributor

@staabm I'm not sure but I think all the phpstan-bot workflow are broken atm.

Could be worth merging it like this (?)

@staabm

staabm commented Nov 30, 2026

Copy link
Copy Markdown
Contributor

I think we are not in a hurry and can wait. seems ondrej is working on it already

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

3 participants