Skip to content

Commit 687d002

Browse files
committed
PhpDocParser: disallow few tokens at the beginning of some descriptions
1 parent 5fa9b5a commit 687d002

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

src/Parser/PhpDocParser.php

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@
88
class PhpDocParser
99
{
1010

11+
const DISALLOWED_DESCRIPTION_START_TOKENS = [
12+
Lexer::TOKEN_UNION,
13+
Lexer::TOKEN_INTERSECTION,
14+
Lexer::TOKEN_OPEN_ANGLE_BRACKET,
15+
];
16+
1117
/** @var TypeParser */
1218
private $typeParser;
1319

@@ -116,24 +122,24 @@ private function parseParamTagValue(TokenIterator $tokens): Ast\PhpDoc\ParamTagV
116122
{
117123
$type = $this->typeParser->parse($tokens);
118124
$parameterName = $this->parseOptionalVariableName($tokens);
119-
$description = $this->parseOptionalDescription($tokens);
125+
$description = $this->parseOptionalDescription($tokens, $parameterName === '');
120126
return new Ast\PhpDoc\ParamTagValueNode($type, $parameterName, $description);
121127
}
122128

123129

124130
private function parseVarTagValue(TokenIterator $tokens): Ast\PhpDoc\VarTagValueNode
125131
{
126132
$type = $this->typeParser->parse($tokens);
127-
$parameterName = $this->parseOptionalVariableName($tokens);
128-
$description = $this->parseOptionalDescription($tokens);
129-
return new Ast\PhpDoc\VarTagValueNode($type, $parameterName, $description);
133+
$variableName = $this->parseOptionalVariableName($tokens);
134+
$description = $this->parseOptionalDescription($tokens, $variableName === '');
135+
return new Ast\PhpDoc\VarTagValueNode($type, $variableName, $description);
130136
}
131137

132138

133139
private function parseReturnTagValue(TokenIterator $tokens): Ast\PhpDoc\ReturnTagValueNode
134140
{
135141
$type = $this->typeParser->parse($tokens);
136-
$description = $this->parseOptionalDescription($tokens);
142+
$description = $this->parseOptionalDescription($tokens, true);
137143
return new Ast\PhpDoc\ReturnTagValueNode($type, $description);
138144
}
139145

@@ -235,8 +241,16 @@ private function parseRequiredVariableName(TokenIterator $tokens): string
235241
}
236242

237243

238-
private function parseOptionalDescription(TokenIterator $tokens): string
244+
private function parseOptionalDescription(TokenIterator $tokens, bool $limitStartToken = false): string
239245
{
246+
if ($limitStartToken) {
247+
foreach (self::DISALLOWED_DESCRIPTION_START_TOKENS as $disallowedStartToken) {
248+
if ($tokens->isCurrentTokenType($disallowedStartToken)) {
249+
$tokens->consumeTokenType(Lexer::TOKEN_OTHER); // will throw exception
250+
}
251+
}
252+
}
253+
240254
// description MUST separated from any previous node by horizontal whitespace
241255
if ($tokens->tryConsumeHorizontalWhiteSpace()) {
242256
return $tokens->joinUntil(Lexer::TOKEN_EOL, Lexer::TOKEN_PHPDOC_TAG, Lexer::TOKEN_CLOSE_PHPDOC);

0 commit comments

Comments
 (0)