Skip to content

Commit 3510b0a

Browse files
ekisuondrejmirtes
authored andcommitted
Allow asserting the type of $this
1 parent e540adc commit 3510b0a

File tree

2 files changed

+47
-14
lines changed

2 files changed

+47
-14
lines changed

src/Parser/PhpDocParser.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -1127,15 +1127,13 @@ private function parseAssertParameter(TokenIterator $tokens): array
11271127
{
11281128
if ($tokens->isCurrentTokenType(Lexer::TOKEN_THIS_VARIABLE)) {
11291129
$parameter = '$this';
1130-
$requirePropertyOrMethod = true;
11311130
$tokens->next();
11321131
} else {
11331132
$parameter = $tokens->currentTokenValue();
1134-
$requirePropertyOrMethod = false;
11351133
$tokens->consumeTokenType(Lexer::TOKEN_VARIABLE);
11361134
}
11371135

1138-
if ($requirePropertyOrMethod || $tokens->isCurrentTokenType(Lexer::TOKEN_ARROW)) {
1136+
if ($tokens->isCurrentTokenType(Lexer::TOKEN_ARROW)) {
11391137
$tokens->consumeTokenType(Lexer::TOKEN_ARROW);
11401138

11411139
$propertyOrMethod = $tokens->currentTokenValue();

tests/PHPStan/Parser/PhpDocParserTest.php

+46-11
Original file line numberDiff line numberDiff line change
@@ -4563,21 +4563,56 @@ public function provideAssertTagsData(): Iterator
45634563
];
45644564

45654565
yield [
4566-
'invalid $this',
4566+
'OK $this',
45674567
'/** @phpstan-assert Type $this */',
45684568
new PhpDocNode([
45694569
new PhpDocTagNode(
45704570
'@phpstan-assert',
4571-
new InvalidTagValueNode(
4572-
'Type $this',
4573-
new ParserException(
4574-
'*/',
4575-
Lexer::TOKEN_CLOSE_PHPDOC,
4576-
31,
4577-
Lexer::TOKEN_ARROW,
4578-
null,
4579-
1
4580-
)
4571+
new AssertTagValueNode(
4572+
new IdentifierTypeNode('Type'),
4573+
'$this',
4574+
false,
4575+
''
4576+
)
4577+
),
4578+
]),
4579+
];
4580+
4581+
yield [
4582+
'OK $this with description',
4583+
'/** @phpstan-assert Type $this assert Type to $this */',
4584+
new PhpDocNode([
4585+
new PhpDocTagNode(
4586+
'@phpstan-assert',
4587+
new AssertTagValueNode(
4588+
new IdentifierTypeNode('Type'),
4589+
'$this',
4590+
false,
4591+
'assert Type to $this'
4592+
)
4593+
),
4594+
]),
4595+
];
4596+
4597+
yield [
4598+
'OK $this with generic type',
4599+
'/** @phpstan-assert GenericType<T> $this */',
4600+
new PhpDocNode([
4601+
new PhpDocTagNode(
4602+
'@phpstan-assert',
4603+
new AssertTagValueNode(
4604+
new GenericTypeNode(
4605+
new IdentifierTypeNode('GenericType'),
4606+
[
4607+
new IdentifierTypeNode('T'),
4608+
],
4609+
[
4610+
GenericTypeNode::VARIANCE_INVARIANT,
4611+
]
4612+
),
4613+
'$this',
4614+
false,
4615+
''
45814616
)
45824617
),
45834618
]),

0 commit comments

Comments
 (0)