Skip to content

Conversation

@janedbal
Copy link
Contributor

When processing function calls with named arguments, the parameter was incorrectly matched using positional index instead of argument name. This caused Scope::getFunctionCallStackWithParameters() to return the wrong parameter when:

  1. Arguments were passed out of order using named syntax
  2. A named argument was passed for an optional parameter while a required parameter was missing

The fix matches parameters by name for named arguments before falling back to positional matching.

…meters

When processing function calls with named arguments, the parameter was
incorrectly matched using positional index instead of argument name.
This caused Scope::getFunctionCallStackWithParameters() to return the
wrong parameter when:

1. Arguments were passed out of order using named syntax
2. A named argument was passed for an optional parameter while a
   required parameter was missing

The fix matches parameters by name for named arguments before falling
back to positional matching.
@janedbal janedbal force-pushed the fix-named-argument-parameter-matching branch from 698ece3 to 5bd7008 Compare January 20, 2026 15:47
janedbal added a commit to shipmonk-rnd/phpstan-rules that referenced this pull request Jan 20, 2026
Refactor the rule to use Scope::getFunctionCallStackWithParameters()
instead of relying on node processing order, which broke with PHPStan's
fiber-based processing.

The new implementation:
- Uses scope's call stack to determine if callable is immediately invoked
- Handles IIFEs (immediately invoked function expressions) separately
- Properly resolves named arguments to correct parameters

Note: There's a PHPStan bug with named argument parameter matching
that affects edge cases. Fix: phpstan/phpstan-src#4791
if (isset($parameters[$i])) {
$assignByReference = $parameters[$i]->passedByReference()->createsNewVariable();
$parameterType = $parameters[$i]->getType();
$matchedParameter = null;
Copy link
Member

Choose a reason for hiding this comment

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

This fix is in a wrong place. processArgs gets $normalizedExpr which is output of ArgumentsNormalizer. The fix should be there.

@janedbal janedbal force-pushed the fix-named-argument-parameter-matching branch from 5be9f41 to 5bd7008 Compare January 20, 2026 18:45
@ondrejmirtes ondrejmirtes merged commit 0e00d4b into phpstan:2.1.x Jan 20, 2026
1242 of 1272 checks passed
@ondrejmirtes
Copy link
Member

Thank you!

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.

2 participants