|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Rector\PHPUnit\CodeQuality\NodeAnalyser; |
| 6 | + |
| 7 | +use PhpParser\Node\Expr; |
| 8 | +use PhpParser\Node\Expr\MethodCall; |
| 9 | +use PhpParser\Node\Expr\StaticCall; |
| 10 | +use PhpParser\Node\Expr\Variable; |
| 11 | +use PhpParser\Node\Stmt\ClassMethod; |
| 12 | +use Rector\NodeNameResolver\NodeNameResolver; |
| 13 | +use Rector\PhpParser\Node\BetterNodeFinder; |
| 14 | +use Rector\PHPUnit\CodeQuality\NodeFinder\VariableFinder; |
| 15 | + |
| 16 | +final readonly class MockObjectExprDetector |
| 17 | +{ |
| 18 | + public function __construct( |
| 19 | + private BetterNodeFinder $betterNodeFinder, |
| 20 | + private NodeNameResolver $nodeNameResolver, |
| 21 | + private VariableFinder $variableFinder, |
| 22 | + ) { |
| 23 | + } |
| 24 | + |
| 25 | + public function isUsedForMocking(Expr $expr, ClassMethod $classMethod): bool |
| 26 | + { |
| 27 | + if (! $expr instanceof Variable) { |
| 28 | + return false; |
| 29 | + } |
| 30 | + |
| 31 | + $variableName = $this->nodeNameResolver->getName($expr); |
| 32 | + |
| 33 | + // to be safe |
| 34 | + if ($variableName === null) { |
| 35 | + return true; |
| 36 | + } |
| 37 | + |
| 38 | + $relatedVariables = $this->variableFinder->find($classMethod, $variableName); |
| 39 | + |
| 40 | + // only self variable found, nothing to mock |
| 41 | + if (count($relatedVariables) === 1) { |
| 42 | + return false; |
| 43 | + } |
| 44 | + |
| 45 | + // find out, how many are used in call likes as args |
| 46 | + /** @var array<Expr\MethodCall> $methodCalls */ |
| 47 | + $methodCalls = $this->betterNodeFinder->findInstancesOfScoped((array) $classMethod->stmts, [MethodCall::class]); |
| 48 | + |
| 49 | + foreach ($methodCalls as $methodCall) { |
| 50 | + if (! $methodCall->var instanceof Variable) { |
| 51 | + continue; |
| 52 | + } |
| 53 | + |
| 54 | + if ($this->nodeNameResolver->isName($methodCall->var, $variableName)) { |
| 55 | + // variable is being called on, most like mocking, lets skip |
| 56 | + return true; |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + return false; |
| 61 | + } |
| 62 | + |
| 63 | + public function isUsedInAssert(Expr $expr, ClassMethod $classMethod): bool |
| 64 | + { |
| 65 | + if (! $expr instanceof Variable) { |
| 66 | + return false; |
| 67 | + } |
| 68 | + |
| 69 | + $variableName = $this->nodeNameResolver->getName($expr); |
| 70 | + |
| 71 | + // to be safe |
| 72 | + if ($variableName === null) { |
| 73 | + return true; |
| 74 | + } |
| 75 | + |
| 76 | + /** @var array<MethodCall|StaticCall> $callLikes */ |
| 77 | + $callLikes = $this->betterNodeFinder->findInstancesOfScoped((array) $classMethod->stmts, [MethodCall::class, StaticCall::class]); |
| 78 | + |
| 79 | + foreach ($callLikes as $callLike) { |
| 80 | + $methodName = $this->nodeNameResolver->getName($callLike->name); |
| 81 | + if (! str_starts_with((string) $methodName, 'assert')) { |
| 82 | + continue; |
| 83 | + } |
| 84 | + |
| 85 | + foreach ($callLike->args as $arg) { |
| 86 | + if (! $arg->value instanceof Variable) { |
| 87 | + continue; |
| 88 | + } |
| 89 | + |
| 90 | + if (! $this->nodeNameResolver->isName($arg->value, $variableName)) { |
| 91 | + continue; |
| 92 | + } |
| 93 | + |
| 94 | + return true; |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + return false; |
| 99 | + } |
| 100 | +} |
0 commit comments