-
-
Notifications
You must be signed in to change notification settings - Fork 441
Expand file tree
/
Copy pathPhpAttributeGroupFactory.php
More file actions
160 lines (133 loc) · 5.11 KB
/
PhpAttributeGroupFactory.php
File metadata and controls
160 lines (133 loc) · 5.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?php
declare(strict_types=1);
namespace Rector\PhpAttribute\NodeFactory;
use PhpParser\Node\Arg;
use PhpParser\Node\ArrayItem;
use PhpParser\Node\Attribute;
use PhpParser\Node\AttributeGroup;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt\Use_;
use Rector\BetterPhpDocParser\PhpDoc\ArrayItemNode;
use Rector\BetterPhpDocParser\PhpDoc\DoctrineAnnotationTagValueNode;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\Php80\ValueObject\AnnotationToAttribute;
use Rector\Php81\Enum\AttributeName;
use Rector\PhpAttribute\AnnotationToAttributeMapper;
use Rector\PhpAttribute\AttributeArrayNameInliner;
/**
* @see \Rector\Tests\PhpAttribute\Printer\PhpAttributeGroupFactoryTest
*/
final readonly class PhpAttributeGroupFactory
{
public function __construct(
private AnnotationToAttributeMapper $annotationToAttributeMapper,
private AttributeNameFactory $attributeNameFactory,
private NamedArgsFactory $namedArgsFactory,
private AnnotationToAttributeIntegerValueCaster $annotationToAttributeIntegerValueCaster,
private AttributeArrayNameInliner $attributeArrayNameInliner
) {
}
public function createFromSimpleTag(
AnnotationToAttribute $annotationToAttribute,
?string $value = null
): AttributeGroup {
return $this->createFromClass($annotationToAttribute->getAttributeClass(), $value);
}
/**
* @param AttributeName::*|string $attributeClass
*/
public function createFromClass(string $attributeClass, ?string $value = null): AttributeGroup
{
$fullyQualified = new FullyQualified($attributeClass);
$attribute = new Attribute($fullyQualified);
if ($value !== null && $value !== '') {
$arg = new Arg(new String_($value));
$attribute->args = [$arg];
}
return new AttributeGroup([$attribute]);
}
/**
* @api tests
* @param mixed[] $items
*/
public function createFromClassWithItems(string $attributeClass, array $items): AttributeGroup
{
$fullyQualified = new FullyQualified($attributeClass);
$args = $this->createArgsFromItems($items);
$attribute = new Attribute($fullyQualified, $args);
return new AttributeGroup([$attribute]);
}
/**
* @param Use_[] $uses
*/
public function create(
DoctrineAnnotationTagValueNode $doctrineAnnotationTagValueNode,
AnnotationToAttribute $annotationToAttribute,
array $uses
): AttributeGroup {
$values = $doctrineAnnotationTagValueNode->getValuesWithSilentKey();
$args = $this->createArgsFromItems($values, '', $annotationToAttribute->getClassReferenceFields());
$this->annotationToAttributeIntegerValueCaster->castAttributeTypes($annotationToAttribute, $args);
$args = $this->attributeArrayNameInliner->inlineArrayToArgs($args, $annotationToAttribute->getAttributeClass());
$attributeName = $this->attributeNameFactory->create(
$annotationToAttribute,
$doctrineAnnotationTagValueNode,
$uses
);
// keep FQN in the attribute, so it can be easily detected later
$attributeName->setAttribute(AttributeKey::PHP_ATTRIBUTE_NAME, $annotationToAttribute->getAttributeClass());
$attribute = new Attribute($attributeName, $args);
return new AttributeGroup([$attribute]);
}
/**
* @api tests
*
* @param ArrayItemNode[]|mixed[] $items
* @param string $attributeClass @deprecated
* @param string[] $classReferencedFields
*
* @return list<Arg>
*/
public function createArgsFromItems(
array $items,
string $attributeClass = '',
array $classReferencedFields = []
): array {
$mappedItems = $this->annotationToAttributeMapper->map($items);
$this->mapClassReferences($mappedItems, $classReferencedFields);
$values = $mappedItems instanceof Array_ ? $mappedItems->items : $mappedItems;
// the key here should contain the named argument
return $this->namedArgsFactory->createFromValues($values);
}
/**
* @param string[] $classReferencedFields
*/
private function mapClassReferences(Expr|string $expr, array $classReferencedFields): void
{
if (! $expr instanceof Array_) {
return;
}
foreach ($expr->items as $arrayItem) {
if (! $arrayItem instanceof ArrayItem) {
continue;
}
if (! $arrayItem->key instanceof String_) {
continue;
}
if (! in_array($arrayItem->key->value, $classReferencedFields)) {
continue;
}
if ($arrayItem->value instanceof ClassConstFetch) {
continue;
}
if (! $arrayItem->value instanceof String_) {
continue;
}
$arrayItem->value = new ClassConstFetch(new FullyQualified($arrayItem->value->value), 'class');
}
}
}