Skip to content

Commit d106813

Browse files
committed
StrictCallSniff: Fixed false positive
1 parent 6de1e23 commit d106813

File tree

4 files changed

+28
-5
lines changed

4 files changed

+28
-5
lines changed

SlevomatCodingStandard/Sniffs/Functions/StrictCallSniff.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,19 @@ public function process(File $phpcsFile, $stringPointer): void
8484
}
8585
}
8686

87-
$parametersCount = count($commaPointers) + 1;
87+
$commaPointersCount = count($commaPointers);
88+
89+
$parametersCount = $commaPointersCount + 1;
90+
$lastCommaPointer = $commaPointersCount > 0 ? $commaPointers[$commaPointersCount - 1] : null;
91+
92+
if (
93+
$lastCommaPointer !== null
94+
&& TokenHelper::findNextEffective($phpcsFile, $lastCommaPointer + 1, $tokens[$parenthesisOpenerPointer]['parenthesis_closer']) === null
95+
) {
96+
$parametersCount--;
97+
}
8898

8999
if ($parametersCount === self::FUNCTIONS[$functionName]) {
90-
$lastCommaPointer = $commaPointers[count($commaPointers) - 1];
91100

92101
$strictParameterValue = strtolower(trim(TokenHelper::getContent($phpcsFile, $lastCommaPointer + 1, $tokens[$parenthesisOpenerPointer]['parenthesis_closer'] - 1)));
93102

tests/Sniffs/Functions/StrictCallSniffTest.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@ public function testErrors(): void
1717
{
1818
$report = self::checkFile(__DIR__ . '/data/strictCallErrors.php');
1919

20-
self::assertSame(4, $report->getErrorCount());
20+
self::assertSame(6, $report->getErrorCount());
2121

2222
self::assertSniffError($report, 3, StrictCallSniff::CODE_STRICT_PARAMETER_MISSING);
2323
self::assertSniffError($report, 4, StrictCallSniff::CODE_STRICT_PARAMETER_MISSING);
24+
self::assertSniffError($report, 5, StrictCallSniff::CODE_STRICT_PARAMETER_MISSING);
2425

25-
self::assertSniffError($report, 6, StrictCallSniff::CODE_NON_STRICT_COMPARISON);
26-
self::assertSniffError($report, 7, StrictCallSniff::CODE_NON_STRICT_COMPARISON);
26+
self::assertSniffError($report, 10, StrictCallSniff::CODE_NON_STRICT_COMPARISON);
27+
self::assertSniffError($report, 11, StrictCallSniff::CODE_NON_STRICT_COMPARISON);
28+
self::assertSniffError($report, 16, StrictCallSniff::CODE_NON_STRICT_COMPARISON);
2729
}
2830

2931
}

tests/Sniffs/Functions/data/strictCallErrors.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@
22

33
array_search(0, [1, 2]);
44
array_keys([], 0);
5+
array_keys(
6+
[],
7+
0,
8+
);
59

610
in_array(0, [], false);
11+
in_array(
12+
0,
13+
[],
14+
false,
15+
);
716
base64_decode('', false);

tests/Sniffs/Functions/data/strictCallNoErrors.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,6 @@ public static function array_search($a, $b)
3535

3636
in_array(0);
3737
array_keys([]);
38+
array_keys(
39+
[],
40+
);

0 commit comments

Comments
 (0)