|
| 1 | +<?php |
| 2 | + |
| 3 | +declare (strict_types=1); |
| 4 | +namespace Rector\TypeDeclaration\Rector\Class_; |
| 5 | + |
| 6 | +use PhpParser\Node; |
| 7 | +use PhpParser\Node\Stmt\Class_; |
| 8 | +use PhpParser\Node\Stmt\ClassMethod; |
| 9 | +use PhpParser\Node\Stmt\Property; |
| 10 | +use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode; |
| 11 | +use PHPStan\Type\MixedType; |
| 12 | +use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo; |
| 13 | +use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory; |
| 14 | +use Rector\Comments\NodeDocBlock\DocBlockUpdater; |
| 15 | +use Rector\PHPStanStaticTypeMapper\Enum\TypeKind; |
| 16 | +use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer; |
| 17 | +use Rector\Rector\AbstractRector; |
| 18 | +use Rector\StaticTypeMapper\StaticTypeMapper; |
| 19 | +use Rector\TypeDeclaration\AlreadyAssignDetector\ConstructorAssignDetector; |
| 20 | +use Rector\ValueObject\MethodName; |
| 21 | +use Rector\ValueObject\PhpVersionFeature; |
| 22 | +use Rector\VersionBonding\Contract\MinPhpVersionInterface; |
| 23 | +use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; |
| 24 | +use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; |
| 25 | +/** |
| 26 | + * @see \Rector\Tests\TypeDeclaration\Rector\Class_\TypedPropertyFromDocblockSetUpDefinedRector\TypedPropertyFromDocblockSetUpDefinedRectorTest |
| 27 | + */ |
| 28 | +final class TypedPropertyFromDocblockSetUpDefinedRector extends AbstractRector implements MinPhpVersionInterface |
| 29 | +{ |
| 30 | + /** |
| 31 | + * @readonly |
| 32 | + */ |
| 33 | + private TestsNodeAnalyzer $testsNodeAnalyzer; |
| 34 | + /** |
| 35 | + * @readonly |
| 36 | + */ |
| 37 | + private ConstructorAssignDetector $constructorAssignDetector; |
| 38 | + /** |
| 39 | + * @readonly |
| 40 | + */ |
| 41 | + private PhpDocInfoFactory $phpDocInfoFactory; |
| 42 | + /** |
| 43 | + * @readonly |
| 44 | + */ |
| 45 | + private StaticTypeMapper $staticTypeMapper; |
| 46 | + /** |
| 47 | + * @readonly |
| 48 | + */ |
| 49 | + private DocBlockUpdater $docBlockUpdater; |
| 50 | + public function __construct(TestsNodeAnalyzer $testsNodeAnalyzer, ConstructorAssignDetector $constructorAssignDetector, PhpDocInfoFactory $phpDocInfoFactory, StaticTypeMapper $staticTypeMapper, DocBlockUpdater $docBlockUpdater) |
| 51 | + { |
| 52 | + $this->testsNodeAnalyzer = $testsNodeAnalyzer; |
| 53 | + $this->constructorAssignDetector = $constructorAssignDetector; |
| 54 | + $this->phpDocInfoFactory = $phpDocInfoFactory; |
| 55 | + $this->staticTypeMapper = $staticTypeMapper; |
| 56 | + $this->docBlockUpdater = $docBlockUpdater; |
| 57 | + } |
| 58 | + public function getRuleDefinition() : RuleDefinition |
| 59 | + { |
| 60 | + return new RuleDefinition('Add property type in PHPUnit test from docblock, if defined in setUp() method', [new CodeSample(<<<'CODE_SAMPLE' |
| 61 | +use PHPUnit\Framework\TestCase; |
| 62 | +
|
| 63 | +class SomeClass extends TestCase |
| 64 | +{ |
| 65 | + /** |
| 66 | + * @var \Doctrine\ORM\EntityManagerInterface |
| 67 | + */ |
| 68 | + private $doctrine; |
| 69 | +
|
| 70 | + protected function setUp(): void |
| 71 | + { |
| 72 | + $this->doctrine = $this->container('doctrine.orm.entity_manager'); |
| 73 | + } |
| 74 | +} |
| 75 | +CODE_SAMPLE |
| 76 | +, <<<'CODE_SAMPLE' |
| 77 | +use PHPUnit\Framework\TestCase; |
| 78 | +
|
| 79 | +class SomeClass extends TestCase |
| 80 | +{ |
| 81 | + private \Doctrine\ORM\EntityManagerInterface $doctrine; |
| 82 | +
|
| 83 | + protected function setUp(): void |
| 84 | + { |
| 85 | + $this->doctrine = $this->container('doctrine.orm.entity_manager'); |
| 86 | + } |
| 87 | +} |
| 88 | +CODE_SAMPLE |
| 89 | +)]); |
| 90 | + } |
| 91 | + /** |
| 92 | + * @return array<class-string<Node>> |
| 93 | + */ |
| 94 | + public function getNodeTypes() : array |
| 95 | + { |
| 96 | + return [Class_::class]; |
| 97 | + } |
| 98 | + /** |
| 99 | + * @param Class_ $node |
| 100 | + */ |
| 101 | + public function refactor(Node $node) : ?Node |
| 102 | + { |
| 103 | + if (!$this->testsNodeAnalyzer->isInTestClass($node)) { |
| 104 | + return null; |
| 105 | + } |
| 106 | + // nothing useful here |
| 107 | + $setUpClassMethod = $node->getMethod(MethodName::SET_UP); |
| 108 | + if (!$setUpClassMethod instanceof ClassMethod) { |
| 109 | + return null; |
| 110 | + } |
| 111 | + $hasChanged = \false; |
| 112 | + foreach ($node->getProperties() as $property) { |
| 113 | + // already known type |
| 114 | + if ($property->type instanceof Node) { |
| 115 | + continue; |
| 116 | + } |
| 117 | + // some magic might be going on |
| 118 | + if ($property->isStatic()) { |
| 119 | + continue; |
| 120 | + } |
| 121 | + if (!$property->isPrivate()) { |
| 122 | + continue; |
| 123 | + } |
| 124 | + // exactly one property |
| 125 | + if (\count($property->props) !== 1) { |
| 126 | + continue; |
| 127 | + } |
| 128 | + $propertyName = $property->props[0]->name->toString(); |
| 129 | + if (!$this->constructorAssignDetector->isPropertyAssigned($node, $propertyName)) { |
| 130 | + continue; |
| 131 | + } |
| 132 | + $propertyPhpDocInfo = $this->phpDocInfoFactory->createFromNode($property); |
| 133 | + if (!$propertyPhpDocInfo instanceof PhpDocInfo) { |
| 134 | + continue; |
| 135 | + } |
| 136 | + $varType = $propertyPhpDocInfo->getVarType(); |
| 137 | + if ($varType instanceof MixedType) { |
| 138 | + continue; |
| 139 | + } |
| 140 | + $nativePropertyTypeNode = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($varType, TypeKind::PROPERTY); |
| 141 | + if (!$nativePropertyTypeNode instanceof Node) { |
| 142 | + continue; |
| 143 | + } |
| 144 | + // remove var tag |
| 145 | + $this->removeVarTag($propertyPhpDocInfo, $property); |
| 146 | + $property->type = $nativePropertyTypeNode; |
| 147 | + $hasChanged = \true; |
| 148 | + } |
| 149 | + if ($hasChanged) { |
| 150 | + return $node; |
| 151 | + } |
| 152 | + return null; |
| 153 | + } |
| 154 | + public function provideMinPhpVersion() : int |
| 155 | + { |
| 156 | + return PhpVersionFeature::TYPED_PROPERTIES; |
| 157 | + } |
| 158 | + private function removeVarTag(PhpDocInfo $propertyPhpDocInfo, Property $property) : void |
| 159 | + { |
| 160 | + $propertyPhpDocInfo->removeByType(VarTagValueNode::class); |
| 161 | + $this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($property); |
| 162 | + } |
| 163 | +} |
0 commit comments