-
Notifications
You must be signed in to change notification settings - Fork 65
Add assert syntax #120
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add assert syntax #120
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\PhpDocParser\Ast\PhpDoc; | ||
|
||
use PHPStan\PhpDocParser\Ast\NodeAttributes; | ||
use PHPStan\PhpDocParser\Ast\Type\TypeNode; | ||
use function trim; | ||
|
||
class AssertTagValueNode implements PhpDocTagValueNode | ||
{ | ||
|
||
use NodeAttributes; | ||
|
||
/** @var TypeNode */ | ||
public $type; | ||
|
||
/** @var string */ | ||
public $parameter; | ||
|
||
/** @var string (may be empty) */ | ||
public $description; | ||
|
||
public function __construct(TypeNode $type, string $parameter, string $description) | ||
{ | ||
$this->type = $type; | ||
$this->parameter = $parameter; | ||
$this->description = $description; | ||
} | ||
|
||
|
||
public function __toString(): string | ||
{ | ||
return trim("{$this->type} {$this->parameter} {$this->description}"); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprStringNode; | ||
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstFetchNode; | ||
use PHPStan\PhpDocParser\Ast\Node; | ||
use PHPStan\PhpDocParser\Ast\PhpDoc\AssertTagValueNode; | ||
use PHPStan\PhpDocParser\Ast\PhpDoc\DeprecatedTagValueNode; | ||
use PHPStan\PhpDocParser\Ast\PhpDoc\ExtendsTagValueNode; | ||
use PHPStan\PhpDocParser\Ast\PhpDoc\GenericTagValueNode; | ||
|
@@ -75,6 +76,7 @@ protected function setUp(): void | |
* @dataProvider provideExtendsTagsData | ||
* @dataProvider provideTypeAliasTagsData | ||
* @dataProvider provideTypeAliasImportTagsData | ||
* @dataProvider provideAssertTagsData | ||
* @dataProvider provideRealWorldExampleData | ||
* @dataProvider provideDescriptionWithOrWithoutHtml | ||
*/ | ||
|
@@ -3480,6 +3482,91 @@ public function provideTypeAliasImportTagsData(): Iterator | |
]; | ||
} | ||
|
||
public function provideAssertTagsData(): Iterator | ||
{ | ||
yield [ | ||
'OK', | ||
'/** @phpstan-assert Type $var */', | ||
new PhpDocNode([ | ||
new PhpDocTagNode( | ||
'@phpstan-assert', | ||
new AssertTagValueNode( | ||
new IdentifierTypeNode('Type'), | ||
'$var', | ||
'' | ||
) | ||
), | ||
]), | ||
]; | ||
|
||
yield [ | ||
'OK with psalm syntax', | ||
'/** @psalm-assert Type $var */', | ||
new PhpDocNode([ | ||
new PhpDocTagNode( | ||
'@psalm-assert', | ||
new AssertTagValueNode( | ||
new IdentifierTypeNode('Type'), | ||
'$var', | ||
'' | ||
) | ||
), | ||
]), | ||
]; | ||
|
||
yield [ | ||
'OK with description', | ||
'/** @phpstan-assert Type $var assert Type to $var */', | ||
new PhpDocNode([ | ||
new PhpDocTagNode( | ||
'@phpstan-assert', | ||
new AssertTagValueNode( | ||
new IdentifierTypeNode('Type'), | ||
'$var', | ||
'assert Type to $var' | ||
) | ||
), | ||
]), | ||
]; | ||
|
||
yield [ | ||
'OK with union type', | ||
'/** @phpstan-assert Type|Other $var */', | ||
new PhpDocNode([ | ||
new PhpDocTagNode( | ||
'@phpstan-assert', | ||
new AssertTagValueNode( | ||
new UnionTypeNode([ | ||
new IdentifierTypeNode('Type'), | ||
new IdentifierTypeNode('Other'), | ||
]), | ||
'$var', | ||
'' | ||
) | ||
), | ||
]), | ||
]; | ||
|
||
yield [ | ||
'invalid $this->method()', | ||
'/** @phpstan-assert Type $this->method() */', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This would be a nice future addition
|
||
new PhpDocNode([ | ||
new PhpDocTagNode( | ||
'@phpstan-assert', | ||
new InvalidTagValueNode( | ||
'Type $this->method()', | ||
new ParserException( | ||
'$this', | ||
Lexer::TOKEN_THIS_VARIABLE, | ||
25, | ||
Lexer::TOKEN_VARIABLE | ||
) | ||
) | ||
), | ||
]), | ||
]; | ||
} | ||
|
||
public function providerDebug(): Iterator | ||
{ | ||
$sample = '/** | ||
|