Skip to content

Commit 87fa2d5

Browse files
authored
Implement @param-out tag
* implement `@param-out` tag * added description
1 parent 5f7eb97 commit 87fa2d5

File tree

4 files changed

+100
-0
lines changed

4 files changed

+100
-0
lines changed
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\PhpDocParser\Ast\PhpDoc;
4+
5+
use PHPStan\PhpDocParser\Ast\NodeAttributes;
6+
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
7+
use function trim;
8+
9+
class ParamOutTagValueNode implements PhpDocTagValueNode
10+
{
11+
12+
use NodeAttributes;
13+
14+
/** @var TypeNode */
15+
public $type;
16+
17+
/** @var string */
18+
public $parameterName;
19+
20+
/** @var string (may be empty) */
21+
public $description;
22+
23+
public function __construct(TypeNode $type, string $parameterName, string $description)
24+
{
25+
$this->type = $type;
26+
$this->parameterName = $parameterName;
27+
$this->description = $description;
28+
}
29+
30+
public function __toString(): string
31+
{
32+
return trim("{$this->type} {$this->parameterName} {$this->description}");
33+
}
34+
35+
}

src/Ast/PhpDoc/PhpDocNode.php

+15
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,21 @@ static function (PhpDocTagValueNode $value): bool {
341341
);
342342
}
343343

344+
345+
/**
346+
* @return ParamOutTagValueNode[]
347+
*/
348+
public function getParamOutTypeTagValues(string $tagName = '@param-out'): array
349+
{
350+
return array_filter(
351+
array_column($this->getTagsByName($tagName), 'value'),
352+
static function (PhpDocTagValueNode $value): bool {
353+
return $value instanceof ParamOutTagValueNode;
354+
}
355+
);
356+
}
357+
358+
344359
public function __toString(): string
345360
{
346361
$children = array_map(

src/Parser/PhpDocParser.php

+15
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,12 @@ public function parseTagValue(TokenIterator $tokens, string $tag): Ast\PhpDoc\Ph
229229
$tagValue = $this->parseSelfOutTagValue($tokens);
230230
break;
231231

232+
case '@param-out':
233+
case '@phpstan-param-out':
234+
case '@psalm-param-out':
235+
$tagValue = $this->parseParamOutTagValue($tokens);
236+
break;
237+
232238
default:
233239
$tagValue = new Ast\PhpDoc\GenericTagValueNode($this->parseOptionalDescription($tokens));
234240
break;
@@ -514,6 +520,15 @@ private function parseSelfOutTagValue(TokenIterator $tokens): Ast\PhpDoc\SelfOut
514520
return new Ast\PhpDoc\SelfOutTagValueNode($type, $description);
515521
}
516522

523+
private function parseParamOutTagValue(TokenIterator $tokens): Ast\PhpDoc\ParamOutTagValueNode
524+
{
525+
$type = $this->typeParser->parse($tokens);
526+
$parameterName = $this->parseRequiredVariableName($tokens);
527+
$description = $this->parseOptionalDescription($tokens);
528+
529+
return new Ast\PhpDoc\ParamOutTagValueNode($type, $parameterName, $description);
530+
}
531+
517532
private function parseOptionalVariableName(TokenIterator $tokens): string
518533
{
519534
if ($tokens->isCurrentTokenType(Lexer::TOKEN_VARIABLE)) {

tests/PHPStan/Parser/PhpDocParserTest.php

+35
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use PHPStan\PhpDocParser\Ast\PhpDoc\MethodTagValueNode;
2020
use PHPStan\PhpDocParser\Ast\PhpDoc\MethodTagValueParameterNode;
2121
use PHPStan\PhpDocParser\Ast\PhpDoc\MixinTagValueNode;
22+
use PHPStan\PhpDocParser\Ast\PhpDoc\ParamOutTagValueNode;
2223
use PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode;
2324
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocNode;
2425
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
@@ -92,6 +93,7 @@ protected function setUp(): void
9293
* @dataProvider provideDescriptionWithOrWithoutHtml
9394
* @dataProvider provideTagsWithBackslash
9495
* @dataProvider provideSelfOutTagsData
96+
* @dataProvider provideParamOutTagsData
9597
*/
9698
public function testParse(
9799
string $label,
@@ -4560,6 +4562,39 @@ public function provideSelfOutTagsData(): Iterator
45604562
];
45614563
}
45624564

4565+
public function provideParamOutTagsData(): Iterator
4566+
{
4567+
yield [
4568+
'OK param-out',
4569+
'/** @param-out string $s */',
4570+
new PhpDocNode([
4571+
new PhpDocTagNode(
4572+
'@param-out',
4573+
new ParamOutTagValueNode(
4574+
new IdentifierTypeNode('string'),
4575+
'$s',
4576+
''
4577+
)
4578+
),
4579+
]),
4580+
];
4581+
4582+
yield [
4583+
'OK param-out description',
4584+
'/** @param-out string $s description */',
4585+
new PhpDocNode([
4586+
new PhpDocTagNode(
4587+
'@param-out',
4588+
new ParamOutTagValueNode(
4589+
new IdentifierTypeNode('string'),
4590+
'$s',
4591+
'description'
4592+
)
4593+
),
4594+
]),
4595+
];
4596+
}
4597+
45634598
/**
45644599
* @dataProvider dataParseTagValue
45654600
* @param PhpDocNode $expectedPhpDocNode

0 commit comments

Comments
 (0)