|
5 | 5 | namespace Rector\PHPUnit\PHPUnit60\Rector\ClassMethod; |
6 | 6 |
|
7 | 7 | use PhpParser\Node; |
| 8 | +use PhpParser\Node\Expr\MethodCall; |
8 | 9 | use PhpParser\Node\Stmt\ClassMethod; |
| 10 | +use PhpParser\Node\Stmt\Expression; |
| 11 | +use PhpParser\Node\Stmt\Nop; |
| 12 | +use PhpParser\NodeVisitor; |
9 | 13 | use PHPStan\PhpDocParser\Ast\PhpDoc\GenericTagValueNode; |
10 | 14 | use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode; |
11 | 15 | use PHPStan\Reflection\ClassReflection; |
@@ -104,6 +108,8 @@ public function refactor(Node $node): ?Node |
104 | 108 | return null; |
105 | 109 | } |
106 | 110 |
|
| 111 | + $this->removeAddToAssertionCountCalls($node); |
| 112 | + |
107 | 113 | $phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node); |
108 | 114 | $phpDocInfo->addPhpDocTagNode(new PhpDocTagNode('@doesNotPerformAssertions', new GenericTagValueNode(''))); |
109 | 115 |
|
@@ -143,6 +149,49 @@ private function shouldSkipClassMethod(ClassMethod $classMethod): bool |
143 | 149 | return $this->mockedVariableAnalyzer->containsMockAsUsedVariable($classMethod); |
144 | 150 | } |
145 | 151 |
|
| 152 | + /** |
| 153 | + * The assertion count fakes an assertion, but the "@doesNotPerformAssertions" annotation makes it obsolete |
| 154 | + */ |
| 155 | + private function removeAddToAssertionCountCalls(ClassMethod $classMethod): void |
| 156 | + { |
| 157 | + $hasJustRemovedCall = false; |
| 158 | + |
| 159 | + $this->traverseNodesWithCallable($classMethod, function (Node $node) use (&$hasJustRemovedCall): ?int { |
| 160 | + // a comment on the same line as the removed call is parsed as a nop statement right behind it |
| 161 | + if ($hasJustRemovedCall && $node instanceof Nop) { |
| 162 | + return NodeVisitor::REMOVE_NODE; |
| 163 | + } |
| 164 | + |
| 165 | + $hasJustRemovedCall = false; |
| 166 | + |
| 167 | + if (! $this->isAddToAssertionCountExpression($node)) { |
| 168 | + return null; |
| 169 | + } |
| 170 | + |
| 171 | + $hasJustRemovedCall = true; |
| 172 | + |
| 173 | + return NodeVisitor::REMOVE_NODE; |
| 174 | + }); |
| 175 | + } |
| 176 | + |
| 177 | + private function isAddToAssertionCountExpression(Node $node): bool |
| 178 | + { |
| 179 | + if (! $node instanceof Expression) { |
| 180 | + return false; |
| 181 | + } |
| 182 | + |
| 183 | + if (! $node->expr instanceof MethodCall) { |
| 184 | + return false; |
| 185 | + } |
| 186 | + |
| 187 | + $methodCall = $node->expr; |
| 188 | + if (! $this->isName($methodCall->var, 'this')) { |
| 189 | + return false; |
| 190 | + } |
| 191 | + |
| 192 | + return $this->isName($methodCall->name, 'addToAssertionCount'); |
| 193 | + } |
| 194 | + |
146 | 195 | private function isInTwigIntegrationTestCase(ClassMethod $classMethod): bool |
147 | 196 | { |
148 | 197 | $classReflection = $this->reflectionResolver->resolveClassReflection($classMethod); |
|
0 commit comments