|
5 | 5 | namespace Rector\PHPUnit\NodeAnalyzer; |
6 | 6 |
|
7 | 7 | use PhpParser\Comment\Doc; |
| 8 | +use PhpParser\Node; |
| 9 | +use PhpParser\Node\Expr\StaticCall; |
8 | 10 | use PhpParser\Node\Stmt\Class_; |
| 11 | +use PhpParser\Node\Stmt\ClassLike; |
9 | 12 | use PhpParser\Node\Stmt\ClassMethod; |
| 13 | +use PHPStan\Analyser\Scope; |
| 14 | +use PHPStan\Reflection\ClassReflection; |
10 | 15 | use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory; |
| 16 | +use Rector\Enum\ObjectReference; |
11 | 17 | use Rector\NodeNameResolver\NodeNameResolver; |
12 | 18 | use Rector\Php80\NodeAnalyzer\PhpAttributeAnalyzer; |
| 19 | +use Rector\PhpParser\AstResolver; |
| 20 | +use Rector\PhpParser\Node\BetterNodeFinder; |
13 | 21 | use Rector\PHPUnit\Enum\PHPUnitAttribute; |
14 | | -use WeakMap; |
| 22 | +use Rector\PHPUnit\Enum\PHPUnitClassName; |
15 | 23 |
|
16 | 24 | /** |
17 | | - * PHPUnit counts assertions made in lifecycle methods toward every test of the class |
| 25 | + * PHPUnit counts assertions made in lifecycle methods of the class and its parents toward every test of the class |
18 | 26 | */ |
19 | | -final readonly class LifecycleMethodAssertAnalyzer |
| 27 | +final class LifecycleMethodAssertAnalyzer |
20 | 28 | { |
21 | 29 | /** |
22 | 30 | * @var string[] |
|
39 | 47 | private const array HOOK_ANNOTATIONS = ['before', 'after', 'preCondition', 'postCondition']; |
40 | 48 |
|
41 | 49 | /** |
42 | | - * @var WeakMap<Class_, bool> |
| 50 | + * @var array<string, bool> |
43 | 51 | */ |
44 | | - private WeakMap $assertingByClass; |
| 52 | + private array $assertingByParentClassAndOverriddenHooks = []; |
45 | 53 |
|
46 | 54 | public function __construct( |
47 | | - private AssertCallAnalyzer $assertCallAnalyzer, |
48 | | - private PhpAttributeAnalyzer $phpAttributeAnalyzer, |
49 | | - private PhpDocInfoFactory $phpDocInfoFactory, |
50 | | - private NodeNameResolver $nodeNameResolver, |
| 55 | + private readonly AssertCallAnalyzer $assertCallAnalyzer, |
| 56 | + private readonly PhpAttributeAnalyzer $phpAttributeAnalyzer, |
| 57 | + private readonly PhpDocInfoFactory $phpDocInfoFactory, |
| 58 | + private readonly NodeNameResolver $nodeNameResolver, |
| 59 | + private readonly AstResolver $astResolver, |
| 60 | + private readonly BetterNodeFinder $betterNodeFinder, |
51 | 61 | ) { |
52 | | - $this->assertingByClass = new WeakMap(); |
53 | 62 | } |
54 | 63 |
|
55 | | - public function hasAssertingLifecycleMethod(Class_ $class): bool |
| 64 | + public function hasAssertingLifecycleMethod(Class_ $class, Scope $scope): bool |
56 | 65 | { |
57 | | - // every test method of the class shares the result |
58 | | - return $this->assertingByClass[$class] ??= $this->resolveHasAssertingLifecycleMethod($class); |
| 66 | + $classReflection = $scope->getClassReflection(); |
| 67 | + if (! $classReflection instanceof ClassReflection) { |
| 68 | + return false; |
| 69 | + } |
| 70 | + |
| 71 | + return $this->isClassLikeAsserting($class, $classReflection, []); |
59 | 72 | } |
60 | 73 |
|
61 | | - private function resolveHasAssertingLifecycleMethod(Class_ $class): bool |
62 | | - { |
63 | | - foreach ($class->getMethods() as $classMethod) { |
64 | | - if (! $this->isLifecycleMethod($classMethod)) { |
65 | | - continue; |
| 74 | + /** |
| 75 | + * @param string[] $overriddenHookMethodNames |
| 76 | + */ |
| 77 | + private function isClassLikeAsserting( |
| 78 | + ?ClassLike $classLike, |
| 79 | + ClassReflection $classReflection, |
| 80 | + array $overriddenHookMethodNames |
| 81 | + ): bool { |
| 82 | + $classMethods = $classLike instanceof ClassLike ? $classLike->getMethods() : []; |
| 83 | + |
| 84 | + foreach ($classMethods as $classMethod) { |
| 85 | + $hookMethodName = $this->resolveHookMethodName($classMethod); |
| 86 | + |
| 87 | + if ($this->isRelevantHook($classMethod, $hookMethodName, $overriddenHookMethodNames) |
| 88 | + && $this->containsAssertCall($classMethod)) { |
| 89 | + return true; |
66 | 90 | } |
67 | 91 |
|
68 | | - $this->assertCallAnalyzer->resetNesting(); |
69 | | - if ($this->assertCallAnalyzer->containsAssertCall($classMethod)) { |
70 | | - return true; |
| 92 | + // a hook overridden here hides the parent one, unless it calls it through "parent::" |
| 93 | + if ($hookMethodName !== null && ! $this->hasParentCall($classMethod, $hookMethodName)) { |
| 94 | + $overriddenHookMethodNames[] = $hookMethodName; |
71 | 95 | } |
72 | 96 | } |
73 | 97 |
|
74 | | - return false; |
| 98 | + $parentClassReflection = $classReflection->getParentClass(); |
| 99 | + if (! $parentClassReflection instanceof ClassReflection) { |
| 100 | + return false; |
| 101 | + } |
| 102 | + |
| 103 | + return $this->isParentClassAsserting($parentClassReflection, $overriddenHookMethodNames); |
75 | 104 | } |
76 | 105 |
|
77 | | - private function isLifecycleMethod(ClassMethod $classMethod): bool |
| 106 | + /** |
| 107 | + * @param string[] $overriddenHookMethodNames |
| 108 | + */ |
| 109 | + private function isParentClassAsserting(ClassReflection $classReflection, array $overriddenHookMethodNames): bool |
78 | 110 | { |
79 | | - if ($this->nodeNameResolver->isNames($classMethod->name, self::HOOK_METHOD_NAMES)) { |
| 111 | + // do not analyze PHPUnit itself |
| 112 | + if ($classReflection->getName() === PHPUnitClassName::TEST_CASE) { |
| 113 | + return false; |
| 114 | + } |
| 115 | + |
| 116 | + // the result of a parent only depends on the hooks overridden below it, so test classes share the parent chain |
| 117 | + $overriddenHookMethodNames = array_unique($overriddenHookMethodNames); |
| 118 | + sort($overriddenHookMethodNames); |
| 119 | + $cacheKey = $classReflection->getName() . '|' . implode(',', $overriddenHookMethodNames); |
| 120 | + |
| 121 | + return $this->assertingByParentClassAndOverriddenHooks[$cacheKey] ??= $this->isClassLikeAsserting( |
| 122 | + $this->astResolver->resolveClassFromClassReflection($classReflection), |
| 123 | + $classReflection, |
| 124 | + $overriddenHookMethodNames |
| 125 | + ); |
| 126 | + } |
| 127 | + |
| 128 | + private function containsAssertCall(ClassMethod $classMethod): bool |
| 129 | + { |
| 130 | + // every hook gets its own nesting budget, a "parent::" hook call is evaluated again in the parent itself |
| 131 | + $this->assertCallAnalyzer->resetNesting(); |
| 132 | + |
| 133 | + return $this->assertCallAnalyzer->containsAssertCall($classMethod); |
| 134 | + } |
| 135 | + |
| 136 | + private function hasParentCall(ClassMethod $classMethod, string $hookMethodName): bool |
| 137 | + { |
| 138 | + return $this->betterNodeFinder->findFirst( |
| 139 | + (array) $classMethod->stmts, |
| 140 | + fn (Node $node): bool => $node instanceof StaticCall |
| 141 | + && $this->nodeNameResolver->isName($node->class, ObjectReference::PARENT) |
| 142 | + && $this->nodeNameResolver->isName($node->name, $hookMethodName) |
| 143 | + ) instanceof StaticCall; |
| 144 | + } |
| 145 | + |
| 146 | + private function resolveHookMethodName(ClassMethod $classMethod): ?string |
| 147 | + { |
| 148 | + $methodName = $classMethod->name->toString(); |
| 149 | + |
| 150 | + foreach (self::HOOK_METHOD_NAMES as $hookMethodName) { |
| 151 | + if (strcasecmp($hookMethodName, $methodName) === 0) { |
| 152 | + return $hookMethodName; |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + return null; |
| 157 | + } |
| 158 | + |
| 159 | + /** |
| 160 | + * @param string[] $overriddenHookMethodNames |
| 161 | + */ |
| 162 | + private function isRelevantHook( |
| 163 | + ClassMethod $classMethod, |
| 164 | + ?string $hookMethodName, |
| 165 | + array $overriddenHookMethodNames |
| 166 | + ): bool { |
| 167 | + if ($hookMethodName !== null && ! in_array($hookMethodName, $overriddenHookMethodNames, true)) { |
80 | 168 | return true; |
81 | 169 | } |
82 | 170 |
|
| 171 | + // methods marked as hooks run too, also when overridden |
83 | 172 | if ($classMethod->attrGroups !== [] |
84 | 173 | && $this->phpAttributeAnalyzer->hasPhpAttributes($classMethod, self::HOOK_ATTRIBUTES)) { |
85 | 174 | return true; |
|
0 commit comments