Skip to content

Commit

Permalink
SlevomatCodingStandard.PHP.RequireExplicitAssertionSniff: Fixed broke…
Browse files Browse the repository at this point in the history
…n fixer when "enableAdvancedStringTypes" is enabled
  • Loading branch information
kukulich committed Sep 23, 2022
1 parent a52720f commit 971f489
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 3 deletions.
Expand Up @@ -443,14 +443,17 @@ private function createConditions(string $variableName, TypeNode $typeNode): arr
}
}

if ($this->enableAdvancedStringTypes) {
if (
$this->enableAdvancedStringTypes
&& in_array($typeNode->name, ['non-empty-string', 'callable-string', 'numeric-string'], true)
) {
$conditions = [sprintf('\is_string(%s)', $variableName)];

if ($typeNode->name === 'non-empty-string') {
$conditions[] = sprintf("%s !== ''", $variableName);
} elseif ($typeNode->name === 'callable-string') {
$conditions[] = sprintf('\is_callable(%s)', $variableName);
} elseif ($typeNode->name === 'numeric-string') {
} else {
$conditions[] = sprintf('\is_numeric(%s)', $variableName);
}

Expand Down
3 changes: 2 additions & 1 deletion tests/Sniffs/PHP/RequireExplicitAssertionSniffTest.php
Expand Up @@ -99,11 +99,12 @@ public function testAdvancedStringTypesErrors(): void
'enableAdvancedStringTypes' => true,
]);

self::assertSame(3, $report->getErrorCount());
self::assertSame(4, $report->getErrorCount());

self::assertSniffError($report, 3, RequireExplicitAssertionSniff::CODE_REQUIRED_EXPLICIT_ASSERTION);
self::assertSniffError($report, 6, RequireExplicitAssertionSniff::CODE_REQUIRED_EXPLICIT_ASSERTION);
self::assertSniffError($report, 9, RequireExplicitAssertionSniff::CODE_REQUIRED_EXPLICIT_ASSERTION);
self::assertSniffError($report, 12, RequireExplicitAssertionSniff::CODE_REQUIRED_EXPLICIT_ASSERTION);

self::assertAllFixedInFile($report);
}
Expand Down
Expand Up @@ -8,3 +8,6 @@

$c = '100';
\assert(\is_string($c) && \is_numeric($c));

$d = null;
\assert($d instanceof SomeClass || $d === null);
Expand Up @@ -8,3 +8,6 @@

/** @var numeric-string $c */
$c = '100';

/** @var SomeClass|null $d */
$d = null;

0 comments on commit 971f489

Please sign in to comment.