-
-
Notifications
You must be signed in to change notification settings - Fork 671
/
PhpAttributeGroupFactory.php
146 lines (144 loc) · 5.74 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
<?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 RectorPrefix20211120\Webmozart\Assert\Assert;
/**
* @see \Rector\Tests\PhpAttribute\Printer\PhpAttributeGroupFactoryTest
*/
final class PhpAttributeGroupFactory
{
/**
* @var \Rector\PhpAttribute\NodeAnalyzer\NamedArgumentsResolver
*/
private $namedArgumentsResolver;
/**
* @var \Rector\PhpAttribute\Value\ValueNormalizer
*/
private $valueNormalizer;
public function __construct(\Rector\PhpAttribute\NodeAnalyzer\NamedArgumentsResolver $namedArgumentsResolver, \Rector\PhpAttribute\Value\ValueNormalizer $valueNormalizer)
{
$this->namedArgumentsResolver = $namedArgumentsResolver;
$this->valueNormalizer = $valueNormalizer;
}
public function createFromSimpleTag(\Rector\Php80\ValueObject\AnnotationToAttribute $annotationToAttribute) : \PhpParser\Node\AttributeGroup
{
return $this->createFromClass($annotationToAttribute->getAttributeClass());
}
public function createFromClass(string $attributeClass) : \PhpParser\Node\AttributeGroup
{
$fullyQualified = new \PhpParser\Node\Name\FullyQualified($attributeClass);
$attribute = new \PhpParser\Node\Attribute($fullyQualified);
return new \PhpParser\Node\AttributeGroup([$attribute]);
}
/**
* @param mixed[] $items
*/
public function createFromClassWithItems(string $attributeClass, array $items) : \PhpParser\Node\AttributeGroup
{
$fullyQualified = new \PhpParser\Node\Name\FullyQualified($attributeClass);
$args = $this->createArgsFromItems($items);
$attribute = new \PhpParser\Node\Attribute($fullyQualified, $args);
return new \PhpParser\Node\AttributeGroup([$attribute]);
}
public function create(\Rector\BetterPhpDocParser\PhpDoc\DoctrineAnnotationTagValueNode $doctrineAnnotationTagValueNode, \Rector\Php80\ValueObject\AnnotationToAttribute $annotationToAttribute) : \PhpParser\Node\AttributeGroup
{
$fullyQualified = new \PhpParser\Node\Name\FullyQualified($annotationToAttribute->getAttributeClass());
$values = $doctrineAnnotationTagValueNode->getValuesWithExplicitSilentAndWithoutQuotes();
$args = $this->createArgsFromItems($values);
$argumentNames = $this->namedArgumentsResolver->resolveFromClass($annotationToAttribute->getAttributeClass());
$this->completeNamedArguments($args, $argumentNames);
$attribute = new \PhpParser\Node\Attribute($fullyQualified, $args);
return new \PhpParser\Node\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 \PhpParser\Node\Arg($silentValue);
unset($items[$silentKey]);
}
foreach ($items as $key => $value) {
$value = $this->mapAnnotationValueToAttribute($value);
$name = null;
if (\is_string($key)) {
$name = new \PhpParser\Node\Identifier($key);
}
// resolve argument name
$args[] = $this->isArrayArguments($items) ? new \PhpParser\Node\Arg($value, \false, \false, [], $name) : new \PhpParser\Node\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(\PhpParser\Node\Expr $expr) : void
{
if (!$expr instanceof \PhpParser\Node\Scalar\String_) {
return;
}
// avoid escaping quotes + preserve newlines
if (\strpos($expr->value, "'") === \false) {
return;
}
if (\strpos($expr->value, "\n") !== \false) {
return;
}
$expr->setAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::KIND, \PhpParser\Node\Scalar\String_::KIND_DOUBLE_QUOTED);
}
/**
* @param Arg[] $args
* @param string[] $argumentNames
*/
private function completeNamedArguments(array $args, array $argumentNames) : void
{
\RectorPrefix20211120\Webmozart\Assert\Assert::allIsAOf($args, \PhpParser\Node\Arg::class);
foreach ($args as $key => $arg) {
$argumentName = $argumentNames[$key] ?? null;
if ($argumentName === null) {
continue;
}
if ($arg->name !== null) {
continue;
}
$arg->name = new \PhpParser\Node\Identifier($argumentName);
}
}
/**
* @param mixed $annotationValue
*/
private function mapAnnotationValueToAttribute($annotationValue) : \PhpParser\Node\Expr
{
$value = $this->valueNormalizer->normalize($annotationValue);
$value = \PhpParser\BuilderHelpers::normalizeValue($value);
$this->normalizeStringDoubleQuote($value);
return $value;
}
}