-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathMockObjectExprDetector.php
More file actions
132 lines (104 loc) · 4.03 KB
/
MockObjectExprDetector.php
File metadata and controls
132 lines (104 loc) · 4.03 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
<?php
declare(strict_types=1);
namespace Rector\PHPUnit\CodeQuality\NodeAnalyser;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\CallLike;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\PhpParser\Node\BetterNodeFinder;
use Rector\PHPUnit\CodeQuality\NodeFinder\VariableFinder;
final readonly class MockObjectExprDetector
{
public function __construct(
private BetterNodeFinder $betterNodeFinder,
private NodeNameResolver $nodeNameResolver,
private VariableFinder $variableFinder,
) {
}
public function hasMethodCallWithoutExpects(ClassMethod $classMethod): bool
{
/** @var array<Expr\MethodCall> $methodCalls */
$methodCalls = $this->betterNodeFinder->findInstancesOfScoped((array) $classMethod->stmts, [MethodCall::class]);
foreach ($methodCalls as $methodCall) {
if (! $this->nodeNameResolver->isName($methodCall->name, 'method')) {
continue;
}
if ($methodCall->var instanceof MethodCall) {
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]);
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;
}
}
return false;
}
public function isPropertyUsedForMocking(Class_ $class, string $propertyName): bool
{
// find out, how many are used in call likes as args
/** @var array<Expr\MethodCall> $methodCalls */
$methodCalls = $this->betterNodeFinder->findInstancesOfScoped($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;
}
public function isPropertyMockObjectPassedAsArgument(Class_ $class, string $propertyName): bool
{
/** @var array<Expr\CallLike> $callLikes */
$callLikes = $this->betterNodeFinder->findInstancesOfScoped($class->getMethods(), [CallLike::class]);
foreach ($callLikes as $callLike) {
if ($callLike->isFirstClassCallable()) {
continue;
}
foreach ($callLike->getArgs() as $arg) {
if (! $arg->value instanceof PropertyFetch) {
continue;
}
$propertyFetch = $arg->value;
// its used in arg
if ($this->nodeNameResolver->isName($propertyFetch->name, $propertyName)) {
return true;
}
}
}
return false;
}
}