|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Rector\PHPUnit\PHPUnit120\Rector\Class_; |
| 6 | + |
| 7 | +use PhpParser\Node; |
| 8 | +use PhpParser\Node\Stmt\Class_; |
| 9 | +use PhpParser\Node\Stmt\ClassMethod; |
| 10 | +use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer; |
| 11 | +use Rector\Rector\AbstractRector; |
| 12 | +use Rector\ValueObject\MethodName; |
| 13 | +use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; |
| 14 | +use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; |
| 15 | + |
| 16 | +/** |
| 17 | + * @see \Rector\PHPUnit\Tests\PHPUnit120\Rector\Class_\RemoveOverrideFinalConstructTestCaseRector\RemoveOverrideFinalConstructTestCaseRectorTest |
| 18 | + */ |
| 19 | +final class RemoveOverrideFinalConstructTestCaseRector extends AbstractRector |
| 20 | +{ |
| 21 | + public function __construct( |
| 22 | + private readonly TestsNodeAnalyzer $testsNodeAnalyzer, |
| 23 | + ) { |
| 24 | + } |
| 25 | + |
| 26 | + public function getRuleDefinition(): RuleDefinition |
| 27 | + { |
| 28 | + return new RuleDefinition( |
| 29 | + 'Remove override final construct test case', |
| 30 | + [ |
| 31 | + new CodeSample( |
| 32 | + <<<'CODE_SAMPLE' |
| 33 | + use PHPUnit\Framework\TestCase; |
| 34 | +
|
| 35 | + final class SomeClass extends TestCase |
| 36 | + { |
| 37 | + public function __construct() |
| 38 | + { |
| 39 | + parent::__construct(static::class); |
| 40 | + } |
| 41 | + } |
| 42 | + CODE_SAMPLE |
| 43 | + , |
| 44 | + <<<'CODE_SAMPLE' |
| 45 | + use PHPUnit\Framework\TestCase; |
| 46 | +
|
| 47 | + final class SomeClass extends TestCase |
| 48 | + { |
| 49 | + } |
| 50 | + CODE_SAMPLE |
| 51 | + , |
| 52 | + ), |
| 53 | + ], |
| 54 | + ); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * @return array<class-string<Node>> |
| 59 | + */ |
| 60 | + public function getNodeTypes(): array |
| 61 | + { |
| 62 | + return [Class_::class]; |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * @param Class_ $node |
| 67 | + */ |
| 68 | + public function refactor(Node $node): Node|null |
| 69 | + { |
| 70 | + if (! $this->testsNodeAnalyzer->isInTestClass($node)) { |
| 71 | + return null; |
| 72 | + } |
| 73 | + |
| 74 | + $constructClassMethod = $node->getMethod(MethodName::CONSTRUCT); |
| 75 | + |
| 76 | + if ($constructClassMethod instanceof ClassMethod) { |
| 77 | + foreach ($node->stmts as $key => $stmt) { |
| 78 | + if ($stmt instanceof ClassMethod && $this->isName($stmt, MethodName::CONSTRUCT)) { |
| 79 | + unset($node->stmts[$key]); |
| 80 | + |
| 81 | + $node->setAttribute('hasRemovedFinalConstruct', true); |
| 82 | + return $node; |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + return null; |
| 88 | + } |
| 89 | +} |
0 commit comments