|
| 1 | +<?php |
| 2 | + |
| 3 | +declare (strict_types=1); |
| 4 | +namespace Rector\Doctrine\CodeQuality\Rector\Class_; |
| 5 | + |
| 6 | +use PhpParser\Node; |
| 7 | +use PhpParser\Node\Expr; |
| 8 | +use PhpParser\Node\Expr\ConstFetch; |
| 9 | +use PhpParser\Node\Name; |
| 10 | +use PhpParser\Node\Stmt\Class_; |
| 11 | +use PhpParser\Node\Stmt\ClassMethod; |
| 12 | +use PhpParser\Node\Stmt\Property; |
| 13 | +use PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode; |
| 14 | +use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode; |
| 15 | +use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode; |
| 16 | +use PHPStan\Type\MixedType; |
| 17 | +use PHPStan\Type\Type; |
| 18 | +use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo; |
| 19 | +use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory; |
| 20 | +use Rector\Comments\NodeDocBlock\DocBlockUpdater; |
| 21 | +use Rector\DeadCode\PhpDoc\DeadVarTagValueNodeAnalyzer; |
| 22 | +use Rector\Doctrine\CodeQuality\Helper\SetterGetterFinder; |
| 23 | +use Rector\Doctrine\NodeAnalyzer\DoctrineEntityDetector; |
| 24 | +use Rector\Doctrine\NodeManipulator\ColumnPropertyTypeResolver; |
| 25 | +use Rector\PHPStanStaticTypeMapper\Enum\TypeKind; |
| 26 | +use Rector\Rector\AbstractRector; |
| 27 | +use Rector\StaticTypeMapper\StaticTypeMapper; |
| 28 | +use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; |
| 29 | +use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; |
| 30 | +/** |
| 31 | + * @see \Rector\Doctrine\Tests\CodeQuality\Rector\Class_\TypeNullableEntityFromDocblockRector\TypeNullableEntityFromDocblockRectorTest |
| 32 | + */ |
| 33 | +final class TypeNullableEntityFromDocblockRector extends AbstractRector |
| 34 | +{ |
| 35 | + /** |
| 36 | + * @readonly |
| 37 | + */ |
| 38 | + private ColumnPropertyTypeResolver $columnPropertyTypeResolver; |
| 39 | + /** |
| 40 | + * @readonly |
| 41 | + */ |
| 42 | + private StaticTypeMapper $staticTypeMapper; |
| 43 | + /** |
| 44 | + * @readonly |
| 45 | + */ |
| 46 | + private DeadVarTagValueNodeAnalyzer $deadVarTagValueNodeAnalyzer; |
| 47 | + /** |
| 48 | + * @readonly |
| 49 | + */ |
| 50 | + private PhpDocInfoFactory $phpDocInfoFactory; |
| 51 | + /** |
| 52 | + * @readonly |
| 53 | + */ |
| 54 | + private DocBlockUpdater $docBlockUpdater; |
| 55 | + /** |
| 56 | + * @readonly |
| 57 | + */ |
| 58 | + private SetterGetterFinder $setterGetterFinder; |
| 59 | + /** |
| 60 | + * @readonly |
| 61 | + */ |
| 62 | + private DoctrineEntityDetector $doctrineEntityDetector; |
| 63 | + public function __construct(ColumnPropertyTypeResolver $columnPropertyTypeResolver, StaticTypeMapper $staticTypeMapper, DeadVarTagValueNodeAnalyzer $deadVarTagValueNodeAnalyzer, PhpDocInfoFactory $phpDocInfoFactory, DocBlockUpdater $docBlockUpdater, SetterGetterFinder $setterGetterFinder, DoctrineEntityDetector $doctrineEntityDetector) |
| 64 | + { |
| 65 | + $this->columnPropertyTypeResolver = $columnPropertyTypeResolver; |
| 66 | + $this->staticTypeMapper = $staticTypeMapper; |
| 67 | + $this->deadVarTagValueNodeAnalyzer = $deadVarTagValueNodeAnalyzer; |
| 68 | + $this->phpDocInfoFactory = $phpDocInfoFactory; |
| 69 | + $this->docBlockUpdater = $docBlockUpdater; |
| 70 | + $this->setterGetterFinder = $setterGetterFinder; |
| 71 | + $this->doctrineEntityDetector = $doctrineEntityDetector; |
| 72 | + } |
| 73 | + public function getRuleDefinition() : RuleDefinition |
| 74 | + { |
| 75 | + return new RuleDefinition('Add full nullable type coverage for Doctrine entity based on docblocks. Useful stepping stone to add type coverage while keeping entities safe to read and write getter/setters', [new CodeSample(<<<'CODE_SAMPLE' |
| 76 | +use Doctrine\ORM\Mapping as ORM; |
| 77 | +
|
| 78 | +/** |
| 79 | + * @ORM\Entity() |
| 80 | + */ |
| 81 | +class SomeEntity |
| 82 | +{ |
| 83 | + /** |
| 84 | + * @var string |
| 85 | + * @ORM\Id |
| 86 | + * @ORM\Column(type="string") |
| 87 | + * @ORM\GeneratedValue |
| 88 | + */ |
| 89 | + private $name; |
| 90 | +
|
| 91 | +
|
| 92 | + /** |
| 93 | + * @return string |
| 94 | + */ |
| 95 | + public function getName() |
| 96 | + { |
| 97 | + return $this->name; |
| 98 | + } |
| 99 | +
|
| 100 | + /** |
| 101 | + * @param string $name |
| 102 | + */ |
| 103 | + public function setName($name): void |
| 104 | + { |
| 105 | + $this->name = $name; |
| 106 | + } |
| 107 | +} |
| 108 | +CODE_SAMPLE |
| 109 | +, <<<'CODE_SAMPLE' |
| 110 | +use Doctrine\ORM\Mapping as ORM; |
| 111 | +
|
| 112 | +/** |
| 113 | + * @ORM\Entity() |
| 114 | + */ |
| 115 | +class SomeEntity |
| 116 | +{ |
| 117 | + /** |
| 118 | + * @ORM\Id |
| 119 | + * @ORM\Column(type="string") |
| 120 | + * @ORM\GeneratedValue |
| 121 | + */ |
| 122 | + private ?string $name = null; |
| 123 | +
|
| 124 | + public function getName(): ?string |
| 125 | + { |
| 126 | + return $this->name; |
| 127 | + } |
| 128 | +
|
| 129 | + public function setName(?string $name): void |
| 130 | + { |
| 131 | + $this->name = $name; |
| 132 | + } |
| 133 | +} |
| 134 | +CODE_SAMPLE |
| 135 | +)]); |
| 136 | + } |
| 137 | + public function getNodeTypes() : array |
| 138 | + { |
| 139 | + return [Class_::class]; |
| 140 | + } |
| 141 | + /** |
| 142 | + * @param Class_ $node |
| 143 | + */ |
| 144 | + public function refactor(Node $node) : ?Class_ |
| 145 | + { |
| 146 | + if (!$this->doctrineEntityDetector->detect($node)) { |
| 147 | + return null; |
| 148 | + } |
| 149 | + $hasChanged = \false; |
| 150 | + foreach ($node->getProperties() as $property) { |
| 151 | + // property is already typed, skip |
| 152 | + if ($property->type instanceof Node) { |
| 153 | + continue; |
| 154 | + } |
| 155 | + $propertyType = $this->columnPropertyTypeResolver->resolve($property, \true); |
| 156 | + if (!$propertyType instanceof Type || $propertyType instanceof MixedType) { |
| 157 | + continue; |
| 158 | + } |
| 159 | + $propertyTypeNode = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($propertyType, TypeKind::PROPERTY); |
| 160 | + if (!$propertyTypeNode instanceof Node) { |
| 161 | + continue; |
| 162 | + } |
| 163 | + $property->type = $propertyTypeNode; |
| 164 | + $propertyItem = $property->props[0]; |
| 165 | + // set default value to null if nullable |
| 166 | + if (!$propertyItem->default instanceof Expr) { |
| 167 | + $propertyItem->default = new ConstFetch(new Name('null')); |
| 168 | + } |
| 169 | + $hasChanged = \true; |
| 170 | + $this->removeVarTagIfNotUseful($property); |
| 171 | + $propertyName = $this->getName($property); |
| 172 | + $this->decorateGetterClassMethodReturnType($node, $propertyName, $propertyType); |
| 173 | + $this->decorateSetterClassMethodParameterType($node, $propertyName, $propertyType); |
| 174 | + } |
| 175 | + if ($hasChanged) { |
| 176 | + return $node; |
| 177 | + } |
| 178 | + return null; |
| 179 | + } |
| 180 | + private function removeVarTagIfNotUseful(Property $property) : void |
| 181 | + { |
| 182 | + // remove @var docblock if not useful |
| 183 | + $propertyPhpDocInfo = $this->phpDocInfoFactory->createFromNode($property); |
| 184 | + if (!$propertyPhpDocInfo instanceof PhpDocInfo) { |
| 185 | + return; |
| 186 | + } |
| 187 | + $propertyVarTagValueNode = $propertyPhpDocInfo->getVarTagValueNode(); |
| 188 | + if (!$propertyVarTagValueNode instanceof VarTagValueNode) { |
| 189 | + return; |
| 190 | + } |
| 191 | + if (!$this->deadVarTagValueNodeAnalyzer->isDead($propertyVarTagValueNode, $property)) { |
| 192 | + return; |
| 193 | + } |
| 194 | + $propertyPhpDocInfo->removeByType(VarTagValueNode::class); |
| 195 | + $this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($property); |
| 196 | + } |
| 197 | + private function decorateGetterClassMethodReturnType(Class_ $class, string $propertyName, Type $propertyType) : void |
| 198 | + { |
| 199 | + $getterClassMethod = $this->setterGetterFinder->findGetterClassMethod($class, $propertyName); |
| 200 | + if (!$getterClassMethod instanceof ClassMethod) { |
| 201 | + return; |
| 202 | + } |
| 203 | + // already known type |
| 204 | + if ($getterClassMethod->returnType instanceof Node) { |
| 205 | + return; |
| 206 | + } |
| 207 | + $returnTypeNode = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($propertyType, TypeKind::RETURN); |
| 208 | + // no type to fill |
| 209 | + if (!$returnTypeNode instanceof Node) { |
| 210 | + return; |
| 211 | + } |
| 212 | + $getterClassMethod->returnType = $returnTypeNode; |
| 213 | + $this->removeReturnDocblock($getterClassMethod); |
| 214 | + } |
| 215 | + private function decorateSetterClassMethodParameterType(Class_ $class, string $propertyName, Type $propertyType) : void |
| 216 | + { |
| 217 | + $setterClassMethod = $this->setterGetterFinder->findSetterClassMethod($class, $propertyName); |
| 218 | + if (!$setterClassMethod instanceof ClassMethod) { |
| 219 | + return; |
| 220 | + } |
| 221 | + if (\count($setterClassMethod->params) !== 1) { |
| 222 | + return; |
| 223 | + } |
| 224 | + $soleParam = $setterClassMethod->params[0]; |
| 225 | + // already known type |
| 226 | + if ($soleParam->type instanceof Node) { |
| 227 | + return; |
| 228 | + } |
| 229 | + $parameterTypeNode = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($propertyType, TypeKind::PARAM); |
| 230 | + // no type to fill |
| 231 | + if (!$parameterTypeNode instanceof Node) { |
| 232 | + return; |
| 233 | + } |
| 234 | + $soleParam->type = $parameterTypeNode; |
| 235 | + $this->removeParamDocblock($setterClassMethod); |
| 236 | + } |
| 237 | + private function removeParamDocblock(ClassMethod $classMethod) : void |
| 238 | + { |
| 239 | + $classMethodPhpDocInfo = $this->phpDocInfoFactory->createFromNode($classMethod); |
| 240 | + if (!$classMethodPhpDocInfo instanceof PhpDocInfo) { |
| 241 | + return; |
| 242 | + } |
| 243 | + if ($classMethodPhpDocInfo->getParamTagValueNodes() === []) { |
| 244 | + return; |
| 245 | + } |
| 246 | + $classMethodPhpDocInfo->removeByType(ParamTagValueNode::class); |
| 247 | + $this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($classMethod); |
| 248 | + } |
| 249 | + private function removeReturnDocblock(ClassMethod $classMethod) : void |
| 250 | + { |
| 251 | + $classMethodPhpDocInfo = $this->phpDocInfoFactory->createFromNode($classMethod); |
| 252 | + if (!$classMethodPhpDocInfo instanceof PhpDocInfo) { |
| 253 | + return; |
| 254 | + } |
| 255 | + if (!$classMethodPhpDocInfo->getReturnTagValue() instanceof ReturnTagValueNode) { |
| 256 | + return; |
| 257 | + } |
| 258 | + $classMethodPhpDocInfo->removeByType(ReturnTagValueNode::class); |
| 259 | + $this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($classMethod); |
| 260 | + } |
| 261 | +} |
0 commit comments