-
-
Notifications
You must be signed in to change notification settings - Fork 355
/
DoctrineAnnotationDecorator.php
216 lines (172 loc) · 8.01 KB
/
DoctrineAnnotationDecorator.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
<?php
declare(strict_types=1);
namespace Rector\BetterPhpDocParser\PhpDocParser;
use PhpParser\Node;
use PHPStan\PhpDocParser\Ast\PhpDoc\GenericTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Lexer\Lexer;
use Rector\BetterPhpDocParser\Attributes\AttributeMirrorer;
use Rector\BetterPhpDocParser\PhpDoc\DoctrineAnnotationTagValueNode;
use Rector\BetterPhpDocParser\PhpDoc\SpacelessPhpDocTagNode;
use Rector\BetterPhpDocParser\PhpDocInfo\TokenIteratorFactory;
use Rector\BetterPhpDocParser\ValueObject\DoctrineAnnotation\SilentKeyMap;
use Rector\BetterPhpDocParser\ValueObject\PhpDocAttributeKey;
use Rector\BetterPhpDocParser\ValueObject\StartAndEnd;
use Rector\Core\Configuration\CurrentNodeProvider;
use Rector\Core\Exception\ShouldNotHappenException;
final class DoctrineAnnotationDecorator
{
public function __construct(
private CurrentNodeProvider $currentNodeProvider,
private ClassAnnotationMatcher $classAnnotationMatcher,
private StaticDoctrineAnnotationParser $staticDoctrineAnnotationParser,
private TokenIteratorFactory $tokenIteratorFactory,
private AttributeMirrorer $attributeMirrorer
) {
}
public function decorate(PhpDocNode $phpDocNode): void
{
$currentPhpNode = $this->currentNodeProvider->getNode();
if (! $currentPhpNode instanceof Node) {
throw new ShouldNotHappenException();
}
// merge split doctrine nested tags
$this->mergeNestedDoctrineAnnotations($phpDocNode);
$this->transformGenericTagValueNodesToDoctrineAnnotationTagValueNodes($phpDocNode, $currentPhpNode);
}
/**
* Join token iterator with all the following nodes if nested
*/
private function mergeNestedDoctrineAnnotations(PhpDocNode $phpDocNode): void
{
$removedKeys = [];
foreach ($phpDocNode->children as $key => $phpDocChildNode) {
if (in_array($key, $removedKeys, true)) {
continue;
}
if (! $phpDocChildNode instanceof PhpDocTagNode) {
continue;
}
if (! $phpDocChildNode->value instanceof GenericTagValueNode) {
continue;
}
$genericTagValueNode = $phpDocChildNode->value;
while (isset($phpDocNode->children[$key])) {
++$key;
// no more next nodes
if (! isset($phpDocNode->children[$key])) {
break;
}
$nextPhpDocChildNode = $phpDocNode->children[$key];
if (! $nextPhpDocChildNode instanceof PhpDocTagNode) {
continue;
}
if (! $nextPhpDocChildNode->value instanceof GenericTagValueNode) {
continue;
}
if ($this->isClosedContent($genericTagValueNode->value)) {
break;
}
$composedContent = $genericTagValueNode->value . PHP_EOL . $nextPhpDocChildNode->name . $nextPhpDocChildNode->value;
$genericTagValueNode->value = $composedContent;
/** @var StartAndEnd $currentStartAndEnd */
$currentStartAndEnd = $phpDocChildNode->getAttribute(PhpDocAttributeKey::START_AND_END);
/** @var StartAndEnd $nextStartAndEnd */
$nextStartAndEnd = $nextPhpDocChildNode->getAttribute(PhpDocAttributeKey::START_AND_END);
$startAndEnd = new StartAndEnd($currentStartAndEnd->getStart(), $nextStartAndEnd->getEnd());
$phpDocChildNode->setAttribute(PhpDocAttributeKey::START_AND_END, $startAndEnd);
$currentChildValueNode = $phpDocNode->children[$key];
if (! $currentChildValueNode instanceof PhpDocTagNode) {
continue;
}
$currentGenericTagValueNode = $currentChildValueNode->value;
if (! $currentGenericTagValueNode instanceof GenericTagValueNode) {
continue;
}
$removedKeys[] = $key;
}
}
foreach (array_keys($phpDocNode->children) as $key) {
if (! in_array($key, $removedKeys, true)) {
continue;
}
unset($phpDocNode->children[$key]);
}
}
private function transformGenericTagValueNodesToDoctrineAnnotationTagValueNodes(
PhpDocNode $phpDocNode,
Node $currentPhpNode
): void {
foreach ($phpDocNode->children as $key => $phpDocChildNode) {
if (! $phpDocChildNode instanceof PhpDocTagNode) {
continue;
}
if (! $phpDocChildNode->value instanceof GenericTagValueNode) {
continue;
}
// known doc tag to annotation class
$fullyQualifiedAnnotationClass = $this->classAnnotationMatcher->resolveTagFullyQualifiedName(
$phpDocChildNode->name,
$currentPhpNode
);
// not an annotations class
if (! \str_contains($fullyQualifiedAnnotationClass, '\\')) {
continue;
}
$genericTagValueNode = $phpDocChildNode->value;
$nestedTokenIterator = $this->tokenIteratorFactory->create($genericTagValueNode->value);
// mimics doctrine behavior just in phpdoc-parser syntax :)
// https://github.com/doctrine/annotations/blob/c66f06b7c83e9a2a7523351a9d5a4b55f885e574/lib/Doctrine/Common/Annotations/DocParser.php#L742
$values = $this->staticDoctrineAnnotationParser->resolveAnnotationMethodCall($nestedTokenIterator);
$formerStartEnd = $genericTagValueNode->getAttribute(PhpDocAttributeKey::START_AND_END);
$identifierTypeNode = new IdentifierTypeNode($phpDocChildNode->name);
$identifierTypeNode->setAttribute(PhpDocAttributeKey::RESOLVED_CLASS, $fullyQualifiedAnnotationClass);
$doctrineAnnotationTagValueNode = new DoctrineAnnotationTagValueNode(
$identifierTypeNode,
$genericTagValueNode->value,
$values,
SilentKeyMap::CLASS_NAMES_TO_SILENT_KEYS[$fullyQualifiedAnnotationClass] ?? null
);
$doctrineAnnotationTagValueNode->setAttribute(PhpDocAttributeKey::START_AND_END, $formerStartEnd);
$spacelessPhpDocTagNode = new SpacelessPhpDocTagNode(
$phpDocChildNode->name,
$doctrineAnnotationTagValueNode
);
$this->attributeMirrorer->mirror($phpDocChildNode, $spacelessPhpDocTagNode);
$phpDocNode->children[$key] = $spacelessPhpDocTagNode;
}
}
/**
* This is closed block, e.g. {( ... )},
* false on: {( ... )
*/
private function isClosedContent(string $composedContent): bool
{
$composedTokenIterator = $this->tokenIteratorFactory->create($composedContent);
$tokenCount = $composedTokenIterator->count();
$openBracketCount = 0;
$closeBracketCount = 0;
if ($composedContent === '') {
return true;
}
do {
if ($composedTokenIterator->isCurrentTokenTypes([
Lexer::TOKEN_OPEN_CURLY_BRACKET,
Lexer::TOKEN_OPEN_PARENTHESES,
]) || \str_contains($composedTokenIterator->currentTokenValue(), '(')) {
++$openBracketCount;
}
if ($composedTokenIterator->isCurrentTokenTypes([
Lexer::TOKEN_CLOSE_CURLY_BRACKET,
Lexer::TOKEN_CLOSE_PARENTHESES,
// sometimes it gets mixed int ")
]) || \str_contains($composedTokenIterator->currentTokenValue(), ')')) {
++$closeBracketCount;
}
$composedTokenIterator->next();
} while ($composedTokenIterator->currentPosition() < ($tokenCount - 1));
return $openBracketCount === $closeBracketCount;
}
}