Skip to content

Commit 48dc21a

Browse files
committed
DisallowImplicitArrayCreationSniff: Fixed false positives
1 parent e3c70bc commit 48dc21a

File tree

4 files changed

+29
-9
lines changed

4 files changed

+29
-9
lines changed

SlevomatCodingStandard/Sniffs/Arrays/DisallowImplicitArrayCreationSniff.php

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use PHP_CodeSniffer\Sniffs\Sniff;
77
use SlevomatCodingStandard\Helpers\ScopeHelper;
88
use SlevomatCodingStandard\Helpers\TokenHelper;
9+
use const T_CLOSE_PARENTHESIS;
910
use const T_CLOSURE;
1011
use const T_DOUBLE_COLON;
1112
use const T_EQUAL;
@@ -16,6 +17,7 @@
1617
use const T_OPEN_SHORT_ARRAY;
1718
use const T_OPEN_SQUARE_BRACKET;
1819
use const T_OPEN_TAG;
20+
use const T_STATIC;
1921
use const T_STRING;
2022
use const T_USE;
2123
use const T_VARIABLE;
@@ -161,11 +163,16 @@ private function hasExplicitCreation(File $phpcsFile, int $scopeOpenerPointer, i
161163
return true;
162164
}
163165

164-
if ($this->isCreatedInList($phpcsFile, $i, $scopeOpenerPointer)) {
166+
$staticPointer = TokenHelper::findPreviousEffective($phpcsFile, $i - 1);
167+
if ($tokens[$staticPointer]['code'] === T_STATIC) {
168+
return true;
169+
}
170+
171+
if ($this->isCreatedInForeach($phpcsFile, $i, $scopeCloserPointer)) {
165172
return true;
166173
}
167174

168-
if ($this->isCreatedInForeach($phpcsFile, $i, $scopeOpenerPointer)) {
175+
if ($this->isCreatedInList($phpcsFile, $i, $scopeOpenerPointer)) {
169176
return true;
170177
}
171178

@@ -198,15 +205,15 @@ private function isCreatedInList(File $phpcsFile, int $variablePointer, int $sco
198205
return $tokens[$parenthesisOpenerPointer]['bracket_closer'] > $variablePointer;
199206
}
200207

201-
private function isCreatedInForeach(File $phpcsFile, int $variablePointer, int $scopeOpenerPointer): bool
208+
private function isCreatedInForeach(File $phpcsFile, int $variablePointer, int $scopeCloserPointer): bool
202209
{
203210
$tokens = $phpcsFile->getTokens();
204211

205-
$parenthesisOpenerPointer = TokenHelper::findPrevious($phpcsFile, T_OPEN_PARENTHESIS, $variablePointer - 1, $scopeOpenerPointer);
206-
return $parenthesisOpenerPointer !== null
207-
&& array_key_exists('parenthesis_owner', $tokens[$parenthesisOpenerPointer])
208-
&& $tokens[$tokens[$parenthesisOpenerPointer]['parenthesis_owner']]['code'] === T_FOREACH
209-
&& $tokens[$parenthesisOpenerPointer]['parenthesis_closer'] > $variablePointer;
212+
$parenthesisCloserPointer = TokenHelper::findNext($phpcsFile, T_CLOSE_PARENTHESIS, $variablePointer + 1, $scopeCloserPointer);
213+
return $parenthesisCloserPointer !== null
214+
&& array_key_exists('parenthesis_owner', $tokens[$parenthesisCloserPointer])
215+
&& $tokens[$tokens[$parenthesisCloserPointer]['parenthesis_owner']]['code'] === T_FOREACH
216+
&& $tokens[$parenthesisCloserPointer]['parenthesis_opener'] < $variablePointer;
210217
}
211218

212219
private function isCreatedByReferencedParameterInFunctionCall(File $phpcsFile, int $variablePointer, int $scopeOpenerPointer): bool

tests/Sniffs/Arrays/DisallowImplicitArrayCreationSniffTest.php

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

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

2222
self::assertSniffError($report, 3, DisallowImplicitArrayCreationSniff::CODE_IMPLICIT_ARRAY_CREATION_USED);
2323
self::assertSniffError($report, 7, DisallowImplicitArrayCreationSniff::CODE_IMPLICIT_ARRAY_CREATION_USED);
2424
self::assertSniffError($report, 13, DisallowImplicitArrayCreationSniff::CODE_IMPLICIT_ARRAY_CREATION_USED);
2525
self::assertSniffError($report, 20, DisallowImplicitArrayCreationSniff::CODE_IMPLICIT_ARRAY_CREATION_USED);
26+
self::assertSniffError($report, 27, DisallowImplicitArrayCreationSniff::CODE_IMPLICIT_ARRAY_CREATION_USED);
2627
}
2728

2829
}

tests/Sniffs/Arrays/data/disallowImplicitArrayCreationErrors.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,9 @@ function differentScope()
2020
$a[] = 'a';
2121
})();
2222
}
23+
24+
function parenthesesBefore()
25+
{
26+
$x = (10) + $value;
27+
$value[] = 'a';
28+
}

tests/Sniffs/Arrays/data/disallowImplicitArrayCreationNoErrors.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,9 @@ function createdByReferencedParameterInFunctionCall($query)
8383
parse_str($query, $arguments);
8484
$arguments[] = 'a';
8585
}
86+
87+
function staticVariable()
88+
{
89+
static $value;
90+
$value[] = true;
91+
}

0 commit comments

Comments
 (0)