Repository navigation
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
Conversation
VincentLanglet
approved these changes
Nov 25, 2026
Contributor
|
Current behavior https://phpstan.org/r/c7473a6d-86a4-4904-a0d4-53444b5aa49f |
…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
force-pushed
the
create-pull-request/patch-42irhqi
branch
from
November 30, 2026 09:51
210adea to
0d0cd54
Compare
staabm
reviewed
Nov 30, 2026
staabm
left a comment
Contributor
There was a problem hiding this comment.
in case this fixes phpstan/phpstan#15894 - add a regression test
Contributor
|
@staabm I'm not sure but I think all the phpstan-bot workflow are broken atm. Could be worth merging it like this (?) |
Contributor
|
I think we are not in a hurry and can wait. seems ondrej is working on it already |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 ownGetenvFunctionReturnTypeExtension(a79395e). The same regression also affected every other multi-variant built-in, for examplemb_detect_order(encoding: 'UTF-8')(array|boolinstead ofbool) andstrtok(string: $s, token: ' ')(string|falseinstead ofnon-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: newselectFromNamedTypes()(and privategetPositionalTypes()), used byselectFromArgs()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 oneExtendedParametersAcceptornamed variant.src/Analyser/ArgumentsHandler.php:selectArgsAcceptor()uses the same method, so the pre-resolved acceptor agrees withselectFromArgs().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 ofselectFromNamedTypes(): reordering, default filling, unknown name, unknown default, variadic name, unpack, and the fallbacks.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
Php8SignatureMapProviderreturns[$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()andstrtok()called with named arguments in various orders, mixed with positional ones, and through first-class callables. It fails without the fix.bug-15789.php(getenv) also passes through the new path with the dedicated extension disabled.Fixes phpstan/phpstan#15789
🤖 Generated with Claude Code