-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathMockObjectExprDetector.php
More file actions
84 lines (67 loc) · 2.58 KB
/
MockObjectExprDetector.php
File metadata and controls
84 lines (67 loc) · 2.58 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
<?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\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 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;
}
}