Skip to content

Commit 82598bf

Browse files
committed
feat: add NoSuperglobalsRule to PHPStan rules, fixes #4
1 parent 579626e commit 82598bf

File tree

4 files changed

+118
-0
lines changed

4 files changed

+118
-0
lines changed

rules.neon

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ rules:
1414
- Shopware\PhpStan\Rule\ScheduledTaskTooLowIntervalRule
1515
- Shopware\PhpStan\Rule\SetForeignKeyRule
1616
- Shopware\PhpStan\Rule\NoEntityRepositoryInLoopRule
17+
- Shopware\PhpStan\Rule\NoSuperglobalsRule
1718

1819
services:
1920
-

src/Rule/NoSuperglobalsRule.php

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Shopware\PhpStan\Rule;
6+
7+
use PhpParser\Node;
8+
use PhpParser\Node\Expr\Variable;
9+
use PHPStan\Analyser\Scope;
10+
use PHPStan\Rules\Rule;
11+
use PHPStan\Rules\RuleErrorBuilder;
12+
use PHPStan\Rules\IdentifierRuleError;
13+
14+
/**
15+
* @implements Rule<Variable>
16+
*/
17+
final class NoSuperglobalsRule implements Rule
18+
{
19+
private const FORBIDDEN_SUPERGLOBALS = [
20+
'_GET',
21+
'_POST',
22+
'_FILES',
23+
'_REQUEST',
24+
];
25+
26+
public function getNodeType(): string
27+
{
28+
return Variable::class;
29+
}
30+
31+
/**
32+
* @return list<IdentifierRuleError>
33+
*/
34+
public function processNode(Node $node, Scope $scope): array
35+
{
36+
if (!is_string($node->name)) {
37+
return [];
38+
}
39+
40+
if (!in_array($node->name, self::FORBIDDEN_SUPERGLOBALS, true)) {
41+
return [];
42+
}
43+
44+
return [
45+
RuleErrorBuilder::message(
46+
sprintf(
47+
'Usage of superglobal $%s is forbidden. Use a proper request object instead.',
48+
$node->name,
49+
),
50+
)
51+
->identifier('shopware.noSuperGlobals')
52+
->build(),
53+
];
54+
}
55+
}

tests/Rule/NoSuperglobalsRuleTest.php

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Shopware\PhpStan\Tests\Rule;
6+
7+
use PHPStan\Rules\Rule;
8+
use PHPStan\Testing\RuleTestCase;
9+
use Shopware\PhpStan\Rule\NoSuperglobalsRule;
10+
11+
/**
12+
* @extends RuleTestCase<NoSuperglobalsRule>
13+
*/
14+
class NoSuperglobalsRuleTest extends RuleTestCase
15+
{
16+
protected function getRule(): Rule
17+
{
18+
return new NoSuperglobalsRule();
19+
}
20+
21+
public function testRule(): void
22+
{
23+
$this->analyse([__DIR__ . '/fixtures/NoSuperglobals.php'], [
24+
[
25+
'Usage of superglobal $_GET is forbidden. Use a proper request object instead.',
26+
11,
27+
],
28+
[
29+
'Usage of superglobal $_POST is forbidden. Use a proper request object instead.',
30+
12,
31+
],
32+
[
33+
'Usage of superglobal $_FILES is forbidden. Use a proper request object instead.',
34+
13,
35+
],
36+
[
37+
'Usage of superglobal $_REQUEST is forbidden. Use a proper request object instead.',
38+
14,
39+
],
40+
]);
41+
}
42+
}
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Shopware\PHPStan\Tests\Rule\Fixtures;
6+
7+
class NoSuperglobals
8+
{
9+
public function test(): void
10+
{
11+
$get = $_GET['test'];
12+
$post = $_POST['test'];
13+
$files = $_FILES['test'];
14+
$request = $_REQUEST['test'];
15+
16+
// These should not trigger errors
17+
$normalVar = 'test';
18+
$anotherVar = $normalVar;
19+
}
20+
}

0 commit comments

Comments
 (0)