|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Rector\PHPUnit\CodeQuality\Rector\ClassMethod; |
| 6 | + |
| 7 | +use PhpParser\Node; |
| 8 | +use PhpParser\Node\Stmt\ClassMethod; |
| 9 | +use Rector\Doctrine\NodeAnalyzer\AttributeFinder; |
| 10 | +use Rector\PHPUnit\CodeQuality\NodeAnalyser\ParentCallDetector; |
| 11 | +use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer; |
| 12 | +use Rector\Rector\AbstractRector; |
| 13 | +use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; |
| 14 | +use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; |
| 15 | + |
| 16 | +/** |
| 17 | + * @see \Rector\PHPUnit\Tests\CodeQuality\Rector\ClassMethod\NoSetupWithParentCallOverrideRector\NoSetupWithParentCallOverrideRectorTest |
| 18 | + */ |
| 19 | +final class NoSetupWithParentCallOverrideRector extends AbstractRector |
| 20 | +{ |
| 21 | + public function __construct( |
| 22 | + private readonly TestsNodeAnalyzer $testsNodeAnalyzer, |
| 23 | + private readonly ParentCallDetector $parentCallDetector, |
| 24 | + private readonly AttributeFinder $attributeFinder, |
| 25 | + ) { |
| 26 | + } |
| 27 | + |
| 28 | + public function getRuleDefinition(): RuleDefinition |
| 29 | + { |
| 30 | + return new RuleDefinition( |
| 31 | + 'Remove override, if setUp() references parent::setUp() call to improve readability', |
| 32 | + [ |
| 33 | + new CodeSample( |
| 34 | + <<<'CODE_SAMPLE' |
| 35 | +use PHPUnit\Framework\TestCase; |
| 36 | +
|
| 37 | +final class SomeTest extends TestCase |
| 38 | +{ |
| 39 | + #[\Override] |
| 40 | + protected function setUp(): void |
| 41 | + { |
| 42 | + parent::setUp(); |
| 43 | +
|
| 44 | + $value = 100; |
| 45 | + } |
| 46 | +} |
| 47 | +CODE_SAMPLE |
| 48 | + |
| 49 | + , |
| 50 | + <<<'CODE_SAMPLE' |
| 51 | +use PHPUnit\Framework\TestCase; |
| 52 | +
|
| 53 | +final class SomeTest extends TestCase |
| 54 | +{ |
| 55 | + protected function setUp(): void |
| 56 | + { |
| 57 | + parent::setUp(); |
| 58 | +
|
| 59 | + $value = 100; |
| 60 | + } |
| 61 | +} |
| 62 | +CODE_SAMPLE |
| 63 | + ), |
| 64 | + ] |
| 65 | + ); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * @return array<class-string<Node>> |
| 70 | + */ |
| 71 | + public function getNodeTypes(): array |
| 72 | + { |
| 73 | + return [ClassMethod::class]; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * @param ClassMethod $node |
| 78 | + */ |
| 79 | + public function refactor(Node $node): ?Node |
| 80 | + { |
| 81 | + if (! $this->testsNodeAnalyzer->isInTestClass($node)) { |
| 82 | + return null; |
| 83 | + } |
| 84 | + |
| 85 | + if (! $this->isName($node, 'setUp')) { |
| 86 | + return null; |
| 87 | + } |
| 88 | + |
| 89 | + if (! $this->parentCallDetector->hasParentCall($node)) { |
| 90 | + return null; |
| 91 | + } |
| 92 | + |
| 93 | + if (! $this->attributeFinder->hasAttributeByClasses($node, ['Override'])) { |
| 94 | + return null; |
| 95 | + } |
| 96 | + |
| 97 | + $hasChanged = false; |
| 98 | + |
| 99 | + foreach ($node->attrGroups as $attributeGroupKey => $attrGroup) { |
| 100 | + foreach ($attrGroup->attrs as $attributeKey => $attribute) { |
| 101 | + if (! $this->isName($attribute->name, 'Override')) { |
| 102 | + continue; |
| 103 | + } |
| 104 | + |
| 105 | + unset($attrGroup->attrs[$attributeKey]); |
| 106 | + $hasChanged = true; |
| 107 | + } |
| 108 | + |
| 109 | + if ($attrGroup->attrs === []) { |
| 110 | + unset($node->attrGroups[$attributeGroupKey]); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + if (! $hasChanged) { |
| 115 | + return null; |
| 116 | + } |
| 117 | + |
| 118 | + return $node; |
| 119 | + } |
| 120 | +} |
0 commit comments