Skip to content

Commit 971f489

Browse files
committed
SlevomatCodingStandard.PHP.RequireExplicitAssertionSniff: Fixed broken fixer when "enableAdvancedStringTypes" is enabled
1 parent a52720f commit 971f489

File tree

4 files changed

+13
-3
lines changed

4 files changed

+13
-3
lines changed

SlevomatCodingStandard/Sniffs/PHP/RequireExplicitAssertionSniff.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -443,14 +443,17 @@ private function createConditions(string $variableName, TypeNode $typeNode): arr
443443
}
444444
}
445445

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

449452
if ($typeNode->name === 'non-empty-string') {
450453
$conditions[] = sprintf("%s !== ''", $variableName);
451454
} elseif ($typeNode->name === 'callable-string') {
452455
$conditions[] = sprintf('\is_callable(%s)', $variableName);
453-
} elseif ($typeNode->name === 'numeric-string') {
456+
} else {
454457
$conditions[] = sprintf('\is_numeric(%s)', $variableName);
455458
}
456459

tests/Sniffs/PHP/RequireExplicitAssertionSniffTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,12 @@ public function testAdvancedStringTypesErrors(): void
9999
'enableAdvancedStringTypes' => true,
100100
]);
101101

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

104104
self::assertSniffError($report, 3, RequireExplicitAssertionSniff::CODE_REQUIRED_EXPLICIT_ASSERTION);
105105
self::assertSniffError($report, 6, RequireExplicitAssertionSniff::CODE_REQUIRED_EXPLICIT_ASSERTION);
106106
self::assertSniffError($report, 9, RequireExplicitAssertionSniff::CODE_REQUIRED_EXPLICIT_ASSERTION);
107+
self::assertSniffError($report, 12, RequireExplicitAssertionSniff::CODE_REQUIRED_EXPLICIT_ASSERTION);
107108

108109
self::assertAllFixedInFile($report);
109110
}

tests/Sniffs/PHP/data/requireExplicitAssertionAdvancedStringTypesErrors.fixed.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,6 @@
88

99
$c = '100';
1010
\assert(\is_string($c) && \is_numeric($c));
11+
12+
$d = null;
13+
\assert($d instanceof SomeClass || $d === null);

tests/Sniffs/PHP/data/requireExplicitAssertionAdvancedStringTypesErrors.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,6 @@
88

99
/** @var numeric-string $c */
1010
$c = '100';
11+
12+
/** @var SomeClass|null $d */
13+
$d = null;

0 commit comments

Comments
 (0)