Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 863eb33

Browse files
committed
wip
1 parent 3d8df36 commit 863eb33

5 files changed

Lines changed: 121 additions & 37 deletions

File tree

packages/BetterPhpDocParser/ValueObject/PhpDoc/DoctrineAnnotation/AbstractValuesAwareNode.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ public function getValuesWithSilentKey(): array
6767

6868
$silentKeyAwareValues = $this->values;
6969

70-
foreach ($silentKeyAwareValues as $value) {
71-
if ($value->key === 0) {
70+
foreach ($silentKeyAwareValues as $key => $value) {
71+
if ($value->key === null) {
7272
$value->key = $this->silentKey;
7373
break;
7474
}

packages/PhpAttribute/NodeFactory/PhpAttributeGroupFactory.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ public function create(
6464
array $uses
6565
): AttributeGroup {
6666
$values = $doctrineAnnotationTagValueNode->getValuesWithSilentKey();
67-
6867
$args = $this->createArgsFromItems($values, $annotationToAttribute->getAttributeClass());
6968

7069
$args = $this->attributeArrayNameInliner->inlineArrayToArgs($args);

packages/PhpAttribute/NodeFactory/PhpNestedAttributeGroupFactory.php

Lines changed: 87 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Rector\BetterPhpDocParser\PhpDoc\ArrayItemNode;
1616
use Rector\BetterPhpDocParser\PhpDoc\DoctrineAnnotationTagValueNode;
1717
use Rector\BetterPhpDocParser\ValueObject\PhpDoc\DoctrineAnnotation\CurlyListNode;
18+
use Rector\Core\Exception\ShouldNotHappenException;
1819
use Rector\Php80\ValueObject\NestedAnnotationToAttribute;
1920
use Rector\PhpAttribute\AnnotationToAttributeMapper;
2021
use Rector\PhpAttribute\AttributeArrayNameInliner;
@@ -71,40 +72,55 @@ public function createNested(
7172
): array {
7273
$attributeGroups = [];
7374

74-
// $values = $doctrineAnnotationTagValueNode->getValues();
75+
if ($this->isExplicitProperties($nestedAnnotationToAttribute)) {
76+
return $this->createFromExplicitProperties(
77+
$nestedAnnotationToAttribute,
78+
$doctrineAnnotationTagValueNode
79+
);
80+
}
7581

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) {
7987
continue;
8088
}
8189

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+
}
9394

94-
$args = $this->attributeArrayNameInliner->inlineArrayToArgs($args);
95-
$originalIdentifier = $nestedDoctrineAnnotationTagValueNode->identifierTypeNode->name;
95+
$attributeArgs = $this->createAttributeArgs($nestedArrayItemNode->value, $nestedAnnotationToAttribute);
9696

97-
$attributeName = $this->resolveAliasedAttributeName($originalIdentifier, $nestedAttributeClass);
97+
$originalIdentifier = $doctrineAnnotationTagValueNode->identifierTypeNode->name;
98+
$attributeName = $this->resolveAliasedAttributeName($originalIdentifier, $nestedAttributeClass);
9899

99-
$attribute = new Attribute($attributeName, $args);
100-
$attributeGroups[] = new AttributeGroup([$attribute]);
101-
}
100+
$attribute = new Attribute($attributeName, $attributeArgs);
101+
$attributeGroups[] = new AttributeGroup([$attribute]);
102102
}
103103
}
104104

105105
return $attributeGroups;
106106
}
107107

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+
108124
/**
109125
* @param ArrayItemNode[] $arrayItemNodes
110126
* @return Arg[]
@@ -170,4 +186,54 @@ private function removeItems(
170186

171187
return $arrayItemNodes;
172188
}
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+
}
173239
}

rules/Php80/NodeAnalyzer/AnnotationTargetResolver.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Rector\Php80\NodeAnalyzer;
66

77
use PhpParser\Node\Expr\ClassConstFetch;
8+
use Rector\BetterPhpDocParser\PhpDoc\ArrayItemNode;
89
use Rector\Core\PhpParser\Node\NodeFactory;
910

1011
final class AnnotationTargetResolver
@@ -29,19 +30,21 @@ public function __construct(
2930
}
3031

3132
/**
32-
* @param array<int|string, mixed> $targetValues
33+
* @param ArrayItemNode[] $targetValues
3334
* @return ClassConstFetch[]
3435
*/
3536
public function resolveFlagClassConstFetches(array $targetValues): array
3637
{
3738
$classConstFetches = [];
3839

39-
foreach (self::TARGET_TO_CONSTANT_MAP as $target => $constant) {
40-
if (! in_array($target, $targetValues, true)) {
41-
continue;
42-
}
40+
foreach ($targetValues as $targetValue) {
41+
foreach (self::TARGET_TO_CONSTANT_MAP as $target => $constant) {
42+
if ($target !== $targetValue->value) {
43+
continue;
44+
}
4345

44-
$classConstFetches[] = $this->nodeFactory->createClassConstFetch('Attribute', $constant);
46+
$classConstFetches[] = $this->nodeFactory->createClassConstFetch('Attribute', $constant);
47+
}
4548
}
4649

4750
return $classConstFetches;

rules/Php80/Rector/Class_/DoctrineAnnotationClassToAttributeRector.php

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use PhpParser\Node\AttributeGroup;
1010
use PhpParser\Node\Stmt\Class_;
1111
use PHPStan\Type\MixedType;
12+
use Rector\BetterPhpDocParser\PhpDoc\ArrayItemNode;
1213
use Rector\BetterPhpDocParser\PhpDoc\DoctrineAnnotationTagValueNode;
1314
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
1415
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTagRemover;
@@ -185,13 +186,8 @@ private function decorateTarget(PhpDocInfo $phpDocInfo, AttributeGroup $attribut
185186
$this->phpDocTagRemover->removeTagValueFromNode($phpDocInfo, $targetDoctrineAnnotationTagValueNode);
186187
}
187188

188-
$targets = $targetDoctrineAnnotationTagValueNode->getSilentValue();
189-
190-
if ($targets instanceof CurlyListNode) {
191-
$targetValues = $targets->getValues();
192-
} elseif (is_string($targets)) {
193-
$targetValues = [$targets];
194-
} else {
189+
$targetValues = $this->resolveTargetValues($targetDoctrineAnnotationTagValueNode);
190+
if ($targetValues === []) {
195191
return;
196192
}
197193

@@ -214,4 +210,24 @@ private function shouldSkipClass(PhpDocInfo $phpDocInfo, Class_ $class): bool
214210
// has attribute? skip it
215211
return $this->phpAttributeAnalyzer->hasPhpAttribute($class, AttributeName::ATTRIBUTE);
216212
}
213+
214+
/**
215+
* @return ArrayItemNode[]
216+
*/
217+
private function resolveTargetValues(DoctrineAnnotationTagValueNode $targetDoctrineAnnotationTagValueNode): array
218+
{
219+
$targets = $targetDoctrineAnnotationTagValueNode->getSilentValue();
220+
if ($targets instanceof CurlyListNode) {
221+
return $targets->getValues();
222+
}
223+
224+
if ($targets instanceof ArrayItemNode) {
225+
if ($targets->value instanceof CurlyListNode) {
226+
return $targets->value->getValues();
227+
}
228+
return [$targets];
229+
}
230+
231+
return [];
232+
}
217233
}

0 commit comments

Comments
 (0)