Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a24cc02

Browse files
Allow omitting @param type
1 parent 129a63b commit a24cc02

File tree

4 files changed

+118
-5
lines changed

4 files changed

+118
-5
lines changed

‎doc/grammars/phpdoc-param.peg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
PhpDocParam
22
= AnnotationName Type IsReference? IsVariadic? ParameterName Description?
3+
/ AnnotationName Type? IsReference? IsVariadic? ParameterName Description
34

45
AnnotationName
56
= '@param'

‎src/Ast/PhpDoc/ParamTagValueNode.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class ParamTagValueNode implements PhpDocTagValueNode
1111

1212
use NodeAttributes;
1313

14-
/** @var TypeNode */
14+
/** @var TypeNode|null */
1515
public $type;
1616

1717
/** @var bool */
@@ -26,7 +26,7 @@ class ParamTagValueNode implements PhpDocTagValueNode
2626
/** @var string (may be empty) */
2727
public $description;
2828

29-
public function __construct(TypeNode $type, bool $isVariadic, string $parameterName, string $description, bool $isReference = false)
29+
public function __construct(?TypeNode $type, bool $isVariadic, string $parameterName, string $description, bool $isReference = false)
3030
{
3131
$this->type = $type;
3232
$this->isReference = $isReference;
@@ -38,9 +38,10 @@ public function __construct(TypeNode $type, bool $isVariadic, string $parameterN
3838

3939
public function __toString(): string
4040
{
41+
$type = $this->type !== null ? "{$this->type}" : '';
4142
$reference = $this->isReference ? '&' : '';
4243
$variadic = $this->isVariadic ? '...' : '';
43-
return trim("{$this->type}{$reference}{$variadic}{$this->parameterName}{$this->description}");
44+
return trim("{$type}{$reference}{$variadic}{$this->parameterName}{$this->description}");
4445
}
4546

4647
}

‎src/Parser/PhpDocParser.php

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use PHPStan\ShouldNotHappenException;
99
use function array_values;
1010
use function count;
11+
use function strlen;
1112
use function trim;
1213

1314
class PhpDocParser
@@ -223,11 +224,20 @@ public function parseTagValue(TokenIterator $tokens, string $tag): Ast\PhpDoc\Ph
223224

224225
private function parseParamTagValue(TokenIterator $tokens): Ast\PhpDoc\ParamTagValueNode
225226
{
226-
$type = $this->typeParser->parse($tokens);
227+
if (
228+
$tokens->isCurrentTokenType(Lexer::TOKEN_REFERENCE)
229+
|| $tokens->isCurrentTokenType(Lexer::TOKEN_VARIADIC)
230+
|| $tokens->isCurrentTokenType(Lexer::TOKEN_VARIABLE)
231+
) {
232+
$type = null;
233+
} else {
234+
$type = $this->typeParser->parse($tokens);
235+
}
236+
227237
$isReference = $tokens->tryConsumeTokenType(Lexer::TOKEN_REFERENCE);
228238
$isVariadic = $tokens->tryConsumeTokenType(Lexer::TOKEN_VARIADIC);
229239
$parameterName = $this->parseRequiredVariableName($tokens);
230-
$description = $this->parseOptionalDescription($tokens);
240+
$description = $type === null ? $this->parseRequiredDescription($tokens) : $this->parseOptionalDescription($tokens);
231241
return new Ast\PhpDoc\ParamTagValueNode($type, $isVariadic, $parameterName, $description, $isReference);
232242
}
233243

@@ -445,6 +455,22 @@ private function parseRequiredVariableName(TokenIterator $tokens): string
445455
return $parameterName;
446456
}
447457

458+
private function parseRequiredDescription(TokenIterator $tokens): string
459+
{
460+
$tokens->pushSavePoint();
461+
462+
$description = $this->parseOptionalDescription($tokens);
463+
464+
if (strlen($description) === 0) {
465+
$tokens->rollback();
466+
467+
$tokens->consumeTokenType(Lexer::TOKEN_OTHER);
468+
}
469+
470+
$tokens->dropSavePoint();
471+
472+
return $description;
473+
}
448474

449475
private function parseOptionalDescription(TokenIterator $tokens, bool $limitStartToken = false): string
450476
{

‎tests/PHPStan/Parser/PhpDocParserTest.php

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,72 @@ public function provideParamTagsData(): Iterator
239239
]),
240240
];
241241

242+
yield [
243+
'OK without type',
244+
'/** @param $foo description */',
245+
new PhpDocNode([
246+
new PhpDocTagNode(
247+
'@param',
248+
new ParamTagValueNode(
249+
null,
250+
false,
251+
'$foo',
252+
'description'
253+
)
254+
),
255+
]),
256+
];
257+
258+
yield [
259+
'OK reference without type',
260+
'/** @param &$foo description */',
261+
new PhpDocNode([
262+
new PhpDocTagNode(
263+
'@param',
264+
new ParamTagValueNode(
265+
null,
266+
false,
267+
'$foo',
268+
'description',
269+
true
270+
)
271+
),
272+
]),
273+
];
274+
275+
yield [
276+
'OK variadic without type',
277+
'/** @param ...$foo description */',
278+
new PhpDocNode([
279+
new PhpDocTagNode(
280+
'@param',
281+
new ParamTagValueNode(
282+
null,
283+
true,
284+
'$foo',
285+
'description'
286+
)
287+
),
288+
]),
289+
];
290+
291+
yield [
292+
'OK reference variadic without type',
293+
'/** @param &...$foo description */',
294+
new PhpDocNode([
295+
new PhpDocTagNode(
296+
'@param',
297+
new ParamTagValueNode(
298+
null,
299+
true,
300+
'$foo',
301+
'description',
302+
true
303+
)
304+
),
305+
]),
306+
];
307+
242308
yield [
243309
'invalid without type, parameter name and description',
244310
'/** @param */',
@@ -390,6 +456,25 @@ public function provideParamTagsData(): Iterator
390456
),
391457
]),
392458
];
459+
460+
yield [
461+
'invalid without type and description',
462+
'/** @param $foo */',
463+
new PhpDocNode([
464+
new PhpDocTagNode(
465+
'@param',
466+
new InvalidTagValueNode(
467+
'$foo',
468+
new ParserException(
469+
'*/',
470+
Lexer::TOKEN_CLOSE_PHPDOC,
471+
16,
472+
Lexer::TOKEN_OTHER
473+
)
474+
)
475+
),
476+
]),
477+
];
393478
}
394479

395480

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /