Skip to content
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/Rules/PhpDoc/AssertRuleHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PHPStan\Rules\PhpDoc;

use PHPStan\PhpDoc\Tag\AssertTag;
use PHPStan\Node\Expr\TypeExpr;
use PHPStan\Reflection\ExtendedMethodReflection;
use PHPStan\Reflection\FunctionReflection;
Expand Down Expand Up @@ -55,6 +56,22 @@ public function check(ExtendedMethodReflection|FunctionReflection $reflection, P
continue;
}

if ($assert->getType() instanceof ErrorType) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Call UnresolvableTypeHelper instead please. You should follow the code samples I sent you :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also please add all the other things that should be checked too.

$tagName = [
AssertTag::NULL => '@phpstan-assert',
AssertTag::IF_TRUE => '@phpstan-assert-if-true',
AssertTag::IF_FALSE => '@phpstan-assert-if-false',
][$assert->getIf()];

$errors[] = RuleErrorBuilder::message(sprintf(
'PHPDoc tag %s for parameter $%s contains unresolvable type.',
$tagName,
$parameterName,
))->identifier('parameter.unresolvableType')->build();

continue;
}

$assertedExpr = $assert->getParameter()->getExpr(new TypeExpr($parametersByName[$parameterName]));
$assertedExprType = $this->initializerExprTypeResolver->getType($assertedExpr, $context);
if ($assertedExprType instanceof ErrorType) {
Expand Down
12 changes: 12 additions & 0 deletions tests/PHPStan/Rules/PhpDoc/FunctionAssertRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,18 @@ public function testRule(): void
'Asserted negated type string for $i with type int does not narrow down the type.',
70,
],
[
'PHPDoc tag @phpstan-assert for parameter $str contains unresolvable type.',
83,
],
[
'PHPDoc tag @phpstan-assert-if-true for parameter $str contains unresolvable type.',
90,
],
[
'PHPDoc tag @phpstan-assert-if-false for parameter $str contains unresolvable type.',
97,
],
]);
}

Expand Down
12 changes: 12 additions & 0 deletions tests/PHPStan/Rules/PhpDoc/MethodAssertRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,18 @@ public function testRule(): void
'Asserted negated type string for $i with type int does not narrow down the type.',
72,
],
[
'PHPDoc tag @phpstan-assert for parameter $str contains unresolvable type.',
86,
],
[
'PHPDoc tag @phpstan-assert-if-true for parameter $str contains unresolvable type.',
93,
],
[
'PHPDoc tag @phpstan-assert-if-false for parameter $str contains unresolvable type.',
100,
],
]);
}

Expand Down
21 changes: 21 additions & 0 deletions tests/PHPStan/Rules/PhpDoc/data/function-assert.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,24 @@ function negate1(int $i): void
function negate2(int $i): void
{
}

/**
* @phpstan-assert empty-str $str
*/
function unresolvableAssert(string $str): void
{
}

/**
* @phpstan-assert-if-true empty-str $str
*/
function unresolvableAssertIfTrue(string $str): void
{
}

/**
* @phpstan-assert-if-false empty-str $str
*/
function unresolvableAssertIfFalse(string $str): void
{
}
22 changes: 22 additions & 0 deletions tests/PHPStan/Rules/PhpDoc/data/method-assert.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,26 @@ public function negate1(int $i): void
public function negate2(int $i): void
{
}

/**
* @phpstan-assert empty-str $str
*/
public function unresolvableAssert(string $str): void
{
}

/**
* @phpstan-assert-if-true empty-str $str
*/
public function unresolvableAssertIfTrue(string $str): void
{
}

/**
* @phpstan-assert-if-false empty-str $str
*/
public function unresolvableAssertIfFalse(string $str): void
{
}

}
Loading