Repository navigation
Expand file tree
/
Copy pathMockObjectExprDetector.php
More file actions
189 lines (153 loc) · 6.38 KB
/
Copy pathMockObjectExprDetector.php
File metadata and controls
189 lines (153 loc) · 6.38 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php
declare(strict_types=1);
namespace Rector\PHPUnit\CodeQuality\NodeAnalyser;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Identifier;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Property;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Type\ObjectType;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\NodeTypeResolver;
use Rector\PhpParser\Node\BetterNodeFinder;
use Rector\PHPUnit\CodeQuality\NodeFinder\VariableFinder;
use Rector\PHPUnit\Enum\PHPUnitClassName;
use Rector\Reflection\ReflectionResolver;
final readonly class MockObjectExprDetector
{
public function __construct(
private BetterNodeFinder $betterNodeFinder,
private NodeNameResolver $nodeNameResolver,
private VariableFinder $variableFinder,
private ReflectionResolver $reflectionResolver,
private NodeTypeResolver $nodeTypeResolver,
) {
}
public function hasMethodCallWithoutExpects(ClassMethod $classMethod): bool
{
/** @var array<Expr\MethodCall> $methodCalls */
$methodCalls = $this->betterNodeFinder->findInstancesOfScoped((array) $classMethod->stmts, [MethodCall::class]);
foreach ($methodCalls as $methodCall) {
// dynamic method call, e.g. $sut->$method(), is not a literal ->method() call
if (! $methodCall->name instanceof Identifier) {
continue;
}
if (! $this->nodeNameResolver->isName($methodCall->name, 'method')) {
continue;
}
if ($methodCall->var instanceof MethodCall) {
continue;
}
// stubs, e.g. from createStub(), do not need expectations
if ($this->isStubOnly($methodCall->var)) {
continue;
}
return true;
}
return false;
}
public function isUsedForMocking(Expr $expr, ClassMethod $classMethod): bool
{
if (! $expr instanceof Variable) {
return false;
}
$variableName = $this->nodeNameResolver->getName($expr);
// to be safe
if ($variableName === null) {
return true;
}
$relatedVariables = $this->variableFinder->find($classMethod, $variableName);
// only self variable found, nothing to mock
if (count($relatedVariables) === 1) {
return false;
}
// find out, how many are used in call likes as args
/** @var array<Expr\MethodCall> $methodCalls */
$methodCalls = $this->betterNodeFinder->findInstancesOfScoped((array) $classMethod->stmts, [MethodCall::class]);
$mockObjectType = new ObjectType(PHPUnitClassName::MOCK_OBJECT);
foreach ($methodCalls as $methodCall) {
if (! $methodCall->var instanceof Variable) {
continue;
}
if ($this->nodeNameResolver->isName($methodCall->var, $variableName)) {
// variable is being called on, most like mocking, lets skip
return true;
}
if ($methodCall->isFirstClassCallable()) {
continue;
}
// check if variable is passed as arg to a method that declares MockObject type parameter
foreach ($methodCall->getArgs() as $argIndex => $arg) {
if (! $arg->value instanceof Variable) {
continue;
}
if (! $this->nodeNameResolver->isName($arg->value, $variableName)) {
continue;
}
$methodReflection = $this->reflectionResolver->resolveMethodReflectionFromMethodCall($methodCall);
if (! $methodReflection instanceof MethodReflection) {
continue;
}
$variants = $methodReflection->getVariants();
foreach ($variants as $variant) {
$parameters = $variant->getParameters();
foreach ($parameters as $parameter) {
$paramType = $parameter->getType();
if ($arg->name instanceof Identifier
&& $this->nodeNameResolver->isName($arg->name, $parameter->getName())
&& $mockObjectType->isSuperTypeOf($paramType)
->yes()) {
return true;
}
}
if (isset($parameters[$argIndex])) {
$paramType = $parameters[$argIndex]->getType();
if ($mockObjectType->isSuperTypeOf($paramType)->yes()) {
return true;
}
}
}
}
}
return false;
}
public function isPropertyUsedForMocking(Class_ $class, string $propertyName): bool
{
$property = $class->getProperty($propertyName);
// possibly dynamic property on purpose
// mark as used
if (! $property instanceof Property) {
return true;
}
// find out, how many are used in call likes as args;
// not scoped on purpose, as expects() can be nested in closures, e.g. willReturnCallback()
/** @var array<Expr\MethodCall> $methodCalls */
$methodCalls = $this->betterNodeFinder->findInstancesOf($class->getMethods(), [MethodCall::class]);
foreach ($methodCalls as $methodCall) {
if (! $methodCall->var instanceof PropertyFetch) {
continue;
}
$propertyFetch = $methodCall->var;
if ($this->nodeNameResolver->isName($propertyFetch->name, $propertyName)) {
// variable is being called on, most like mocking, lets skip
return true;
}
}
return false;
}
private function isStubOnly(Expr $expr): bool
{
$exprType = $this->nodeTypeResolver->getType($expr);
$stubObjectType = new ObjectType(PHPUnitClassName::STUB);
if (! $stubObjectType->isSuperTypeOf($exprType)->yes()) {
return false;
}
$mockObjectType = new ObjectType(PHPUnitClassName::MOCK_OBJECT);
return ! $mockObjectType->isSuperTypeOf($exprType)
->yes();
}
}