|
15 | 15 | use Rector\BetterPhpDocParser\PhpDoc\ArrayItemNode; |
16 | 16 | use Rector\BetterPhpDocParser\PhpDoc\DoctrineAnnotationTagValueNode; |
17 | 17 | use Rector\BetterPhpDocParser\ValueObject\PhpDoc\DoctrineAnnotation\CurlyListNode; |
| 18 | +use Rector\Core\Exception\ShouldNotHappenException; |
18 | 19 | use Rector\Php80\ValueObject\NestedAnnotationToAttribute; |
19 | 20 | use Rector\PhpAttribute\AnnotationToAttributeMapper; |
20 | 21 | use Rector\PhpAttribute\AttributeArrayNameInliner; |
@@ -71,40 +72,55 @@ public function createNested( |
71 | 72 | ): array { |
72 | 73 | $attributeGroups = []; |
73 | 74 |
|
74 | | -// $values = $doctrineAnnotationTagValueNode->getValues(); |
| 75 | + if ($this->isExplicitProperties($nestedAnnotationToAttribute)) { |
| 76 | + return $this->createFromExplicitProperties( |
| 77 | + $nestedAnnotationToAttribute, |
| 78 | + $doctrineAnnotationTagValueNode |
| 79 | + ); |
| 80 | + } |
75 | 81 |
|
76 | | - foreach ($nestedAnnotationToAttribute->getAnnotationPropertiesToAttributeClasses() as $itemName => $nestedAttributeClass) { |
77 | | - $nestedArrayItemNode = $doctrineAnnotationTagValueNode->getValue($itemName); //] ?? null; |
78 | | - if (! $nestedArrayItemNode instanceof ArrayItemNode) { |
| 82 | + $nestedAttributeClass = $nestedAnnotationToAttribute->getAnnotationPropertiesToAttributeClasses()[0]; |
| 83 | + |
| 84 | + foreach ($doctrineAnnotationTagValueNode->values as $arrayItemNode) { |
| 85 | + $nestedDoctrineAnnotationTagValueNode = $arrayItemNode->value; |
| 86 | + if (! $nestedDoctrineAnnotationTagValueNode instanceof CurlyListNode) { |
79 | 87 | continue; |
80 | 88 | } |
81 | 89 |
|
82 | | - if ($nestedArrayItemNode->value instanceof CurlyListNode) { |
83 | | - foreach ($nestedArrayItemNode->value->getValues() as $arrayItemNode) { |
84 | | - $nestedDoctrineAnnotationTagValueNode = $arrayItemNode->value; |
85 | | - if (! $nestedDoctrineAnnotationTagValueNode instanceof DoctrineAnnotationTagValueNode) { |
86 | | - continue; |
87 | | - } |
88 | | - |
89 | | - $args = $this->createArgsFromItems( |
90 | | - $nestedDoctrineAnnotationTagValueNode->getValues(), |
91 | | - $nestedAnnotationToAttribute |
92 | | - ); |
| 90 | + foreach ($nestedDoctrineAnnotationTagValueNode->values as $nestedArrayItemNode) { |
| 91 | + if (! $nestedArrayItemNode->value instanceof DoctrineAnnotationTagValueNode) { |
| 92 | + continue; |
| 93 | + } |
93 | 94 |
|
94 | | - $args = $this->attributeArrayNameInliner->inlineArrayToArgs($args); |
95 | | - $originalIdentifier = $nestedDoctrineAnnotationTagValueNode->identifierTypeNode->name; |
| 95 | + $attributeArgs = $this->createAttributeArgs($nestedArrayItemNode->value, $nestedAnnotationToAttribute); |
96 | 96 |
|
97 | | - $attributeName = $this->resolveAliasedAttributeName($originalIdentifier, $nestedAttributeClass); |
| 97 | + $originalIdentifier = $doctrineAnnotationTagValueNode->identifierTypeNode->name; |
| 98 | + $attributeName = $this->resolveAliasedAttributeName($originalIdentifier, $nestedAttributeClass); |
98 | 99 |
|
99 | | - $attribute = new Attribute($attributeName, $args); |
100 | | - $attributeGroups[] = new AttributeGroup([$attribute]); |
101 | | - } |
| 100 | + $attribute = new Attribute($attributeName, $attributeArgs); |
| 101 | + $attributeGroups[] = new AttributeGroup([$attribute]); |
102 | 102 | } |
103 | 103 | } |
104 | 104 |
|
105 | 105 | return $attributeGroups; |
106 | 106 | } |
107 | 107 |
|
| 108 | + /** |
| 109 | + * @return Arg[] |
| 110 | + */ |
| 111 | + public function createAttributeArgs( |
| 112 | + DoctrineAnnotationTagValueNode $nestedDoctrineAnnotationTagValueNode, |
| 113 | + NestedAnnotationToAttribute $nestedAnnotationToAttribute |
| 114 | + ): array { |
| 115 | + $args = $this->createArgsFromItems( |
| 116 | + $nestedDoctrineAnnotationTagValueNode->getValues(), |
| 117 | + $nestedAnnotationToAttribute |
| 118 | + ); |
| 119 | + |
| 120 | + $attributeArgs = $this->attributeArrayNameInliner->inlineArrayToArgs($args); |
| 121 | + return $attributeArgs; |
| 122 | + } |
| 123 | + |
108 | 124 | /** |
109 | 125 | * @param ArrayItemNode[] $arrayItemNodes |
110 | 126 | * @return Arg[] |
@@ -170,4 +186,54 @@ private function removeItems( |
170 | 186 |
|
171 | 187 | return $arrayItemNodes; |
172 | 188 | } |
| 189 | + |
| 190 | + private function isExplicitProperties(NestedAnnotationToAttribute $nestedAnnotationToAttribute): bool |
| 191 | + { |
| 192 | + foreach ($nestedAnnotationToAttribute->getAnnotationPropertiesToAttributeClasses() as $itemName => $attributeClass) { |
| 193 | + if (is_string($itemName)) { |
| 194 | + return true; |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + return false; |
| 199 | + } |
| 200 | + |
| 201 | + /** |
| 202 | + * @return AttributeGroup[] |
| 203 | + */ |
| 204 | + private function createFromExplicitProperties( |
| 205 | + NestedAnnotationToAttribute $nestedAnnotationToAttribute, |
| 206 | + DoctrineAnnotationTagValueNode $doctrineAnnotationTagValueNode |
| 207 | + ): array { |
| 208 | + $attributeGroups = []; |
| 209 | + |
| 210 | + foreach ($nestedAnnotationToAttribute->getAnnotationPropertiesToAttributeClasses() as $itemName => $nestedAttributeClass) { |
| 211 | + $nestedArrayItemNode = $doctrineAnnotationTagValueNode->getValue($itemName); |
| 212 | + if (! $nestedArrayItemNode instanceof ArrayItemNode) { |
| 213 | + continue; |
| 214 | + } |
| 215 | + |
| 216 | + if ($nestedArrayItemNode->value instanceof CurlyListNode) { |
| 217 | + foreach ($nestedArrayItemNode->value->getValues() as $arrayItemNode) { |
| 218 | + $nestedDoctrineAnnotationTagValueNode = $arrayItemNode->value; |
| 219 | + if (! $nestedDoctrineAnnotationTagValueNode instanceof DoctrineAnnotationTagValueNode) { |
| 220 | + throw new ShouldNotHappenException(); |
| 221 | + } |
| 222 | + |
| 223 | + $attributeArgs = $this->createAttributeArgs( |
| 224 | + $nestedDoctrineAnnotationTagValueNode, |
| 225 | + $nestedAnnotationToAttribute |
| 226 | + ); |
| 227 | + $originalIdentifier = $nestedDoctrineAnnotationTagValueNode->identifierTypeNode->name; |
| 228 | + |
| 229 | + $attributeName = $this->resolveAliasedAttributeName($originalIdentifier, $nestedAttributeClass); |
| 230 | + |
| 231 | + $attribute = new Attribute($attributeName, $attributeArgs); |
| 232 | + $attributeGroups[] = new AttributeGroup([$attribute]); |
| 233 | + } |
| 234 | + } |
| 235 | + } |
| 236 | + |
| 237 | + return $attributeGroups; |
| 238 | + } |
173 | 239 | } |
0 commit comments