Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -5542,14 +5542,26 @@ private function processArgs(
$parameterType = null;
$parameterNativeType = null;
if ($parameters !== null) {
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.

if ($arg->name !== null) {
foreach ($parameters as $p) {
if ($p->getName() === $arg->name->toString()) {
$matchedParameter = $p;
break;
}
}
} elseif (isset($parameters[$i])) {
$matchedParameter = $parameters[$i];
}

if ($matchedParameter !== null) {
$assignByReference = $matchedParameter->passedByReference()->createsNewVariable();
$parameterType = $matchedParameter->getType();

if ($parameters[$i] instanceof ExtendedParameterReflection) {
$parameterNativeType = $parameters[$i]->getNativeType();
if ($matchedParameter instanceof ExtendedParameterReflection) {
$parameterNativeType = $matchedParameter->getNativeType();
}
$parameter = $parameters[$i];
$parameter = $matchedParameter;
} elseif (count($parameters) > 0 && $parametersAcceptor->isVariadic()) {
$lastParameter = array_last($parameters);
$assignByReference = $lastParameter->passedByReference()->createsNewVariable();
Expand Down
8 changes: 8 additions & 0 deletions tests/PHPStan/Rules/ScopeFunctionCallStackRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ public function testRule(): void
"var_dump\nprint_r\nsleep",
13,
],
[
'ScopeFunctionCallStack\NamedArgumentTest::testMethod',
31,
],
[
'ScopeFunctionCallStack\NamedArgumentTest::testMethod',
42,
],
]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ public function testRule(): void
"var_dump (\$value)\nprint_r (\$value)\nsleep (\$seconds)",
13,
],
[
// Named argument test - should report $notImmediate, not $immediate
'ScopeFunctionCallStack\NamedArgumentTest::testMethod ($notImmediate)',
31,
],
[
// Named argument with missing required param - should still match by name
'ScopeFunctionCallStack\NamedArgumentTest::testMethod ($notImmediate)',
42,
],
]);
}

Expand Down
32 changes: 32 additions & 0 deletions tests/PHPStan/Rules/data/scope-function-call-stack.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,35 @@ function (): void

var_dump(print_r(fn () => sleep(throw new \Exception())));
};

class NamedArgumentTest
{
/**
* @param-immediately-invoked-callable $immediate
*/
public function testMethod(callable $immediate, ?callable $notImmediate = null): void
{
$immediate();
}

public function test(): void
{
// When using named argument for $notImmediate, the parameter should be reported as "notImmediate", not "immediate"
$this->testMethod(
notImmediate: function () {
throw new \Exception(); // should report: NamedArgumentTest::testMethod ($notImmediate)
},
immediate: function () {},
);
}

public function testMissingRequiredArg(): void
{
// Named argument with missing required param - should still match parameter by name
$this->testMethod(
notImmediate: function () {
throw new \Exception(); // reports $notImmediate
},
);
}
}
Loading