Skip to content

Commit dab2fe6

Browse files
authored
Support array callbacks like [$this, 'method'] (#42)
1 parent 6dbe16f commit dab2fe6

File tree

3 files changed

+12
-18
lines changed

3 files changed

+12
-18
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ class MyEntrypointProvider implements EntrypointProvider
4949

5050
- Only method calls are detected
5151
- Including static methods, trait methods, interface methods, first class callables, etc.
52-
- Callbacks like `[$this, 'method']` are mostly not detected; prefer first class callables `$this->method(...)`
5352
- Any calls on mixed types are not detected, e.g. `$unknownClass->method()`
5453
- Expression method calls are not detected, e.g. `$this->$methodName()`
5554
- Anonymous classes are ignored

src/Collector/MethodCallCollector.php

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
use PhpParser\Node;
66
use PhpParser\Node\Attribute;
77
use PhpParser\Node\Expr;
8+
use PhpParser\Node\Expr\Array_;
89
use PhpParser\Node\Expr\CallLike;
9-
use PhpParser\Node\Expr\FuncCall;
1010
use PhpParser\Node\Expr\MethodCall;
1111
use PhpParser\Node\Expr\New_;
1212
use PhpParser\Node\Expr\NullsafeMethodCall;
@@ -70,8 +70,8 @@ public function processNode(
7070
$this->registerStaticCall($node, $scope);
7171
}
7272

73-
if ($node instanceof FuncCall) {
74-
$this->registerFuncCall($node, $scope);
73+
if ($node instanceof Array_) {
74+
$this->registerArrayCallable($node, $scope);
7575
}
7676

7777
if ($node instanceof Attribute) {
@@ -153,23 +153,13 @@ private function registerStaticCall(
153153
}
154154
}
155155

156-
private function registerFuncCall(
157-
FuncCall $functionCall,
156+
private function registerArrayCallable(
157+
Array_ $array,
158158
Scope $scope
159159
): void
160160
{
161-
if (!$functionCall->name instanceof Name || $functionCall->name->toString() !== 'array_map') { // we should support all native fns
162-
return;
163-
}
164-
165-
if (!isset($functionCall->getArgs()[0])) {
166-
return;
167-
}
168-
169-
$callableType = $scope->getType($functionCall->getArgs()[0]->value);
170-
171-
if ($callableType->isCallable()->yes()) {
172-
foreach ($callableType->getConstantArrays() as $constantArray) {
161+
if ($scope->getType($array)->isCallable()->yes()) {
162+
foreach ($scope->getType($array)->getConstantArrays() as $constantArray) {
173163
$callableTypeAndNames = $constantArray->findTypeAndMethodNames();
174164

175165
foreach ($callableTypeAndNames as $typeAndName) {

tests/Rule/data/DeadMethodRule/array-map-1.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ class ArrayMapTest
88
public function __construct()
99
{
1010
array_map([$this, 'calledMagically'], ['a']);
11+
array_filter([], [$this, 'calledMagically2']);
12+
[$this, 'calledMagically3'];
1113
}
1214

1315
private function notCalledMagically(string $foo): string // error: Unused DeadMap\ArrayMapTest::notCalledMagically
@@ -19,6 +21,9 @@ private function calledMagically(string $foo): string
1921
{
2022
return $foo;
2123
}
24+
25+
private function calledMagically2(): void {}
26+
private function calledMagically3(): void {}
2227
}
2328

2429
new ArrayMapTest();

0 commit comments

Comments
 (0)