-
-
Notifications
You must be signed in to change notification settings - Fork 672
/
PhpDocInfoPrinter.php
335 lines (276 loc) · 10.6 KB
/
PhpDocInfoPrinter.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
<?php
declare(strict_types=1);
namespace Rector\BetterPhpDocParser\Printer;
use Nette\Utils\Strings;
use PHPStan\PhpDocParser\Ast\PhpDoc\GenericTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTextNode;
use PHPStan\PhpDocParser\Lexer\Lexer;
use Rector\AttributeAwarePhpDoc\Ast\PhpDoc\AttributeAwarePhpDocNode;
use Rector\BetterPhpDocParser\Attributes\Attribute\Attribute;
use Rector\BetterPhpDocParser\Contract\PhpDocNode\AttributeAwareNodeInterface;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\BetterPhpDocParser\ValueObject\StartEndValueObject;
/**
* @see \Rector\BetterPhpDocParser\Tests\PhpDocInfo\PhpDocInfoPrinter\PhpDocInfoPrinterTest
*/
final class PhpDocInfoPrinter
{
/**
* @var int
*/
private $tokenCount;
/**
* @var int
*/
private $currentTokenPosition;
/**
* @var mixed[]
*/
private $tokens = [];
/**
* @var StartEndValueObject[]
*/
private $removedNodePositions = [];
/**
* @var AttributeAwarePhpDocNode
*/
private $attributeAwarePhpDocNode;
/**
* @var OriginalSpacingRestorer
*/
private $originalSpacingRestorer;
/**
* @var PhpDocInfo
*/
private $phpDocInfo;
/**
* @var MultilineSpaceFormatPreserver
*/
private $multilineSpaceFormatPreserver;
public function __construct(
OriginalSpacingRestorer $originalSpacingRestorer,
MultilineSpaceFormatPreserver $multilineSpaceFormatPreserver
) {
$this->originalSpacingRestorer = $originalSpacingRestorer;
$this->multilineSpaceFormatPreserver = $multilineSpaceFormatPreserver;
}
/**
* As in php-parser
*
* ref: https://github.com/nikic/PHP-Parser/issues/487#issuecomment-375986259
* - Tokens[node.startPos .. subnode1.startPos]
* - Print(subnode1)
* - Tokens[subnode1.endPos .. subnode2.startPos]
* - Print(subnode2)
* - Tokens[subnode2.endPos .. node.endPos]
*/
public function printFormatPreserving(PhpDocInfo $phpDocInfo): string
{
if ($phpDocInfo->getTokens() === []) {
// completely new noe, just print string version of it
if ($phpDocInfo->getPhpDocNode()->children === []) {
return '';
}
return (string) $phpDocInfo->getPhpDocNode();
}
$this->attributeAwarePhpDocNode = $phpDocInfo->getPhpDocNode();
$this->tokens = $phpDocInfo->getTokens();
$this->tokenCount = count($phpDocInfo->getTokens());
$this->phpDocInfo = $phpDocInfo;
$this->currentTokenPosition = 0;
$this->removedNodePositions = [];
return $this->printPhpDocNode($this->attributeAwarePhpDocNode);
}
private function printPhpDocNode(AttributeAwarePhpDocNode $attributeAwarePhpDocNode): string
{
// no nodes were, so empty doc
if ($this->isPhpDocNodeEmpty($attributeAwarePhpDocNode)) {
return '';
}
$this->currentTokenPosition = 0;
$output = '';
// node output
$nodeCount = count($attributeAwarePhpDocNode->children);
foreach ($attributeAwarePhpDocNode->children as $i => $phpDocChildNode) {
$output .= $this->printNode($phpDocChildNode, null, $i + 1, $nodeCount);
}
$output = $this->printEnd($output);
// @see
// fix missing start
if (! Strings::match($output, '#^(\/\/|\/\*\*|\/\*|\#)#') && $output) {
$output = '/**' . $output;
}
return $output;
}
private function isPhpDocNodeEmpty(PhpDocNode $phpDocNode): bool
{
if (count($phpDocNode->children) === 0) {
return true;
}
foreach ($phpDocNode->children as $phpDocChildNode) {
if ($phpDocChildNode instanceof PhpDocTextNode) {
if ($phpDocChildNode->text !== '') {
return false;
}
} else {
return false;
}
}
return true;
}
private function printNode(
AttributeAwareNodeInterface $attributeAwareNode,
?StartEndValueObject $startEndValueObject = null,
int $i = 0,
int $nodeCount = 0
): string {
$output = '';
/** @var StartEndValueObject|null $startEndValueObject */
$startEndValueObject = $attributeAwareNode->getAttribute(Attribute::START_END) ?: $startEndValueObject;
$attributeAwareNode = $this->multilineSpaceFormatPreserver->fixMultilineDescriptions($attributeAwareNode);
if ($startEndValueObject !== null) {
$isLastToken = ($nodeCount === $i);
$output = $this->addTokensFromTo(
$output,
$this->currentTokenPosition,
$startEndValueObject->getStart(),
$isLastToken
);
$this->currentTokenPosition = $startEndValueObject->getEnd();
}
if ($attributeAwareNode instanceof PhpDocTagNode) {
if ($startEndValueObject !== null) {
return $this->printPhpDocTagNode($attributeAwareNode, $startEndValueObject, $output);
}
return $output . PHP_EOL . ' * ' . $this->printAttributeWithAsterisk($attributeAwareNode);
}
if (! $attributeAwareNode instanceof PhpDocTextNode && ! $attributeAwareNode instanceof GenericTagValueNode && $startEndValueObject) {
return $this->originalSpacingRestorer->restoreInOutputWithTokensStartAndEndPosition(
$attributeAwareNode,
(string) $attributeAwareNode,
$this->tokens,
$startEndValueObject
);
}
return $output . $this->printAttributeWithAsterisk($attributeAwareNode);
}
private function printEnd(string $output): string
{
$lastTokenPosition = $this->attributeAwarePhpDocNode->getAttribute(
Attribute::LAST_TOKEN_POSITION
) ?: $this->currentTokenPosition;
return $this->addTokensFromTo($output, $lastTokenPosition, $this->tokenCount, true);
}
private function addTokensFromTo(
string $output,
int $from,
int $to,
bool $shouldSkipEmptyLinesAbove = false
): string {
// skip removed nodes
$positionJumpSet = [];
foreach ($this->getRemovedNodesPositions() as $removedTokensPosition) {
$positionJumpSet[$removedTokensPosition->getStart()] = $removedTokensPosition->getEnd();
}
// include also space before, in case of inlined docs
if (isset($this->tokens[$from - 1]) && $this->tokens[$from - 1][1] === Lexer::TOKEN_HORIZONTAL_WS) {
--$from;
}
// skip extra empty lines above if this is the last one
if ($shouldSkipEmptyLinesAbove &&
Strings::contains($this->tokens[$from][0], PHP_EOL) &&
Strings::contains($this->tokens[$from + 1][0], PHP_EOL)
) {
++$from;
}
return $this->appendToOutput($output, $from, $to, $positionJumpSet);
}
/**
* @param PhpDocTagNode|AttributeAwareNodeInterface $phpDocTagNode
*/
private function printPhpDocTagNode(
PhpDocTagNode $phpDocTagNode,
StartEndValueObject $startEndValueObject,
string $output
): string {
$output .= $phpDocTagNode->name;
$nodeOutput = $this->printNode($phpDocTagNode->value, $startEndValueObject);
if ($nodeOutput && $this->isTagSeparatedBySpace($nodeOutput, $phpDocTagNode)) {
$output .= ' ';
}
if ($phpDocTagNode->getAttribute(Attribute::HAS_DESCRIPTION_WITH_ORIGINAL_SPACES) && (property_exists(
$phpDocTagNode->value,
'description'
) && $phpDocTagNode->value->description)) {
$quotedDescription = preg_quote($phpDocTagNode->value->description, '#');
$pattern = Strings::replace($quotedDescription, '#[\s]+#', '\s+');
$nodeOutput = Strings::replace($nodeOutput, '#' . $pattern . '#', $phpDocTagNode->value->description);
if (substr_count($nodeOutput, "\n") !== 0) {
$nodeOutput = Strings::replace($nodeOutput, "#\n#", PHP_EOL . ' * ');
}
}
return $output . $nodeOutput;
}
private function printAttributeWithAsterisk(AttributeAwareNodeInterface $attributeAwareNode): string
{
$content = (string) $attributeAwareNode;
$content = explode(PHP_EOL, $content);
return implode(PHP_EOL . ' * ', $content);
}
/**
* @return StartEndValueObject[]
*/
private function getRemovedNodesPositions(): array
{
if ($this->removedNodePositions !== []) {
return $this->removedNodePositions;
}
/** @var AttributeAwareNodeInterface[] $removedNodes */
$removedNodes = array_diff(
$this->phpDocInfo->getOriginalPhpDocNode()->children,
$this->attributeAwarePhpDocNode->children
);
foreach ($removedNodes as $removedNode) {
$removedPhpDocNodeInfo = $removedNode->getAttribute(Attribute::START_END);
// change start position to start of the line, so the whole line is removed
$seekPosition = $removedPhpDocNodeInfo->getStart();
while ($this->tokens[$seekPosition][1] !== Lexer::TOKEN_HORIZONTAL_WS) {
--$seekPosition;
}
$this->removedNodePositions[] = new StartEndValueObject(
$seekPosition - 1,
$removedPhpDocNodeInfo->getEnd()
);
}
return $this->removedNodePositions;
}
/**
* @param int[] $positionJumpSet
*/
private function appendToOutput(string $output, int $from, int $to, array $positionJumpSet): string
{
for ($i = $from; $i < $to; ++$i) {
while (isset($positionJumpSet[$i])) {
$i = $positionJumpSet[$i];
}
$output .= $this->tokens[$i][0] ?? '';
}
return $output;
}
/**
* Covers:
* - "@Long\Annotation"
* - "@Route("/", name="homepage")",
* - "@customAnnotation(value)"
*/
private function isTagSeparatedBySpace(string $nodeOutput, PhpDocTagNode $phpDocTagNode): bool
{
$contentWithoutSpace = $phpDocTagNode->name . Strings::substring($nodeOutput, 0, 1);
if (Strings::contains($this->phpDocInfo->getOriginalContent(), $contentWithoutSpace)) {
return false;
}
return Strings::contains($this->phpDocInfo->getOriginalContent(), $phpDocTagNode->name . ' ');
}
}