Skip to content

Commit b905a82

Browse files
committed
StrictCallSniff: Fixed false positive
1 parent 1a6575c commit b905a82

File tree

4 files changed

+38
-6
lines changed

4 files changed

+38
-6
lines changed

SlevomatCodingStandard/Sniffs/Functions/StrictCallSniff.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ public function process(File $phpcsFile, $stringPointer): void
5656
return;
5757
}
5858

59+
$parenthesisCloserPointer = $tokens[$parenthesisOpenerPointer]['parenthesis_closer'];
60+
5961
$functionName = strtolower($tokens[$stringPointer]['content']);
6062

6163
if (!array_key_exists($functionName, self::FUNCTIONS)) {
@@ -68,7 +70,7 @@ public function process(File $phpcsFile, $stringPointer): void
6870
}
6971

7072
$commaPointers = [];
71-
for ($i = $parenthesisOpenerPointer + 1; $i < $tokens[$parenthesisOpenerPointer]['parenthesis_closer']; $i++) {
73+
for ($i = $parenthesisOpenerPointer + 1; $i < $parenthesisCloserPointer; $i++) {
7274
if ($tokens[$i]['code'] === T_OPEN_PARENTHESIS) {
7375
$i = $tokens[$i]['parenthesis_closer'];
7476
continue;
@@ -88,19 +90,25 @@ public function process(File $phpcsFile, $stringPointer): void
8890

8991
$parametersCount = $commaPointersCount + 1;
9092
$lastCommaPointer = $commaPointersCount > 0 ? $commaPointers[$commaPointersCount - 1] : null;
93+
$hasTrailingComma = false;
9194

9295
if (
9396
$lastCommaPointer !== null
94-
&& TokenHelper::findNextEffective($phpcsFile, $lastCommaPointer + 1, $tokens[$parenthesisOpenerPointer]['parenthesis_closer']) === null
97+
&& TokenHelper::findNextEffective($phpcsFile, $lastCommaPointer + 1, $parenthesisCloserPointer) === null
9598
) {
99+
$hasTrailingComma = true;
96100
$parametersCount--;
97101
}
98102

99103
if ($parametersCount === self::FUNCTIONS[$functionName]) {
100104

101-
$strictParameterValue = strtolower(trim(TokenHelper::getContent($phpcsFile, $lastCommaPointer + 1, $tokens[$parenthesisOpenerPointer]['parenthesis_closer'] - 1)));
105+
$strictParameterValue = TokenHelper::getContent(
106+
$phpcsFile,
107+
$commaPointers[self::FUNCTIONS[$functionName] - 2] + 1,
108+
($hasTrailingComma ? $lastCommaPointer : $parenthesisCloserPointer) - 1
109+
);
102110

103-
if ($strictParameterValue === 'true') {
111+
if (strtolower(trim($strictParameterValue)) === 'true') {
104112
return;
105113
}
106114

tests/Sniffs/Functions/StrictCallSniffTest.php

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

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

2222
self::assertSniffError($report, 3, StrictCallSniff::CODE_STRICT_PARAMETER_MISSING);
2323
self::assertSniffError($report, 4, StrictCallSniff::CODE_STRICT_PARAMETER_MISSING);
@@ -26,6 +26,7 @@ public function testErrors(): void
2626
self::assertSniffError($report, 10, StrictCallSniff::CODE_NON_STRICT_COMPARISON);
2727
self::assertSniffError($report, 11, StrictCallSniff::CODE_NON_STRICT_COMPARISON);
2828
self::assertSniffError($report, 16, StrictCallSniff::CODE_NON_STRICT_COMPARISON);
29+
self::assertSniffError($report, 18, StrictCallSniff::CODE_NON_STRICT_COMPARISON);
2930
}
3031

3132
}

tests/Sniffs/Functions/data/strictCallErrors.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,12 @@
1414
false,
1515
);
1616
base64_decode('', false);
17+
18+
in_array(
19+
0,
20+
[
21+
1,
22+
2,
23+
],
24+
false,
25+
);

tests/Sniffs/Functions/data/strictCallNoErrors.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Foo;
44

5-
use SlevomatCodingStandard\Helpers\ParameterTypeHint;
65
use function array_filter;
76
use function array_keys;
87

@@ -38,3 +37,18 @@ public static function array_search($a, $b)
3837
array_keys(
3938
[],
4039
);
40+
41+
in_array(
42+
0,
43+
[
44+
1,
45+
2,
46+
],
47+
true,
48+
);
49+
50+
in_array(
51+
$whatever,
52+
$cache[$cacheKey],
53+
true,
54+
);

0 commit comments

Comments
 (0)