-
-
Notifications
You must be signed in to change notification settings - Fork 741
Expand file tree
/
Copy pathCallCollectionAnalyzer.php
More file actions
95 lines (93 loc) · 3.73 KB
/
Copy pathCallCollectionAnalyzer.php
File metadata and controls
95 lines (93 loc) · 3.73 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php
declare (strict_types=1);
namespace Rector\DeadCode\NodeAnalyzer;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\NullsafeMethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PHPStan\Type\MixedType;
use Rector\Enum\ObjectReference;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\NodeTypeResolver;
use Rector\StaticTypeMapper\Resolver\ClassNameFromObjectTypeResolver;
final class CallCollectionAnalyzer
{
/**
* @readonly
*/
private NodeTypeResolver $nodeTypeResolver;
/**
* @readonly
*/
private NodeNameResolver $nodeNameResolver;
public function __construct(NodeTypeResolver $nodeTypeResolver, NodeNameResolver $nodeNameResolver)
{
$this->nodeTypeResolver = $nodeTypeResolver;
$this->nodeNameResolver = $nodeNameResolver;
}
/**
* @param StaticCall[]|MethodCall[]|NullsafeMethodCall[] $calls
*/
public function isExists(array $calls, string $classMethodName, string $className) : bool
{
foreach ($calls as $call) {
$callerRoot = $call instanceof StaticCall ? $call->class : $call->var;
$callerType = $this->nodeTypeResolver->getType($callerRoot);
$callerTypeClassName = ClassNameFromObjectTypeResolver::resolve($callerType);
if ($callerTypeClassName === null) {
// handle fluent by $this->bar()->baz()->qux()
// that methods don't have return type
if ($callerType instanceof MixedType && !$callerType->isExplicitMixed()) {
$cloneCallerRoot = clone $callerRoot;
$isFluent = \false;
// init
$methodCallNames = [];
// first append
$methodCallNames[] = (string) $this->nodeNameResolver->getName($call->name);
while ($cloneCallerRoot instanceof MethodCall) {
$methodCallNames[] = (string) $this->nodeNameResolver->getName($cloneCallerRoot->name);
if ($cloneCallerRoot->var instanceof Variable && $cloneCallerRoot->var->name === 'this') {
$isFluent = \true;
break;
}
$cloneCallerRoot = $cloneCallerRoot->var;
}
if ($isFluent && \in_array($classMethodName, $methodCallNames, \true)) {
return \true;
}
}
continue;
}
if ($this->isSelfStatic($call) && $this->shouldSkip($call, $classMethodName)) {
return \true;
}
if ($callerTypeClassName !== $className) {
continue;
}
if ($this->shouldSkip($call, $classMethodName)) {
return \true;
}
}
return \false;
}
/**
* @param \PhpParser\Node\Expr\MethodCall|\PhpParser\Node\Expr\StaticCall|\PhpParser\Node\Expr\NullsafeMethodCall $call
*/
private function isSelfStatic($call) : bool
{
return $call instanceof StaticCall && $call->class instanceof Name && \in_array($call->class->toString(), [ObjectReference::SELF, ObjectReference::STATIC], \true);
}
/**
* @param \PhpParser\Node\Expr\StaticCall|\PhpParser\Node\Expr\MethodCall|\PhpParser\Node\Expr\NullsafeMethodCall $call
*/
private function shouldSkip($call, string $classMethodName) : bool
{
if (!$call->name instanceof Identifier) {
return \true;
}
// the method is used
return $this->nodeNameResolver->isName($call->name, $classMethodName);
}
}