-
Notifications
You must be signed in to change notification settings - Fork 572
Expand file tree
/
Copy pathPossiblyPureStaticCallCollector.php
More file actions
79 lines (66 loc) · 1.92 KB
/
PossiblyPureStaticCallCollector.php
File metadata and controls
79 lines (66 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php declare(strict_types = 1);
namespace PHPStan\Rules\DeadCode;
use PhpParser\Node;
use PhpParser\Node\Stmt\Expression;
use PHPStan\Analyser\Scope;
use PHPStan\Collectors\Collector;
use PHPStan\DependencyInjection\RegisteredCollector;
/**
* @implements Collector<Node\Stmt\Expression, array{class-string, string, int}>
*/
#[RegisteredCollector(level: 4)]
final class PossiblyPureStaticCallCollector implements Collector
{
public function __construct()
{
}
public function getNodeType(): string
{
return Expression::class;
}
public function processNode(Node $node, Scope $scope)
{
$expr = $node->expr;
if ($expr instanceof Node\Expr\BinaryOp\Pipe) {
if ($expr->right instanceof Node\Expr\StaticCall) {
if (!$expr->right->isFirstClassCallable()) {
return null;
}
$expr = new Node\Expr\StaticCall($expr->right->class, $expr->right->name, []);
} elseif ($expr->right instanceof Node\Expr\ArrowFunction) {
$expr = $expr->right->expr;
}
}
if (!$expr instanceof Node\Expr\StaticCall || $expr->isFirstClassCallable()) {
return null;
}
if (!$expr->name instanceof Node\Identifier) {
return null;
}
if (!$expr->class instanceof Node\Name) {
return null;
}
$methodName = $expr->name->toString();
$calledOnType = $scope->resolveTypeByName($expr->class);
$methodReflection = $scope->getMethodReflection($calledOnType, $methodName);
if ($methodReflection === null) {
return null;
}
if (!$methodReflection->isPure()->maybe()) {
return null;
}
if (!$methodReflection->hasSideEffects()->maybe()) {
return null;
}
if (
$expr->class->toLowerString() === 'static'
&& $scope->isInClass()
&& !$scope->getClassReflection()->isFinal()
&& !$methodReflection->isFinal()->yes()
&& !$methodReflection->isPrivate()
) {
return null;
}
return [$methodReflection->getDeclaringClass()->getName(), $methodReflection->getName(), $node->getStartLine()];
}
}