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 33aefcd

Browse files
rvanvelzenondrejmirtes
authored andcommitted
Add equality assert syntax
1 parent ae85d4b commit 33aefcd

File tree

5 files changed

+59
-9
lines changed

5 files changed

+59
-9
lines changed

‎src/Ast/PhpDoc/AssertTagMethodValueNode.php‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,28 @@ class AssertTagMethodValueNode implements PhpDocTagValueNode
2323
/** @var bool */
2424
public $isNegated;
2525

26+
/** @var bool */
27+
public $isEquality;
28+
2629
/** @var string (may be empty) */
2730
public $description;
2831

29-
public function __construct(TypeNode $type, string $parameter, string $method, bool $isNegated, string $description)
32+
public function __construct(TypeNode $type, string $parameter, string $method, bool $isNegated, string $description, bool$isEquality = false)
3033
{
3134
$this->type = $type;
3235
$this->parameter = $parameter;
3336
$this->method = $method;
3437
$this->isNegated = $isNegated;
38+
$this->isEquality = $isEquality;
3539
$this->description = $description;
3640
}
3741

3842

3943
public function __toString(): string
4044
{
4145
$isNegated = $this->isNegated ? '!' : '';
42-
return trim("{$isNegated}{$this->type}{$this->parameter}->{$this->method}() {$this->description}");
46+
$isEquality = $this->isEquality ? '=' : '';
47+
return trim("{$isNegated}{$isEquality}{$this->type}{$this->parameter}->{$this->method}() {$this->description}");
4348
}
4449

4550
}

‎src/Ast/PhpDoc/AssertTagPropertyValueNode.php‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,28 @@ class AssertTagPropertyValueNode implements PhpDocTagValueNode
2323
/** @var bool */
2424
public $isNegated;
2525

26+
/** @var bool */
27+
public $isEquality;
28+
2629
/** @var string (may be empty) */
2730
public $description;
2831

29-
public function __construct(TypeNode $type, string $parameter, string $property, bool $isNegated, string $description)
32+
public function __construct(TypeNode $type, string $parameter, string $property, bool $isNegated, string $description, bool$isEquality = false)
3033
{
3134
$this->type = $type;
3235
$this->parameter = $parameter;
3336
$this->property = $property;
3437
$this->isNegated = $isNegated;
38+
$this->isEquality = $isEquality;
3539
$this->description = $description;
3640
}
3741

3842

3943
public function __toString(): string
4044
{
4145
$isNegated = $this->isNegated ? '!' : '';
42-
return trim("{$isNegated}{$this->type}{$this->parameter}->{$this->property}{$this->description}");
46+
$isEquality = $this->isEquality ? '=' : '';
47+
return trim("{$isNegated}{$isEquality}{$this->type}{$this->parameter}->{$this->property}{$this->description}");
4348
}
4449

4550
}

‎src/Ast/PhpDoc/AssertTagValueNode.php‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,27 @@ class AssertTagValueNode implements PhpDocTagValueNode
2020
/** @var bool */
2121
public $isNegated;
2222

23+
/** @var bool */
24+
public $isEquality;
25+
2326
/** @var string (may be empty) */
2427
public $description;
2528

26-
public function __construct(TypeNode $type, string $parameter, bool $isNegated, string $description)
29+
public function __construct(TypeNode $type, string $parameter, bool $isNegated, string $description, bool$isEquality = false)
2730
{
2831
$this->type = $type;
2932
$this->parameter = $parameter;
3033
$this->isNegated = $isNegated;
34+
$this->isEquality = $isEquality;
3135
$this->description = $description;
3236
}
3337

3438

3539
public function __toString(): string
3640
{
3741
$isNegated = $this->isNegated ? '!' : '';
38-
return trim("{$isNegated}{$this->type}{$this->parameter}{$this->description}");
42+
$isEquality = $this->isEquality ? '=' : '';
43+
return trim("{$isNegated}{$isEquality}{$this->type}{$this->parameter}{$this->description}");
3944
}
4045

4146
}

‎src/Parser/PhpDocParser.php‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -472,17 +472,18 @@ private function parseTypeAliasImportTagValue(TokenIterator $tokens): Ast\PhpDoc
472472
private function parseAssertTagValue(TokenIterator $tokens): Ast\PhpDoc\PhpDocTagValueNode
473473
{
474474
$isNegated = $tokens->tryConsumeTokenType(Lexer::TOKEN_NEGATED);
475+
$isEquality = $tokens->tryConsumeTokenType(Lexer::TOKEN_EQUAL);
475476
$type = $this->typeParser->parse($tokens);
476477
$parameter = $this->parseAssertParameter($tokens);
477478
$description = $this->parseOptionalDescription($tokens);
478479

479480
if (array_key_exists('method', $parameter)) {
480-
return new Ast\PhpDoc\AssertTagMethodValueNode($type, $parameter['parameter'], $parameter['method'], $isNegated, $description);
481+
return new Ast\PhpDoc\AssertTagMethodValueNode($type, $parameter['parameter'], $parameter['method'], $isNegated, $description, $isEquality);
481482
} elseif (array_key_exists('property', $parameter)) {
482-
return new Ast\PhpDoc\AssertTagPropertyValueNode($type, $parameter['parameter'], $parameter['property'], $isNegated, $description);
483+
return new Ast\PhpDoc\AssertTagPropertyValueNode($type, $parameter['parameter'], $parameter['property'], $isNegated, $description, $isEquality);
483484
}
484485

485-
return new Ast\PhpDoc\AssertTagValueNode($type, $parameter['parameter'], $isNegated, $description);
486+
return new Ast\PhpDoc\AssertTagValueNode($type, $parameter['parameter'], $isNegated, $description, $isEquality);
486487
}
487488

488489
/**

‎tests/PHPStan/Parser/PhpDocParserTest.php‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4002,6 +4002,40 @@ public function provideAssertTagsData(): Iterator
40024002
),
40034003
]),
40044004
];
4005+
4006+
yield [
4007+
'OK equality',
4008+
'/** @phpstan-assert =Type $var */',
4009+
new PhpDocNode([
4010+
new PhpDocTagNode(
4011+
'@phpstan-assert',
4012+
new AssertTagValueNode(
4013+
new IdentifierTypeNode('Type'),
4014+
'$var',
4015+
false,
4016+
'',
4017+
true
4018+
)
4019+
),
4020+
]),
4021+
];
4022+
4023+
yield [
4024+
'OK negated equality',
4025+
'/** @phpstan-assert !=Type $var */',
4026+
new PhpDocNode([
4027+
new PhpDocTagNode(
4028+
'@phpstan-assert',
4029+
new AssertTagValueNode(
4030+
new IdentifierTypeNode('Type'),
4031+
'$var',
4032+
true,
4033+
'',
4034+
true
4035+
)
4036+
),
4037+
]),
4038+
];
40054039
}
40064040

40074041
public function providerDebug(): Iterator

0 commit comments

Comments
(0)

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