Skip to content

Commit

Permalink
fix(RequireExplicitAssertion): do not throw away static type
Browse files Browse the repository at this point in the history
  • Loading branch information
simPod authored and kukulich committed Jul 16, 2022
1 parent 05ec182 commit fd5336f
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,10 @@ private function createConditions(string $variableName, TypeNode $typeNode): arr
return [sprintf('%s instanceof %s', $variableName, $typeNode->name)];
}

if ($typeNode->name === 'static') {
return [sprintf('%s instanceof static', $variableName)];
}

if (TypeHintHelper::isSimpleTypeHint($typeNode->name)) {
return [sprintf('\is_%s(%s)', $typeNode->name, $variableName)];
}
Expand Down
27 changes: 14 additions & 13 deletions tests/Sniffs/PHP/RequireExplicitAssertionSniffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function testErrors(): void
{
$report = self::checkFile(__DIR__ . '/data/requireExplicitAssertionErrors.php');

self::assertSame(28, $report->getErrorCount());
self::assertSame(29, $report->getErrorCount());

self::assertSniffError($report, 3, RequireExplicitAssertionSniff::CODE_REQUIRED_EXPLICIT_ASSERTION);
self::assertSniffError($report, 6, RequireExplicitAssertionSniff::CODE_REQUIRED_EXPLICIT_ASSERTION);
Expand All @@ -27,26 +27,27 @@ public function testErrors(): void
self::assertSniffError($report, 23, RequireExplicitAssertionSniff::CODE_REQUIRED_EXPLICIT_ASSERTION);
self::assertSniffError($report, 26, RequireExplicitAssertionSniff::CODE_REQUIRED_EXPLICIT_ASSERTION);
self::assertSniffError($report, 32, RequireExplicitAssertionSniff::CODE_REQUIRED_EXPLICIT_ASSERTION);
self::assertSniffError($report, 35, RequireExplicitAssertionSniff::CODE_REQUIRED_EXPLICIT_ASSERTION);
self::assertSniffError($report, 38, RequireExplicitAssertionSniff::CODE_REQUIRED_EXPLICIT_ASSERTION);
self::assertSniffError($report, 41, RequireExplicitAssertionSniff::CODE_REQUIRED_EXPLICIT_ASSERTION);
self::assertSniffError($report, 44, RequireExplicitAssertionSniff::CODE_REQUIRED_EXPLICIT_ASSERTION);
self::assertSniffError($report, 47, RequireExplicitAssertionSniff::CODE_REQUIRED_EXPLICIT_ASSERTION);
self::assertSniffError($report, 51, RequireExplicitAssertionSniff::CODE_REQUIRED_EXPLICIT_ASSERTION);
self::assertSniffError($report, 56, RequireExplicitAssertionSniff::CODE_REQUIRED_EXPLICIT_ASSERTION);
self::assertSniffError($report, 50, RequireExplicitAssertionSniff::CODE_REQUIRED_EXPLICIT_ASSERTION);
self::assertSniffError($report, 53, RequireExplicitAssertionSniff::CODE_REQUIRED_EXPLICIT_ASSERTION);
self::assertSniffError($report, 57, RequireExplicitAssertionSniff::CODE_REQUIRED_EXPLICIT_ASSERTION);
self::assertSniffError($report, 62, RequireExplicitAssertionSniff::CODE_REQUIRED_EXPLICIT_ASSERTION);
self::assertSniffError($report, 66, RequireExplicitAssertionSniff::CODE_REQUIRED_EXPLICIT_ASSERTION);
self::assertSniffError($report, 67, RequireExplicitAssertionSniff::CODE_REQUIRED_EXPLICIT_ASSERTION);
self::assertSniffError($report, 74, RequireExplicitAssertionSniff::CODE_REQUIRED_EXPLICIT_ASSERTION);
self::assertSniffError($report, 78, RequireExplicitAssertionSniff::CODE_REQUIRED_EXPLICIT_ASSERTION);
self::assertSniffError($report, 82, RequireExplicitAssertionSniff::CODE_REQUIRED_EXPLICIT_ASSERTION);
self::assertSniffError($report, 63, RequireExplicitAssertionSniff::CODE_REQUIRED_EXPLICIT_ASSERTION);
self::assertSniffError($report, 68, RequireExplicitAssertionSniff::CODE_REQUIRED_EXPLICIT_ASSERTION);
self::assertSniffError($report, 72, RequireExplicitAssertionSniff::CODE_REQUIRED_EXPLICIT_ASSERTION);
self::assertSniffError($report, 73, RequireExplicitAssertionSniff::CODE_REQUIRED_EXPLICIT_ASSERTION);
self::assertSniffError($report, 80, RequireExplicitAssertionSniff::CODE_REQUIRED_EXPLICIT_ASSERTION);
self::assertSniffError($report, 84, RequireExplicitAssertionSniff::CODE_REQUIRED_EXPLICIT_ASSERTION);
self::assertSniffError($report, 88, RequireExplicitAssertionSniff::CODE_REQUIRED_EXPLICIT_ASSERTION);
self::assertSniffError($report, 89, RequireExplicitAssertionSniff::CODE_REQUIRED_EXPLICIT_ASSERTION);
self::assertSniffError($report, 96, RequireExplicitAssertionSniff::CODE_REQUIRED_EXPLICIT_ASSERTION);
self::assertSniffError($report, 97, RequireExplicitAssertionSniff::CODE_REQUIRED_EXPLICIT_ASSERTION);
self::assertSniffError($report, 94, RequireExplicitAssertionSniff::CODE_REQUIRED_EXPLICIT_ASSERTION);
self::assertSniffError($report, 95, RequireExplicitAssertionSniff::CODE_REQUIRED_EXPLICIT_ASSERTION);
self::assertSniffError($report, 102, RequireExplicitAssertionSniff::CODE_REQUIRED_EXPLICIT_ASSERTION);
self::assertSniffError($report, 109, RequireExplicitAssertionSniff::CODE_REQUIRED_EXPLICIT_ASSERTION);
self::assertSniffError($report, 103, RequireExplicitAssertionSniff::CODE_REQUIRED_EXPLICIT_ASSERTION);
self::assertSniffError($report, 108, RequireExplicitAssertionSniff::CODE_REQUIRED_EXPLICIT_ASSERTION);
self::assertSniffError($report, 115, RequireExplicitAssertionSniff::CODE_REQUIRED_EXPLICIT_ASSERTION);

self::assertAllFixedInFile($report);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ public function __construct()
\assert($g instanceof self);
}

public function foo()
{
$x = call();
\assert($x instanceof static || $x === null);
}

}

$h = fopen('file.txt', 'r');
Expand Down
6 changes: 6 additions & 0 deletions tests/Sniffs/PHP/data/requireExplicitAssertionErrors.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ public function __construct()
$g = $this;
}

public function foo()
{
/** @var static|null $x */
$x = call();
}

}

/** @var resource $h */
Expand Down

0 comments on commit fd5336f

Please sign in to comment.