Skip to content

Commit

Permalink
Arrays/DisallowImplicitArrayCreation: bug fix - recognize global stat…
Browse files Browse the repository at this point in the history
…ements

While it may be unclear whether the variable imported via the `global` statement is an array, the variable does exist, so this is not implicit array creation.

Includes tests.
  • Loading branch information
jrfnl authored and kukulich committed Sep 29, 2023
1 parent fc778da commit 5504980
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
use function in_array;
use const T_CLOSE_PARENTHESIS;
use const T_CLOSURE;
use const T_COMMA;
use const T_DOUBLE_COLON;
use const T_EQUAL;
use const T_FOREACH;
use const T_GLOBAL;
use const T_LIST;
use const T_OBJECT_OPERATOR;
use const T_OPEN_PARENTHESIS;
Expand Down Expand Up @@ -203,6 +205,10 @@ private function hasExplicitCreation(File $phpcsFile, int $scopeOpenerPointer, i
if ($this->isCreatedByReferencedParameterInFunctionCall($phpcsFile, $i, $scopeOpenerPointer)) {
return true;
}

if ($this->isImportedUsingGlobalStatement($phpcsFile, $i)) {
return true;
}
}

return false;
Expand Down Expand Up @@ -262,4 +268,13 @@ private function isCreatedByReferencedParameterInFunctionCall(File $phpcsFile, i
return $tokens[$pointerBeforeParenthesisOpener]['code'] === T_STRING;
}

private function isImportedUsingGlobalStatement(File $phpcsFile, int $variablePointer): bool
{
$tokens = $phpcsFile->getTokens();

$startOfStatement = $phpcsFile->findStartOfStatement($variablePointer, T_COMMA);

return $tokens[$startOfStatement]['code'] === T_GLOBAL;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function testErrors(): void
{
$report = self::checkFile(__DIR__ . '/data/disallowImplicitArrayCreationErrors.php');

self::assertSame(8, $report->getErrorCount());
self::assertSame(9, $report->getErrorCount());

self::assertSniffError($report, 3, DisallowImplicitArrayCreationSniff::CODE_IMPLICIT_ARRAY_CREATION_USED);
self::assertSniffError($report, 7, DisallowImplicitArrayCreationSniff::CODE_IMPLICIT_ARRAY_CREATION_USED);
Expand All @@ -27,6 +27,7 @@ public function testErrors(): void
self::assertSniffError($report, 36, DisallowImplicitArrayCreationSniff::CODE_IMPLICIT_ARRAY_CREATION_USED);
self::assertSniffError($report, 41, DisallowImplicitArrayCreationSniff::CODE_IMPLICIT_ARRAY_CREATION_USED);
self::assertSniffError($report, 48, DisallowImplicitArrayCreationSniff::CODE_IMPLICIT_ARRAY_CREATION_USED);
self::assertSniffError($report, 54, DisallowImplicitArrayCreationSniff::CODE_IMPLICIT_ARRAY_CREATION_USED);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,9 @@ function undefinedVariable()
$b = $a ? true : false;
$a[] = 2;
}

function notImportedViaGlobalStatement()
{
static $something, $value, $somethingElse;
$value[] = 'a';
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,9 @@ function staticVariable()
static $value;
$value[] = true;
}

function importedViaGlobalStatement()
{
global $something, $value, $somethingElse;
$value[] = 'a';
}

0 comments on commit 5504980

Please sign in to comment.