Skip to content

Commit 7c76a17

Browse files
committed
Allow superglobal assign and access at root level
1 parent ff38e66 commit 7c76a17

File tree

6 files changed

+42
-21
lines changed

6 files changed

+42
-21
lines changed

src/Rules/Superglobals/SuperglobalAccessRule.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use PhpParser\Node;
1717
use PHPStan\Analyser\Scope;
1818
use PHPStan\Rules\Rule;
19+
use PHPStan\Rules\RuleError;
1920
use PHPStan\Rules\RuleErrorBuilder;
2021
use PHPStan\Type\VerbosityLevel;
2122

@@ -35,6 +36,8 @@ public function getNodeType(): string
3536

3637
/**
3738
* @param Node\Expr\ArrayDimFetch $node
39+
*
40+
* @return list<RuleError>
3841
*/
3942
public function processNode(Node $node, Scope $scope): array
4043
{
@@ -56,6 +59,10 @@ public function processNode(Node $node, Scope $scope): array
5659
return [];
5760
}
5861

62+
if ($scope->getFunction() === null) {
63+
return []; // ignore uses in root level (not inside function or method)
64+
}
65+
5966
if ($node->dim === null) {
6067
return [];
6168
}

src/Rules/Superglobals/SuperglobalAssignRule.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ private function processArrayDimFetch(Node $node, Scope $scope): array
8686
return [];
8787
}
8888

89+
if ($scope->getFunction() === null) {
90+
return []; // ignore uses in root level (not inside function or method)
91+
}
92+
8993
if ($scope->isInClass() && $scope->getClassReflection()->getName() === Superglobals::class) {
9094
return [];
9195
}
@@ -159,6 +163,10 @@ private function processVariableExpr(Node $node, Scope $scope): array
159163
];
160164
}
161165

166+
if ($scope->getFunction() === null) {
167+
return []; // ignore uses in root level (not inside function or method)
168+
}
169+
162170
return [
163171
RuleErrorBuilder::message('Re-assigning arrays to $_GET directly is discouraged.')
164172
->tip('Use \\Config\\Services::superglobals()->setGetArray() instead.')

tests/Fixtures/Rules/Superglobals/superglobal-access-cases.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,18 @@
1313

1414
namespace SuperglobalAccess;
1515

16-
$foo = $_SERVER['foo'] ?? null;
16+
/**
17+
* @return list<mixed>
18+
*/
19+
function access(): array
20+
{
21+
$foo = $_SERVER['foo'] ?? null;
1722

18-
$a = (static fn (): string => mt_rand(0, 1) ? 'a' : 'b')();
19-
$b = $_GET[$a] ?? null;
23+
$a = (static fn (): string => mt_rand(0, 1) ? 'a' : 'b')();
24+
$b = $_GET[$a] ?? null;
25+
26+
return [$foo, $b];
27+
}
2028

2129
function bar(string $c): ?string
2230
{

tests/Fixtures/Rules/Superglobals/superglobal-assign-cases.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,14 @@
1313

1414
namespace SuperglobalAssign;
1515

16-
$_SERVER['HTTP_HOST'] = 'https://localhost';
16+
function assigns(): void
17+
{
18+
$_SERVER['HTTP_HOST'] = 'https://localhost';
1719

18-
$_GET['first_name'] = 'John Doe';
20+
$_GET['first_name'] = 'John Doe';
1921

20-
$_SERVER[0] = 'hello';
22+
$_SERVER[0] = 'hello';
23+
}
2124

2225
function bar(string $key, string $value): void
2326
{

tests/Rules/Superglobals/SuperglobalAccessRuleTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,22 +40,22 @@ public function testRule(): void
4040
$this->analyse([__DIR__ . '/../../Fixtures/Rules/Superglobals/superglobal-access-cases.php'], [
4141
[
4242
'Accessing offset \'foo\' directly on $_SERVER is discouraged.',
43-
16,
43+
21,
4444
'Use \\Config\\Services::superglobals()->server(\'foo\') instead.',
4545
],
4646
[
4747
'Accessing offset \'a\' directly on $_GET is discouraged.',
48-
19,
48+
24,
4949
'Use \\Config\\Services::superglobals()->get(\'a\') instead.',
5050
],
5151
[
5252
'Accessing offset \'b\' directly on $_GET is discouraged.',
53-
19,
53+
24,
5454
'Use \\Config\\Services::superglobals()->get(\'b\') instead.',
5555
],
5656
[
5757
'Accessing offset string directly on $_SERVER is discouraged.',
58-
23,
58+
31,
5959
'Use \\Config\\Services::superglobals()->server() instead.',
6060
],
6161
]);

tests/Rules/Superglobals/SuperglobalAssignRuleTest.php

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,36 +40,31 @@ public function testRule(): void
4040
$this->analyse([__DIR__ . '/../../Fixtures/Rules/Superglobals/superglobal-assign-cases.php'], [
4141
[
4242
'Assigning \'https://localhost\' directly on offset \'HTTP_HOST\' of $_SERVER is discouraged.',
43-
16,
43+
18,
4444
'Use \\Config\\Services::superglobals()->setServer(\'HTTP_HOST\', \'https://localhost\') instead.',
4545
],
4646
[
4747
'Assigning \'John Doe\' directly on offset \'first_name\' of $_GET is discouraged.',
48-
18,
48+
20,
4949
'Use \\Config\\Services::superglobals()->setGet(\'first_name\', \'John Doe\') instead.',
5050
],
5151
[
5252
'Assigning string directly on offset string of $_SERVER is discouraged.',
53-
24,
53+
27,
5454
'Use \\Config\\Services::superglobals()->setServer() instead.',
5555
],
5656
[
5757
'Assigning string directly on offset string of $_GET is discouraged.',
58-
26,
58+
29,
5959
'Use \Config\Services::superglobals()->setGet() instead.',
6060
],
6161
[
6262
'Cannot re-assign non-arrays to $_GET, got string.',
63-
29,
63+
32,
6464
],
6565
[
6666
'Cannot re-assign non-arrays to $_GET, got int.',
67-
30,
68-
],
69-
[
70-
'Re-assigning arrays to $_GET directly is discouraged.',
71-
32,
72-
'Use \\Config\\Services::superglobals()->setGetArray() instead.',
67+
33,
7368
],
7469
]);
7570
}

0 commit comments

Comments
 (0)