Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit db76246

Browse files
Allow certain superglobal offsets to be set directly
1 parent 8da11ff commit db76246

File tree

6 files changed

+80
-20
lines changed

6 files changed

+80
-20
lines changed

‎src/Rules/Superglobals/SuperglobalAccessRule.php‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,17 @@ public function processNode(Node $node, Scope $scope): array
7272
}
7373

7474
$method = $this->superglobalRuleHelper->getSuperglobalMethodGetter($name);
75-
$errors = [];
7675

7776
if ($dimType->getConstantStrings() !== []) {
77+
$errors = [];
78+
7879
foreach ($dimType->getConstantStrings() as $dimString) {
7980
$dim = $dimString->getValue();
8081

82+
if ($this->superglobalRuleHelper->isAllowedOffsetAccess($name, $dim)) {
83+
continue;
84+
}
85+
8186
$errors[] = RuleErrorBuilder::message(sprintf('Accessing offset \'%s\' directly on $%s is discouraged.', $dim, $name))
8287
->tip(sprintf('Use \\Config\\Services::superglobals()->%s(\'%s\') instead.', $method, $dim))
8388
->identifier('codeigniter.superglobalAccess')

‎src/Rules/Superglobals/SuperglobalAssignRule.php‎

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -93,32 +93,38 @@ private function processArrayDimFetch(Node $node, Scope $scope): array
9393

9494
$exprType = $scope->getType($node->expr);
9595

96-
$expr = $exprType->describe(VerbosityLevel::precise());
97-
$dim = $dimType->describe(VerbosityLevel::precise());
9896
$method = $this->superglobalRuleHelper->getSuperglobalMethodSetter($name);
9997

100-
$addTip = static function (RuleErrorBuilder $ruleErrorBuilder) use ($method, $dimType, $exprType): RuleErrorBuilder {
101-
if ($dimType->getConstantStrings() !== [] && $exprType->getConstantStrings() !== []) {
102-
foreach ($dimType->getConstantStrings() as $dimString) {
103-
foreach ($exprType->getConstantStrings() as $exprString) {
104-
$ruleErrorBuilder->addTip(sprintf(
105-
'Use \\Config\\Services::superglobals()->%s(%s, %s) instead.',
106-
$method,
107-
$dimString->describe(VerbosityLevel::precise()),
108-
$exprString->describe(VerbosityLevel::precise()),
109-
));
110-
}
98+
if ($dimType->getConstantStrings() !== []) {
99+
$errors = [];
100+
101+
foreach ($dimType->getConstantStrings() as $dimString) {
102+
$dim = $dimString->getValue();
103+
104+
if ($this->superglobalRuleHelper->isAllowedOffsetAccess($name, $dim)) {
105+
continue;
111106
}
112107

113-
return $ruleErrorBuilder;
108+
$expr = $exprType->describe(VerbosityLevel::precise());
109+
110+
$errors[] = RuleErrorBuilder::message(sprintf('Assigning %s directly on offset \'%s\' of $%s is discouraged.', $expr, $dim, $name))
111+
->identifier('codeigniter.superglobalAccessAssign')
112+
->tip(sprintf('Use \\Config\\Services::superglobals()->%s(\'%s\', %s) instead.', $method, $dim, $expr))
113+
->build();
114114
}
115115

116-
return $ruleErrorBuilder->tip(sprintf('Use \\Config\\Services::superglobals()->%s() instead.', $method));
117-
};
116+
return $errors;
117+
}
118118

119119
return [
120-
$addTip(RuleErrorBuilder::message(sprintf('Assigning %s directly on offset %s of $%s is discouraged.', $expr, $dim, $name)))
120+
RuleErrorBuilder::message(sprintf(
121+
'Assigning %s directly on offset %s of $%s is discouraged.',
122+
$exprType->describe(VerbosityLevel::precise()),
123+
$dimType->describe(VerbosityLevel::precise()),
124+
$name,
125+
))
121126
->identifier('codeigniter.superglobalAccessAssign')
127+
->tip(sprintf('Use \\Config\\Services::superglobals()->%s(...) instead.', $method))
122128
->build(),
123129
];
124130
}

‎src/Rules/Superglobals/SuperglobalRuleHelper.php‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,28 @@
1717

1818
final class SuperglobalRuleHelper
1919
{
20+
/**
21+
* @var array{_SERVER: list<string>, _GET: list<string>}
22+
*/
23+
private static array $allowedOffsetAccess = [
24+
'_SERVER' => ['argv', 'argc'],
25+
'_GET' => [],
26+
];
27+
2028
public function isHandledSuperglobal(string $name): bool
2129
{
2230
return in_array($name, ['_SERVER', '_GET'], true);
2331
}
2432

33+
public function isAllowedOffsetAccess(string $name, string $offset): bool
34+
{
35+
if (! $this->isHandledSuperglobal($name)) {
36+
return false;
37+
}
38+
39+
return in_array($offset, self::$allowedOffsetAccess[$name], true);
40+
}
41+
2542
/**
2643
* @throws InvalidArgumentException
2744
*/

‎tests/Rules/Superglobals/SuperglobalAssignRuleTest.php‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,12 @@ public function testRule(): void
5151
[
5252
'Assigning string directly on offset string of $_SERVER is discouraged.',
5353
27,
54-
'Use \\Config\\Services::superglobals()->setServer() instead.',
54+
'Use \\Config\\Services::superglobals()->setServer(...) instead.',
5555
],
5656
[
5757
'Assigning string directly on offset string of $_GET is discouraged.',
5858
29,
59-
'Use \Config\Services::superglobals()->setGet() instead.',
59+
'Use \Config\Services::superglobals()->setGet(...) instead.',
6060
],
6161
[
6262
'Cannot re-assign non-arrays to $_GET, got string.',
@@ -66,6 +66,16 @@ public function testRule(): void
6666
'Cannot re-assign non-arrays to $_GET, got int.',
6767
33,
6868
],
69+
[
70+
'Assigning mixed directly on offset \'HTTP_HOST\' of $_SERVER is discouraged.',
71+
42,
72+
'Use \Config\Services::superglobals()->setServer(\'HTTP_HOST\', mixed) instead.',
73+
],
74+
[
75+
'Assigning mixed directly on offset \'SCRIPT_NAME\' of $_SERVER is discouraged.',
76+
42,
77+
'Use \Config\Services::superglobals()->setServer(\'SCRIPT_NAME\', mixed) instead.',
78+
],
6979
]);
7080
}
7181
}

‎tests/Rules/Superglobals/data/superglobal-access-cases.php‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,11 @@ function bar(string $c): ?string
3030
{
3131
return $_SERVER[$c] ?? null;
3232
}
33+
34+
/**
35+
* @return array{array<string, mixed>, int}
36+
*/
37+
function allowed_offset_access(): array
38+
{
39+
return [$_SERVER['argv'] ?? [], $_SERVER['argc'] ?? 0];
40+
}

‎tests/Rules/Superglobals/data/superglobal-assign-cases.php‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,17 @@ function bar(string $key, string $value): void
3333
$_GET = 12_500;
3434

3535
$_GET = ['first' => 'John', 'last' => 'Doe'];
36+
37+
/**
38+
* @param 'HTTP_HOST'|'SCRIPT_NAME' $key
39+
*/
40+
function baz(string $key, mixed $value): void
41+
{
42+
$_SERVER[$key] = $value;
43+
}
44+
45+
function allowed_offset_assigns(): void
46+
{
47+
$_SERVER['argv'] = ['arg1', 'arg2'];
48+
$_SERVER['argc'] = 2;
49+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /