|
| 1 | +<?php |
| 2 | + |
| 3 | +declare (strict_types=1); |
| 4 | +namespace Rector\TypeDeclaration\Rector\Class_; |
| 5 | + |
| 6 | +use PhpParser\Node; |
| 7 | +use PhpParser\Node\Expr; |
| 8 | +use PhpParser\Node\Name\FullyQualified; |
| 9 | +use PhpParser\Node\NullableType; |
| 10 | +use PhpParser\Node\Stmt\Class_; |
| 11 | +use PhpParser\Node\Stmt\Property; |
| 12 | +use PHPStan\Reflection\ClassReflection; |
| 13 | +use Rector\Php74\Guard\MakePropertyTypedGuard; |
| 14 | +use Rector\PHPStan\ScopeFetcher; |
| 15 | +use Rector\Rector\AbstractRector; |
| 16 | +use Rector\TypeDeclaration\NodeAnalyzer\JMSTypeAnalyzer; |
| 17 | +use Rector\TypeDeclaration\NodeFactory\JMSTypePropertyTypeFactory; |
| 18 | +use Rector\ValueObject\PhpVersionFeature; |
| 19 | +use Rector\VersionBonding\Contract\MinPhpVersionInterface; |
| 20 | +use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; |
| 21 | +use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; |
| 22 | +/** |
| 23 | + * @see \Rector\Tests\TypeDeclaration\Rector\Class_\ObjectTypedPropertyFromJMSSerializerAttributeTypeRector\ObjectTypedPropertyFromJMSSerializerAttributeTypeRectorTest |
| 24 | + */ |
| 25 | +final class ObjectTypedPropertyFromJMSSerializerAttributeTypeRector extends AbstractRector implements MinPhpVersionInterface |
| 26 | +{ |
| 27 | + /** |
| 28 | + * @readonly |
| 29 | + */ |
| 30 | + private MakePropertyTypedGuard $makePropertyTypedGuard; |
| 31 | + /** |
| 32 | + * @readonly |
| 33 | + */ |
| 34 | + private JMSTypeAnalyzer $jmsTypeAnalyzer; |
| 35 | + /** |
| 36 | + * @readonly |
| 37 | + */ |
| 38 | + private JMSTypePropertyTypeFactory $jmsTypePropertyTypeFactory; |
| 39 | + public function __construct(MakePropertyTypedGuard $makePropertyTypedGuard, JMSTypeAnalyzer $jmsTypeAnalyzer, JMSTypePropertyTypeFactory $jmsTypePropertyTypeFactory) |
| 40 | + { |
| 41 | + $this->makePropertyTypedGuard = $makePropertyTypedGuard; |
| 42 | + $this->jmsTypeAnalyzer = $jmsTypeAnalyzer; |
| 43 | + $this->jmsTypePropertyTypeFactory = $jmsTypePropertyTypeFactory; |
| 44 | + } |
| 45 | + public function getRuleDefinition(): RuleDefinition |
| 46 | + { |
| 47 | + return new RuleDefinition('Add object typed property from JMS Serializer Type attribute', [new CodeSample(<<<'CODE_SAMPLE' |
| 48 | +use JMS\Serializer\Annotation\Type; |
| 49 | +
|
| 50 | +final class SomeClass |
| 51 | +{ |
| 52 | + #[Type(Product::class)] |
| 53 | + private $product; |
| 54 | +} |
| 55 | +CODE_SAMPLE |
| 56 | +, <<<'CODE_SAMPLE' |
| 57 | +use JMS\Serializer\Annotation\Type; |
| 58 | +
|
| 59 | +final class SomeClass |
| 60 | +{ |
| 61 | + #[Type(Product::class)] |
| 62 | + private ?Product $product = null; |
| 63 | +} |
| 64 | +CODE_SAMPLE |
| 65 | +)]); |
| 66 | + } |
| 67 | + /** |
| 68 | + * @return array<class-string<Node>> |
| 69 | + */ |
| 70 | + public function getNodeTypes(): array |
| 71 | + { |
| 72 | + return [Class_::class]; |
| 73 | + } |
| 74 | + public function provideMinPhpVersion(): int |
| 75 | + { |
| 76 | + return PhpVersionFeature::ATTRIBUTES; |
| 77 | + } |
| 78 | + /** |
| 79 | + * @param Class_ $node |
| 80 | + */ |
| 81 | + public function refactor(Node $node): ?Node |
| 82 | + { |
| 83 | + if (!$this->jmsTypeAnalyzer->hasAtLeastOneUntypedPropertyUsingJmsAttribute($node)) { |
| 84 | + return null; |
| 85 | + } |
| 86 | + $scope = ScopeFetcher::fetch($node); |
| 87 | + $classReflection = $scope->getClassReflection(); |
| 88 | + if (!$classReflection instanceof ClassReflection) { |
| 89 | + return null; |
| 90 | + } |
| 91 | + $hasChanged = \false; |
| 92 | + foreach ($node->getProperties() as $property) { |
| 93 | + if ($this->shouldSkipProperty($property, $classReflection)) { |
| 94 | + continue; |
| 95 | + } |
| 96 | + $typeValue = $this->jmsTypeAnalyzer->resolveTypeAttributeValue($property); |
| 97 | + if (!is_string($typeValue)) { |
| 98 | + continue; |
| 99 | + } |
| 100 | + $propertyTypeNode = $this->jmsTypePropertyTypeFactory->createObjectTypeNode($typeValue); |
| 101 | + if (!$propertyTypeNode instanceof FullyQualified) { |
| 102 | + continue; |
| 103 | + } |
| 104 | + $property->type = new NullableType($propertyTypeNode); |
| 105 | + $property->props[0]->default = $this->nodeFactory->createNull(); |
| 106 | + $hasChanged = \true; |
| 107 | + } |
| 108 | + if ($hasChanged) { |
| 109 | + return $node; |
| 110 | + } |
| 111 | + return null; |
| 112 | + } |
| 113 | + private function shouldSkipProperty(Property $property, ClassReflection $classReflection): bool |
| 114 | + { |
| 115 | + if ($property->type instanceof Node || $property->props[0]->default instanceof Expr) { |
| 116 | + return \true; |
| 117 | + } |
| 118 | + if (!$this->jmsTypeAnalyzer->hasPropertyJMSTypeAttribute($property)) { |
| 119 | + return \true; |
| 120 | + } |
| 121 | + return !$this->makePropertyTypedGuard->isLegal($property, $classReflection); |
| 122 | + } |
| 123 | +} |
0 commit comments