Skip to content

Commit fe2cc21

Browse files
authored
Merge pull request #950 from ergebnis/fix/property-fetch
Fix: Adjust `CallLikes\NoNamedArgumentRule` to handle calls on callables assigned to properties
2 parents c688608 + 0da8520 commit fe2cc21

File tree

6 files changed

+34
-0
lines changed

6 files changed

+34
-0
lines changed

.php-cs-fixer.fixture.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
$config->getFinder()
4040
->in(__DIR__ . '/test/Fixture/')
4141
->notPath([
42+
'CallLikes/NoNamedArgumentRule/ClassUsingInvokableClass.php',
4243
'CallLikes/NoNamedArgumentRule/script.php',
4344
'Closures/NoNullableReturnTypeDeclarationRule/script.php',
4445
'Closures/NoParameterWithNullableTypeDeclarationRule/script.php',

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ For a full diff see [`2.10.3...main`][2.10.3...main].
1212

1313
- Adjusted `Methods\NoNamedArgumentRule` to handle static calls on variable expressions ([#947]), by [@localheinz]
1414
- Adjusted `Methods\NoNamedArgumentRule` to handle calls on invokables ([#948]), by [@localheinz]
15+
- Adjusted `Methods\NoNamedArgumentRule` to handle calls on callables assigned to properties ([#949]), by [@localheinz]
1516

1617
## [`2.10.3`][2.10.3]
1718

@@ -691,6 +692,7 @@ For a full diff see [`362c7ea...0.1.0`][362c7ea...0.1.0].
691692
[#944]: https://github.com/ergebnis/phpstan-rules/pull/944
692693
[#947]: https://github.com/ergebnis/phpstan-rules/pull/947
693694
[#948]: https://github.com/ergebnis/phpstan-rules/pull/948
695+
[#949]: https://github.com/ergebnis/phpstan-rules/pull/949
694696

695697
[@cosmastech]: https://github.com/cosmastech
696698
[@enumag]: https://github.com/enumag

composer-require-checker.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"PhpParser\\Node\\Expr\\Isset_",
2222
"PhpParser\\Node\\Expr\\MethodCall",
2323
"PhpParser\\Node\\Expr\\New_",
24+
"PhpParser\\Node\\Expr\\PropertyFetch",
2425
"PhpParser\\Node\\Expr\\StaticCall",
2526
"PhpParser\\Node\\Expr\\Variable",
2627
"PhpParser\\Node\\Identifier",

src/CallLikes/NoNamedArgumentRule.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,13 @@ private static function describeCallable(
7878
if ($node instanceof Node\Expr\FuncCall) {
7979
$functionName = $node->name;
8080

81+
if ($functionName instanceof Node\Expr\PropertyFetch) {
82+
return \sprintf(
83+
'Callable referenced by property $%s',
84+
$functionName->name,
85+
);
86+
}
87+
8188
if ($functionName instanceof Node\Expr\Variable) {
8289
return \sprintf(
8390
'Callable referenced by $%s',
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Ergebnis\PHPStan\Rules\Test\Fixture\CallLikes\NoNamedArgumentRule;
4+
5+
final class ClassUsingInvokableClass
6+
{
7+
private InvokableClass $invokableClass;
8+
9+
public function __construct(InvokableClass $invokableClass)
10+
{
11+
$this->invokableClass = $invokableClass;
12+
}
13+
14+
public function bar($baz): void
15+
{
16+
($this->invokableClass)($baz);
17+
($this->invokableClass)(bar: $baz);
18+
}
19+
}

test/Integration/CallLikes/NoNamedArgumentRuleTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ public function testNoNamedArgumentRule(): void
3434
$this->analyse(
3535
self::phpFilesIn(__DIR__ . '/../../Fixture/CallLikes/NoNamedArgumentRule'),
3636
[
37+
[
38+
'Callable referenced by property $invokableClass is invoked with named argument for parameter $bar.',
39+
17,
40+
],
3741
[
3842
'Function json_encode() is invoked with named argument for parameter $value.',
3943
8,

0 commit comments

Comments
 (0)