-
-
Notifications
You must be signed in to change notification settings - Fork 439
Expand file tree
/
Copy pathBetterPhpDocParser.php
More file actions
189 lines (156 loc) · 6.59 KB
/
BetterPhpDocParser.php
File metadata and controls
189 lines (156 loc) · 6.59 KB
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
<?php
declare(strict_types=1);
namespace Rector\BetterPhpDocParser\PhpDocParser;
use Nette\Utils\Strings;
use Override;
use PhpParser\Node;
use PHPStan\PhpDocParser\Ast\PhpDoc\GenericTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocChildNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTextNode;
use PHPStan\PhpDocParser\Lexer\Lexer;
use PHPStan\PhpDocParser\Parser\ConstExprParser;
use PHPStan\PhpDocParser\Parser\ParserException;
use PHPStan\PhpDocParser\Parser\PhpDocParser;
use PHPStan\PhpDocParser\Parser\TokenIterator;
use PHPStan\PhpDocParser\Parser\TypeParser;
use PHPStan\PhpDocParser\ParserConfig;
use Rector\BetterPhpDocParser\Contract\PhpDocParser\PhpDocNodeDecoratorInterface;
use Rector\BetterPhpDocParser\PhpDocInfo\TokenIteratorFactory;
use Rector\BetterPhpDocParser\ValueObject\Parser\BetterTokenIterator;
use Rector\BetterPhpDocParser\ValueObject\PhpDocAttributeKey;
use Rector\BetterPhpDocParser\ValueObject\StartAndEnd;
use Rector\Exception\ShouldNotHappenException;
use Rector\Util\Reflection\PrivatesAccessor;
/**
* @see \Rector\Tests\BetterPhpDocParser\PhpDocParser\TagValueNodeReprint\TagValueNodeReprintTest
*/
final class BetterPhpDocParser extends PhpDocParser
{
/**
* @see https://regex101.com/r/JDzr0c/1
*/
private const string NEW_LINE_REGEX = "#(?<new_line>\r\n|\n)#";
/**
* @see https://regex101.com/r/JOKSmr/5
*/
private const string MULTI_NEW_LINES_REGEX = '#(?<new_line>\r\n|\n){2,}#';
/**
* @param PhpDocNodeDecoratorInterface[] $phpDocNodeDecorators
*/
public function __construct(
ParserConfig $parserConfig,
TypeParser $typeParser,
ConstExprParser $constExprParser,
private readonly TokenIteratorFactory $tokenIteratorFactory,
private readonly array $phpDocNodeDecorators,
private readonly PrivatesAccessor $privatesAccessor,
) {
parent::__construct(
// ParserConfig
$parserConfig,
// TypeParser
$typeParser,
// ConstExprParser
$constExprParser,
);
}
public function parseWithNode(BetterTokenIterator $betterTokenIterator, Node $node): PhpDocNode
{
$betterTokenIterator->consumeTokenType(Lexer::TOKEN_OPEN_PHPDOC);
$betterTokenIterator->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL);
$children = [];
if (! $betterTokenIterator->isCurrentTokenType(Lexer::TOKEN_CLOSE_PHPDOC)) {
$children[] = $this->parseChildAndStoreItsPositions($betterTokenIterator);
while ($betterTokenIterator->tryConsumeTokenType(
Lexer::TOKEN_PHPDOC_EOL
) && ! $betterTokenIterator->isCurrentTokenType(Lexer::TOKEN_CLOSE_PHPDOC)) {
$children[] = $this->parseChildAndStoreItsPositions($betterTokenIterator);
}
}
// might be in the middle of annotations
$betterTokenIterator->tryConsumeTokenType(Lexer::TOKEN_CLOSE_PHPDOC);
$phpDocNode = new PhpDocNode($children);
foreach ($this->phpDocNodeDecorators as $phpDocNodeDecorator) {
$phpDocNodeDecorator->decorate($phpDocNode, $node);
}
return $phpDocNode;
}
#[Override]
public function parseTag(TokenIterator $tokenIterator): PhpDocTagNode
{
// replace generic nodes with DoctrineAnnotations
if (! $tokenIterator instanceof BetterTokenIterator) {
throw new ShouldNotHappenException();
}
$tag = $this->resolveTag($tokenIterator);
$phpDocTagValueNode = $this->parseTagValue($tokenIterator, $tag);
return new PhpDocTagNode($tag, $phpDocTagValueNode);
}
/**
* @param BetterTokenIterator $tokenIterator
*/
#[Override]
public function parseTagValue(TokenIterator $tokenIterator, string $tag): PhpDocTagValueNode
{
$isPrecededByHorizontalWhitespace = $tokenIterator->isPrecededByHorizontalWhitespace();
$startPosition = $tokenIterator->currentPosition();
$phpDocTagValueNode = parent::parseTagValue($tokenIterator, $tag);
$endPosition = $tokenIterator->currentPosition();
if ($isPrecededByHorizontalWhitespace && property_exists($phpDocTagValueNode, 'description')) {
$phpDocTagValueNode->description = Strings::replace(
(string) $phpDocTagValueNode->description,
self::NEW_LINE_REGEX,
static fn (array $match): string => $match['new_line'] . ' * '
);
}
$startAndEnd = new StartAndEnd($startPosition, $endPosition);
$phpDocTagValueNode->setAttribute(PhpDocAttributeKey::START_AND_END, $startAndEnd);
if ($phpDocTagValueNode instanceof GenericTagValueNode) {
$phpDocTagValueNode->value = Strings::replace(
$phpDocTagValueNode->value,
self::MULTI_NEW_LINES_REGEX,
static fn (array $match): string => (string) $match['new_line']
);
}
return $phpDocTagValueNode;
}
/**
* @return PhpDocTextNode|PhpDocTagNode
*/
private function parseChildAndStoreItsPositions(TokenIterator $tokenIterator): PhpDocChildNode
{
$betterTokenIterator = $this->tokenIteratorFactory->createFromTokenIterator($tokenIterator);
$startPosition = $betterTokenIterator->currentPosition();
try {
/** @var PhpDocTextNode|PhpDocTagNode $phpDocNode */
$phpDocNode = $this->privatesAccessor->callPrivateMethod($this, 'parseChild', [$betterTokenIterator]);
} catch (ParserException) {
$phpDocNode = new PhpDocTextNode('');
}
$endPosition = $betterTokenIterator->currentPosition();
$startAndEnd = new StartAndEnd($startPosition, $endPosition);
$phpDocNode->setAttribute(PhpDocAttributeKey::START_AND_END, $startAndEnd);
return $phpDocNode;
}
private function resolveTag(BetterTokenIterator $tokenIterator): string
{
$tag = $tokenIterator->currentTokenValue();
$tokenIterator->next();
// there is a space → stop
if ($tokenIterator->isPrecededByHorizontalWhitespace()) {
return $tag;
}
// is not e.g "@var "
// join tags like "@ORM\Column" etc.
if (! $tokenIterator->isCurrentTokenType(Lexer::TOKEN_IDENTIFIER)) {
return $tag;
}
// @todo use joinUntil("(")?
$tag .= $tokenIterator->currentTokenValue();
$tokenIterator->next();
return $tag;
}
}