|
| 1 | +<?php |
| 2 | + |
| 3 | +declare (strict_types=1); |
| 4 | +namespace Rector\PHPUnit\PHPUnit100\Rector\Class_; |
| 5 | + |
| 6 | +use PhpParser\Modifiers; |
| 7 | +use PhpParser\Node; |
| 8 | +use PhpParser\Node\Arg; |
| 9 | +use PhpParser\Node\Expr\ClassConstFetch; |
| 10 | +use PhpParser\Node\Expr\StaticCall; |
| 11 | +use PhpParser\Node\Name; |
| 12 | +use PhpParser\Node\Stmt\Class_; |
| 13 | +use PhpParser\Node\Stmt\ClassMethod; |
| 14 | +use PhpParser\Node\Stmt\Expression; |
| 15 | +use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer; |
| 16 | +use Rector\Rector\AbstractRector; |
| 17 | +use Rector\ValueObject\MethodName; |
| 18 | +use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; |
| 19 | +use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; |
| 20 | +/** |
| 21 | + * @see https://github.com/sebastianbergmann/phpunit/issues/3975 |
| 22 | + * @see https://github.com/sebastianbergmann/phpunit/commit/705874f1b867fd99865e43cb5eaea4e6d141582f |
| 23 | + * |
| 24 | + * @see \Rector\PHPUnit\Tests\PHPUnit100\Rector\Class_\ParentTestClassConstructorRector\ParentTestClassConstructorRectorTest |
| 25 | + */ |
| 26 | +final class ParentTestClassConstructorRector extends AbstractRector |
| 27 | +{ |
| 28 | + /** |
| 29 | + * @readonly |
| 30 | + */ |
| 31 | + private TestsNodeAnalyzer $testsNodeAnalyzer; |
| 32 | + public function __construct(TestsNodeAnalyzer $testsNodeAnalyzer) |
| 33 | + { |
| 34 | + $this->testsNodeAnalyzer = $testsNodeAnalyzer; |
| 35 | + } |
| 36 | + public function getRuleDefinition() : RuleDefinition |
| 37 | + { |
| 38 | + return new RuleDefinition('PHPUnit\\Framework\\TestCase requires a parent constructor call', [new CodeSample(<<<'CODE_SAMPLE' |
| 39 | +use PHPUnit\Framework\TestCase; |
| 40 | +
|
| 41 | +final class SomeHelper extends TestCase |
| 42 | +{ |
| 43 | +} |
| 44 | +CODE_SAMPLE |
| 45 | +, <<<'CODE_SAMPLE' |
| 46 | +use PHPUnit\Framework\TestCase; |
| 47 | +
|
| 48 | +final class SomeHelper extends TestCase |
| 49 | +{ |
| 50 | + public function __construct() |
| 51 | + { |
| 52 | + parent::__construct(static::class); |
| 53 | + } |
| 54 | +} |
| 55 | +CODE_SAMPLE |
| 56 | +)]); |
| 57 | + } |
| 58 | + /** |
| 59 | + * @return array<class-string<Node>> |
| 60 | + */ |
| 61 | + public function getNodeTypes() : array |
| 62 | + { |
| 63 | + return [Class_::class]; |
| 64 | + } |
| 65 | + /** |
| 66 | + * @param Class_ $node |
| 67 | + */ |
| 68 | + public function refactor(Node $node) : ?Node |
| 69 | + { |
| 70 | + if (!$this->testsNodeAnalyzer->isInTestClass($node)) { |
| 71 | + return null; |
| 72 | + } |
| 73 | + if ($this->shouldSkipClass($node)) { |
| 74 | + return null; |
| 75 | + } |
| 76 | + // it already has a constructor, skip as it might require specific tweaking |
| 77 | + if ($node->getMethod(MethodName::CONSTRUCT)) { |
| 78 | + return null; |
| 79 | + } |
| 80 | + $constructorClassMethod = new ClassMethod(MethodName::CONSTRUCT); |
| 81 | + $constructorClassMethod->flags |= Modifiers::PUBLIC; |
| 82 | + $constructorClassMethod->stmts[] = new Expression($this->createParentConstructorCall()); |
| 83 | + $node->stmts = \array_merge([$constructorClassMethod], $node->stmts); |
| 84 | + return $node; |
| 85 | + } |
| 86 | + private function createParentConstructorCall() : StaticCall |
| 87 | + { |
| 88 | + $staticClassConstFetch = new ClassConstFetch(new Name('static'), 'class'); |
| 89 | + return new StaticCall(new Name('parent'), MethodName::CONSTRUCT, [new Arg($staticClassConstFetch)]); |
| 90 | + } |
| 91 | + private function shouldSkipClass(Class_ $class) : bool |
| 92 | + { |
| 93 | + if ($class->isAbstract()) { |
| 94 | + return \true; |
| 95 | + } |
| 96 | + if ($class->isAnonymous()) { |
| 97 | + return \true; |
| 98 | + } |
| 99 | + $className = $this->getName($class); |
| 100 | + // loaded automatically by PHPUnit |
| 101 | + if (\substr_compare((string) $className, 'Test', -\strlen('Test')) === 0) { |
| 102 | + return \true; |
| 103 | + } |
| 104 | + return \substr_compare((string) $className, 'TestCase', -\strlen('TestCase')) === 0; |
| 105 | + } |
| 106 | +} |
0 commit comments