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 8f703ba

Browse files
rvanvelzenondrejmirtes
authored andcommitted
Add assert syntax
1 parent 129a63b commit 8f703ba

File tree

3 files changed

+136
-0
lines changed

3 files changed

+136
-0
lines changed

‎src/Ast/PhpDoc/AssertTagValueNode.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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 AssertTagValueNode implements PhpDocTagValueNode
10+
{
11+
12+
use NodeAttributes;
13+
14+
/** @var TypeNode */
15+
public $type;
16+
17+
/** @var string */
18+
public $parameter;
19+
20+
/** @var string (may be empty) */
21+
public $description;
22+
23+
public function __construct(TypeNode $type, string $parameter, string $description)
24+
{
25+
$this->type = $type;
26+
$this->parameter = $parameter;
27+
$this->description = $description;
28+
}
29+
30+
31+
public function __toString(): string
32+
{
33+
return trim("{$this->type}{$this->parameter}{$this->description}");
34+
}
35+
36+
}

‎src/Parser/PhpDocParser.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,11 @@ public function parseTagValue(TokenIterator $tokens, string $tag): Ast\PhpDoc\Ph
205205
$tagValue = $this->parseTypeAliasImportTagValue($tokens);
206206
break;
207207

208+
case '@phpstan-assert':
209+
case '@psalm-assert':
210+
$tagValue = $this->parseAssertTagValue($tokens);
211+
break;
212+
208213
default:
209214
$tagValue = new Ast\PhpDoc\GenericTagValueNode($this->parseOptionalDescription($tokens));
210215
break;
@@ -420,6 +425,14 @@ private function parseTypeAliasImportTagValue(TokenIterator $tokens): Ast\PhpDoc
420425
return new Ast\PhpDoc\TypeAliasImportTagValueNode($importedAlias, new IdentifierTypeNode($importedFrom), $importedAs);
421426
}
422427

428+
private function parseAssertTagValue(TokenIterator $tokens): Ast\PhpDoc\AssertTagValueNode
429+
{
430+
$type = $this->typeParser->parse($tokens);
431+
$parameter = $this->parseRequiredVariableName($tokens);
432+
$description = $this->parseOptionalDescription($tokens);
433+
return new Ast\PhpDoc\AssertTagValueNode($type, $parameter, $description);
434+
}
435+
423436
private function parseOptionalVariableName(TokenIterator $tokens): string
424437
{
425438
if ($tokens->isCurrentTokenType(Lexer::TOKEN_VARIABLE)) {

‎tests/PHPStan/Parser/PhpDocParserTest.php

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprStringNode;
99
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstFetchNode;
1010
use PHPStan\PhpDocParser\Ast\Node;
11+
use PHPStan\PhpDocParser\Ast\PhpDoc\AssertTagValueNode;
1112
use PHPStan\PhpDocParser\Ast\PhpDoc\DeprecatedTagValueNode;
1213
use PHPStan\PhpDocParser\Ast\PhpDoc\ExtendsTagValueNode;
1314
use PHPStan\PhpDocParser\Ast\PhpDoc\GenericTagValueNode;
@@ -75,6 +76,7 @@ protected function setUp(): void
7576
* @dataProvider provideExtendsTagsData
7677
* @dataProvider provideTypeAliasTagsData
7778
* @dataProvider provideTypeAliasImportTagsData
79+
* @dataProvider provideAssertTagsData
7880
* @dataProvider provideRealWorldExampleData
7981
* @dataProvider provideDescriptionWithOrWithoutHtml
8082
*/
@@ -3480,6 +3482,91 @@ public function provideTypeAliasImportTagsData(): Iterator
34803482
];
34813483
}
34823484

3485+
public function provideAssertTagsData(): Iterator
3486+
{
3487+
yield [
3488+
'OK',
3489+
'/** @phpstan-assert Type $var */',
3490+
new PhpDocNode([
3491+
new PhpDocTagNode(
3492+
'@phpstan-assert',
3493+
new AssertTagValueNode(
3494+
new IdentifierTypeNode('Type'),
3495+
'$var',
3496+
''
3497+
)
3498+
),
3499+
]),
3500+
];
3501+
3502+
yield [
3503+
'OK with psalm syntax',
3504+
'/** @psalm-assert Type $var */',
3505+
new PhpDocNode([
3506+
new PhpDocTagNode(
3507+
'@psalm-assert',
3508+
new AssertTagValueNode(
3509+
new IdentifierTypeNode('Type'),
3510+
'$var',
3511+
''
3512+
)
3513+
),
3514+
]),
3515+
];
3516+
3517+
yield [
3518+
'OK with description',
3519+
'/** @phpstan-assert Type $var assert Type to $var */',
3520+
new PhpDocNode([
3521+
new PhpDocTagNode(
3522+
'@phpstan-assert',
3523+
new AssertTagValueNode(
3524+
new IdentifierTypeNode('Type'),
3525+
'$var',
3526+
'assert Type to $var'
3527+
)
3528+
),
3529+
]),
3530+
];
3531+
3532+
yield [
3533+
'OK with union type',
3534+
'/** @phpstan-assert Type|Other $var */',
3535+
new PhpDocNode([
3536+
new PhpDocTagNode(
3537+
'@phpstan-assert',
3538+
new AssertTagValueNode(
3539+
new UnionTypeNode([
3540+
new IdentifierTypeNode('Type'),
3541+
new IdentifierTypeNode('Other'),
3542+
]),
3543+
'$var',
3544+
''
3545+
)
3546+
),
3547+
]),
3548+
];
3549+
3550+
yield [
3551+
'invalid $this->method()',
3552+
'/** @phpstan-assert Type $this->method() */',
3553+
new PhpDocNode([
3554+
new PhpDocTagNode(
3555+
'@phpstan-assert',
3556+
new InvalidTagValueNode(
3557+
'Type $this->method()',
3558+
new ParserException(
3559+
'$this',
3560+
Lexer::TOKEN_THIS_VARIABLE,
3561+
25,
3562+
Lexer::TOKEN_VARIABLE
3563+
)
3564+
)
3565+
),
3566+
]),
3567+
];
3568+
}
3569+
34833570
public function providerDebug(): Iterator
34843571
{
34853572
$sample = '/**

0 commit comments

Comments
(0)

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