Skip to content

Commit

Permalink
Extract superglobal variables into a public Scope constant
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Mar 29, 2024
1 parent cc45445 commit abb87dd
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 24 deletions.
12 changes: 1 addition & 11 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -568,17 +568,7 @@ public function getDefinedVariables(): array

private function isGlobalVariable(string $variableName): bool
{
return in_array($variableName, [
'GLOBALS',
'_SERVER',
'_GET',
'_POST',
'_FILES',
'_COOKIE',
'_SESSION',
'_REQUEST',
'_ENV',
], true);
return in_array($variableName, self::SUPERGLOBAL_VARIABLES, true);
}

/** @api */
Expand Down
12 changes: 12 additions & 0 deletions src/Analyser/Scope.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,18 @@
interface Scope extends ClassMemberAccessAnswerer, NamespaceAnswerer
{

public const SUPERGLOBAL_VARIABLES = [
'GLOBALS',
'_SERVER',
'_GET',
'_POST',
'_FILES',
'_COOKIE',
'_SESSION',
'_REQUEST',
'_ENV',
];

public function getFile(): string;

public function getFileDescription(): string;
Expand Down
14 changes: 1 addition & 13 deletions src/Rules/Functions/InvalidLexicalVariablesInClosureUseRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,6 @@
class InvalidLexicalVariablesInClosureUseRule implements Rule
{

private const SUPERGLOBAL_NAMES = [
'_COOKIE',
'_ENV',
'_FILES',
'_GET',
'_POST',
'_REQUEST',
'_SERVER',
'_SESSION',
'GLOBALS',
];

public function getNodeType(): string
{
return Node\Expr\Closure::class;
Expand Down Expand Up @@ -72,7 +60,7 @@ static function (Node\Param $param) {
continue;
}

if (in_array($var, self::SUPERGLOBAL_NAMES, true)) {
if (in_array($var, Scope::SUPERGLOBAL_VARIABLES, true)) {
$errors[] = RuleErrorBuilder::message(sprintf('Cannot use superglobal variable $%s as lexical variable.', $var))
->line($use->getStartLine())
->identifier('closure.useSuperGlobal')
Expand Down

0 comments on commit abb87dd

Please sign in to comment.