-
-
Notifications
You must be signed in to change notification settings - Fork 336
/
PhpAttributeGroupFactory.php
139 lines (116 loc) · 4.43 KB
/
PhpAttributeGroupFactory.php
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
<?php
declare(strict_types=1);
namespace Rector\PhpAttribute\NodeFactory;
use PhpParser\Node\Arg;
use PhpParser\Node\Attribute;
use PhpParser\Node\AttributeGroup;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\ArrayItem;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt\Use_;
use Rector\BetterPhpDocParser\PhpDoc\DoctrineAnnotationTagValueNode;
use Rector\Core\Php\PhpVersionProvider;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\Php80\ValueObject\AnnotationToAttribute;
use Rector\PhpAttribute\AnnotationToAttributeMapper;
use Rector\PhpAttribute\AttributeArrayNameInliner;
use Rector\PhpAttribute\NodeAnalyzer\ExprParameterReflectionTypeCorrector;
/**
* @see \Rector\Tests\PhpAttribute\Printer\PhpAttributeGroupFactoryTest
*/
final class PhpAttributeGroupFactory
{
/**
* @var array<string, string[]>>
*/
private array $unwrappedAnnotations = [
'Doctrine\ORM\Mapping\Table' => ['indexes', 'uniqueConstraints'],
'Doctrine\ORM\Mapping\Entity' => ['uniqueConstraints'],
];
public function __construct(
private readonly AnnotationToAttributeMapper $annotationToAttributeMapper,
private readonly AttributeNameFactory $attributeNameFactory,
private readonly NamedArgsFactory $namedArgsFactory,
private readonly ExprParameterReflectionTypeCorrector $exprParameterReflectionTypeCorrector,
private readonly AttributeArrayNameInliner $attributeArrayNameInliner
) {
}
public function createFromSimpleTag(AnnotationToAttribute $annotationToAttribute): AttributeGroup
{
return $this->createFromClass($annotationToAttribute->getAttributeClass());
}
public function createFromClass(string $attributeClass): AttributeGroup
{
$fullyQualified = new FullyQualified($attributeClass);
$attribute = new Attribute($fullyQualified);
return new AttributeGroup([$attribute]);
}
/**
* @param mixed[] $items
*/
public function createFromClassWithItems(string $attributeClass, array $items): AttributeGroup
{
$fullyQualified = new FullyQualified($attributeClass);
$args = $this->createArgsFromItems($items, $attributeClass);
$attribute = new Attribute($fullyQualified, $args);
return new AttributeGroup([$attribute]);
}
/**
* @param Use_[] $uses
*/
public function create(
DoctrineAnnotationTagValueNode $doctrineAnnotationTagValueNode,
AnnotationToAttribute $annotationToAttribute,
array $uses
): AttributeGroup {
$values = $doctrineAnnotationTagValueNode->getValuesWithExplicitSilentAndWithoutQuotes();
$args = $this->createArgsFromItems($values, $annotationToAttribute->getAttributeClass());
$args = $this->attributeArrayNameInliner->inlineArrayToArgs($args);
$attributeName = $this->attributeNameFactory->create(
$annotationToAttribute,
$doctrineAnnotationTagValueNode,
$uses
);
$attribute = new Attribute($attributeName, $args);
return new AttributeGroup([$attribute]);
}
/**
* @param mixed[] $items
* @return Arg[]
*/
public function createArgsFromItems(array $items, string $attributeClass): array
{
/** @var Expr[]|Expr\Array_ $items */
$items = $this->annotationToAttributeMapper->map($items);
$items = $this->exprParameterReflectionTypeCorrector->correctItemsByAttributeClass($items, $attributeClass);
$items = $this->removeUnwrappedItems($attributeClass, $items);
return $this->namedArgsFactory->createFromValues($items);
}
/**
* @param mixed[] $items
* @return mixed[]
*/
private function removeUnwrappedItems(string $attributeClass, array $items): array
{
// unshift annotations that can be extracted
$unwrappeColumns = $this->unwrappedAnnotations[$attributeClass] ?? [];
if ($unwrappeColumns === []) {
return $items;
}
foreach ($items as $key => $item) {
if (! $item instanceof ArrayItem) {
continue;
}
if (! $item->key instanceof String_) {
continue;
}
$stringItemKey = $item->key;
if (! in_array($stringItemKey->value, $unwrappeColumns, true)) {
continue;
}
unset($items[$key]);
}
return $items;
}
}