Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
24 changes: 22 additions & 2 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 All @@ -20,7 +21,10 @@
final class AssertRuleHelper
{

public function __construct(private InitializerExprTypeResolver $initializerExprTypeResolver)
public function __construct(
private InitializerExprTypeResolver $initializerExprTypeResolver,
private UnresolvableTypeHelper $unresolvableTypeHelper,
)
{
}

Expand Down Expand Up @@ -55,9 +59,25 @@ public function check(ExtendedMethodReflection|FunctionReflection $reflection, P
continue;
}

if ($this->unresolvableTypeHelper->containsUnresolvableType($assert->getType())) {
$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) {
if ($this->unresolvableTypeHelper->containsUnresolvableType($assertedExprType)) {
continue;
}

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 @@ -15,7 +15,7 @@
protected function getRule(): Rule
{
$initializerExprTypeResolver = self::getContainer()->getByType(InitializerExprTypeResolver::class);
return new FunctionAssertRule(new AssertRuleHelper($initializerExprTypeResolver));

Check failure on line 18 in tests/PHPStan/Rules/PhpDoc/FunctionAssertRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.2)

Class PHPStan\Rules\PhpDoc\AssertRuleHelper constructor invoked with 1 parameter, 2 required.

Check failure on line 18 in tests/PHPStan/Rules/PhpDoc/FunctionAssertRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.4)

Class PHPStan\Rules\PhpDoc\AssertRuleHelper constructor invoked with 1 parameter, 2 required.

Check failure on line 18 in tests/PHPStan/Rules/PhpDoc/FunctionAssertRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.3)

Class PHPStan\Rules\PhpDoc\AssertRuleHelper constructor invoked with 1 parameter, 2 required.

Check failure on line 18 in tests/PHPStan/Rules/PhpDoc/FunctionAssertRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, ubuntu-latest)

Class PHPStan\Rules\PhpDoc\AssertRuleHelper constructor invoked with 1 parameter, 2 required.

Check failure on line 18 in tests/PHPStan/Rules/PhpDoc/FunctionAssertRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.4, ubuntu-latest)

Class PHPStan\Rules\PhpDoc\AssertRuleHelper constructor invoked with 1 parameter, 2 required.

Check failure on line 18 in tests/PHPStan/Rules/PhpDoc/FunctionAssertRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, ubuntu-latest)

Class PHPStan\Rules\PhpDoc\AssertRuleHelper constructor invoked with 1 parameter, 2 required.

Check failure on line 18 in tests/PHPStan/Rules/PhpDoc/FunctionAssertRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.1)

Class PHPStan\Rules\PhpDoc\AssertRuleHelper constructor invoked with 1 parameter, 2 required.

Check failure on line 18 in tests/PHPStan/Rules/PhpDoc/FunctionAssertRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, ubuntu-latest)

Class PHPStan\Rules\PhpDoc\AssertRuleHelper constructor invoked with 1 parameter, 2 required.

Check failure on line 18 in tests/PHPStan/Rules/PhpDoc/FunctionAssertRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, ubuntu-latest)

Class PHPStan\Rules\PhpDoc\AssertRuleHelper constructor invoked with 1 parameter, 2 required.

Check failure on line 18 in tests/PHPStan/Rules/PhpDoc/FunctionAssertRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, ubuntu-latest)

Class PHPStan\Rules\PhpDoc\AssertRuleHelper constructor invoked with 1 parameter, 2 required.

Check failure on line 18 in tests/PHPStan/Rules/PhpDoc/FunctionAssertRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.3, ubuntu-latest)

Class PHPStan\Rules\PhpDoc\AssertRuleHelper constructor invoked with 1 parameter, 2 required.

Check failure on line 18 in tests/PHPStan/Rules/PhpDoc/FunctionAssertRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.2, ubuntu-latest)

Class PHPStan\Rules\PhpDoc\AssertRuleHelper constructor invoked with 1 parameter, 2 required.

Check failure on line 18 in tests/PHPStan/Rules/PhpDoc/FunctionAssertRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, windows-latest)

Class PHPStan\Rules\PhpDoc\AssertRuleHelper constructor invoked with 1 parameter, 2 required.

Check failure on line 18 in tests/PHPStan/Rules/PhpDoc/FunctionAssertRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.4, windows-latest)

Class PHPStan\Rules\PhpDoc\AssertRuleHelper constructor invoked with 1 parameter, 2 required.

Check failure on line 18 in tests/PHPStan/Rules/PhpDoc/FunctionAssertRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, windows-latest)

Class PHPStan\Rules\PhpDoc\AssertRuleHelper constructor invoked with 1 parameter, 2 required.

Check failure on line 18 in tests/PHPStan/Rules/PhpDoc/FunctionAssertRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, windows-latest)

Class PHPStan\Rules\PhpDoc\AssertRuleHelper constructor invoked with 1 parameter, 2 required.

Check failure on line 18 in tests/PHPStan/Rules/PhpDoc/FunctionAssertRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, windows-latest)

Class PHPStan\Rules\PhpDoc\AssertRuleHelper constructor invoked with 1 parameter, 2 required.

Check failure on line 18 in tests/PHPStan/Rules/PhpDoc/FunctionAssertRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, windows-latest)

Class PHPStan\Rules\PhpDoc\AssertRuleHelper constructor invoked with 1 parameter, 2 required.

Check failure on line 18 in tests/PHPStan/Rules/PhpDoc/FunctionAssertRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.3, windows-latest)

Class PHPStan\Rules\PhpDoc\AssertRuleHelper constructor invoked with 1 parameter, 2 required.

Check failure on line 18 in tests/PHPStan/Rules/PhpDoc/FunctionAssertRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.2, windows-latest)

Class PHPStan\Rules\PhpDoc\AssertRuleHelper constructor invoked with 1 parameter, 2 required.
}

public function testRule(): void
Expand Down Expand Up @@ -54,6 +54,18 @@
'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 @@ -15,7 +15,7 @@
protected function getRule(): Rule
{
$initializerExprTypeResolver = self::getContainer()->getByType(InitializerExprTypeResolver::class);
return new MethodAssertRule(new AssertRuleHelper($initializerExprTypeResolver));

Check failure on line 18 in tests/PHPStan/Rules/PhpDoc/MethodAssertRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.2)

Class PHPStan\Rules\PhpDoc\AssertRuleHelper constructor invoked with 1 parameter, 2 required.

Check failure on line 18 in tests/PHPStan/Rules/PhpDoc/MethodAssertRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.4)

Class PHPStan\Rules\PhpDoc\AssertRuleHelper constructor invoked with 1 parameter, 2 required.

Check failure on line 18 in tests/PHPStan/Rules/PhpDoc/MethodAssertRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.3)

Class PHPStan\Rules\PhpDoc\AssertRuleHelper constructor invoked with 1 parameter, 2 required.

Check failure on line 18 in tests/PHPStan/Rules/PhpDoc/MethodAssertRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, ubuntu-latest)

Class PHPStan\Rules\PhpDoc\AssertRuleHelper constructor invoked with 1 parameter, 2 required.

Check failure on line 18 in tests/PHPStan/Rules/PhpDoc/MethodAssertRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.4, ubuntu-latest)

Class PHPStan\Rules\PhpDoc\AssertRuleHelper constructor invoked with 1 parameter, 2 required.

Check failure on line 18 in tests/PHPStan/Rules/PhpDoc/MethodAssertRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, ubuntu-latest)

Class PHPStan\Rules\PhpDoc\AssertRuleHelper constructor invoked with 1 parameter, 2 required.

Check failure on line 18 in tests/PHPStan/Rules/PhpDoc/MethodAssertRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.1)

Class PHPStan\Rules\PhpDoc\AssertRuleHelper constructor invoked with 1 parameter, 2 required.

Check failure on line 18 in tests/PHPStan/Rules/PhpDoc/MethodAssertRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, ubuntu-latest)

Class PHPStan\Rules\PhpDoc\AssertRuleHelper constructor invoked with 1 parameter, 2 required.

Check failure on line 18 in tests/PHPStan/Rules/PhpDoc/MethodAssertRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, ubuntu-latest)

Class PHPStan\Rules\PhpDoc\AssertRuleHelper constructor invoked with 1 parameter, 2 required.

Check failure on line 18 in tests/PHPStan/Rules/PhpDoc/MethodAssertRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, ubuntu-latest)

Class PHPStan\Rules\PhpDoc\AssertRuleHelper constructor invoked with 1 parameter, 2 required.

Check failure on line 18 in tests/PHPStan/Rules/PhpDoc/MethodAssertRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.3, ubuntu-latest)

Class PHPStan\Rules\PhpDoc\AssertRuleHelper constructor invoked with 1 parameter, 2 required.

Check failure on line 18 in tests/PHPStan/Rules/PhpDoc/MethodAssertRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.2, ubuntu-latest)

Class PHPStan\Rules\PhpDoc\AssertRuleHelper constructor invoked with 1 parameter, 2 required.

Check failure on line 18 in tests/PHPStan/Rules/PhpDoc/MethodAssertRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, windows-latest)

Class PHPStan\Rules\PhpDoc\AssertRuleHelper constructor invoked with 1 parameter, 2 required.

Check failure on line 18 in tests/PHPStan/Rules/PhpDoc/MethodAssertRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.4, windows-latest)

Class PHPStan\Rules\PhpDoc\AssertRuleHelper constructor invoked with 1 parameter, 2 required.

Check failure on line 18 in tests/PHPStan/Rules/PhpDoc/MethodAssertRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, windows-latest)

Class PHPStan\Rules\PhpDoc\AssertRuleHelper constructor invoked with 1 parameter, 2 required.

Check failure on line 18 in tests/PHPStan/Rules/PhpDoc/MethodAssertRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, windows-latest)

Class PHPStan\Rules\PhpDoc\AssertRuleHelper constructor invoked with 1 parameter, 2 required.

Check failure on line 18 in tests/PHPStan/Rules/PhpDoc/MethodAssertRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, windows-latest)

Class PHPStan\Rules\PhpDoc\AssertRuleHelper constructor invoked with 1 parameter, 2 required.

Check failure on line 18 in tests/PHPStan/Rules/PhpDoc/MethodAssertRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, windows-latest)

Class PHPStan\Rules\PhpDoc\AssertRuleHelper constructor invoked with 1 parameter, 2 required.

Check failure on line 18 in tests/PHPStan/Rules/PhpDoc/MethodAssertRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.3, windows-latest)

Class PHPStan\Rules\PhpDoc\AssertRuleHelper constructor invoked with 1 parameter, 2 required.

Check failure on line 18 in tests/PHPStan/Rules/PhpDoc/MethodAssertRuleTest.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.2, windows-latest)

Class PHPStan\Rules\PhpDoc\AssertRuleHelper constructor invoked with 1 parameter, 2 required.
}

public function testRule(): void
Expand Down Expand Up @@ -54,6 +54,18 @@
'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