Skip to content

Commit

Permalink
Superglobal variable assign is impure
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Mar 29, 2024
1 parent 8b7adac commit cf12109
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -4403,6 +4403,9 @@ private function processAssignVar(
$hasYield = $result->hasYield();
$throwPoints = $result->getThrowPoints();
$impurePoints = $result->getImpurePoints();
if (in_array($var->name, Scope::SUPERGLOBAL_VARIABLES, true)) {
$impurePoints[] = new ImpurePoint($scope, $var, 'superglobal', 'assign to superglobal variable', true);
}
$assignedExpr = $this->unwrapAssign($assignedExpr);
$type = $scope->getType($assignedExpr);

Expand Down
8 changes: 8 additions & 0 deletions tests/PHPStan/Rules/Pure/PureMethodRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,14 @@ public function testRule(): void
'Method PureMethod\ClassWithVoidMethods::privateEmptyVoidFunction() returns void but does not have any side effects.',
214,
],
[
'Impure assign to superglobal variable in pure method PureMethod\ClassWithVoidMethods::purePostGetAssign().',
230,
],
[
'Impure assign to superglobal variable in pure method PureMethod\ClassWithVoidMethods::purePostGetAssign().',
231,
],
]);
}

Expand Down
17 changes: 17 additions & 0 deletions tests/PHPStan/Rules/Pure/data/pure-method.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,4 +216,21 @@ private function privateEmptyVoidFunction(): void

}

private function setPostAndGet(array $post = [], array $get = []): void
{
$_POST = $post;
$_GET = $get;
}

/**
* @phpstan-pure
*/
public function purePostGetAssign(array $post = [], array $get = []): int
{
$_POST = $post;
$_GET = $get;

return 1;
}

}

0 comments on commit cf12109

Please sign in to comment.