-
-
Notifications
You must be signed in to change notification settings - Fork 671
/
PhpAttributeGroupFactory.php
168 lines (138 loc) · 4.84 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
declare(strict_types=1);
namespace Rector\PhpAttribute\Printer;
use PhpParser\BuilderHelpers;
use PhpParser\Node\Arg;
use PhpParser\Node\Attribute;
use PhpParser\Node\AttributeGroup;
use PhpParser\Node\Expr;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Scalar\String_;
use Rector\BetterPhpDocParser\PhpDoc\DoctrineAnnotationTagValueNode;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\Php80\ValueObject\AnnotationToAttribute;
use Rector\PhpAttribute\NodeAnalyzer\NamedArgumentsResolver;
use Rector\PhpAttribute\Value\ValueNormalizer;
use Webmozart\Assert\Assert;
/**
* @see \Rector\Tests\PhpAttribute\Printer\PhpAttributeGroupFactoryTest
*/
final class PhpAttributeGroupFactory
{
public function __construct(
private NamedArgumentsResolver $namedArgumentsResolver,
private ValueNormalizer $valueNormalizer
) {
}
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);
$attribute = new Attribute($fullyQualified, $args);
return new AttributeGroup([$attribute]);
}
public function create(
DoctrineAnnotationTagValueNode $doctrineAnnotationTagValueNode,
AnnotationToAttribute $annotationToAttribute,
): AttributeGroup {
$fullyQualified = new FullyQualified($annotationToAttribute->getAttributeClass());
$values = $doctrineAnnotationTagValueNode->getValuesWithExplicitSilentAndWithoutQuotes();
$args = $this->createArgsFromItems($values);
$argumentNames = $this->namedArgumentsResolver->resolveFromClass($annotationToAttribute->getAttributeClass());
$this->completeNamedArguments($args, $argumentNames);
$attribute = new Attribute($fullyQualified, $args);
return new AttributeGroup([$attribute]);
}
/**
* @param mixed[] $items
* @return Arg[]
*/
public function createArgsFromItems(array $items, ?string $silentKey = null): array
{
$args = [];
if ($silentKey !== null && isset($items[$silentKey])) {
$silentValue = $this->mapAnnotationValueToAttribute($items[$silentKey]);
$args[] = new Arg($silentValue);
unset($items[$silentKey]);
}
foreach ($items as $key => $value) {
$value = $this->mapAnnotationValueToAttribute($value);
$name = null;
if (is_string($key)) {
$name = new Identifier($key);
}
// resolve argument name
$args[] = $this->isArrayArguments($items)
? new Arg($value, false, false, [], $name)
: new Arg($value)
;
}
return $args;
}
/**
* @param mixed[] $items
*/
private function isArrayArguments(array $items): bool
{
foreach (array_keys($items) as $key) {
if (! is_int($key)) {
return true;
}
}
return false;
}
private function normalizeStringDoubleQuote(Expr $expr): void
{
if (! $expr instanceof String_) {
return;
}
// avoid escaping quotes + preserve newlines
if (! str_contains($expr->value, "'")) {
return;
}
if (str_contains($expr->value, "\n")) {
return;
}
$expr->setAttribute(AttributeKey::KIND, String_::KIND_DOUBLE_QUOTED);
}
/**
* @param Arg[] $args
* @param string[] $argumentNames
*/
private function completeNamedArguments(array $args, array $argumentNames): void
{
Assert::allIsAOf($args, Arg::class);
foreach ($args as $key => $arg) {
$argumentName = $argumentNames[$key] ?? null;
if ($argumentName === null) {
continue;
}
if ($arg->name !== null) {
continue;
}
$arg->name = new Identifier($argumentName);
}
}
private function mapAnnotationValueToAttribute(mixed $annotationValue): Expr
{
$value = $this->valueNormalizer->normalize($annotationValue);
$value = BuilderHelpers::normalizeValue($value);
$this->normalizeStringDoubleQuote($value);
return $value;
}
}