Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Arrays/DisallowImplicitArrayCreation: bug fix - recognize global statements #1623

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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';
}