Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ lint:
--exclude tests/PHPStan/Rules/Classes/data/bug-14250.php \
--exclude tests/PHPStan/Rules/Classes/data/bug-14250-promoted-properties.php \
--exclude tests/PHPStan/Rules/Operators/data/bug-3585.php \
--exclude tests/PHPStan/Rules/Functions/data/bug-14241.php \
src tests

install-paratest:
Expand Down
60 changes: 60 additions & 0 deletions src/Rules/Functions/InvalidParameterNameRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rules\Functions;

use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\DependencyInjection\RegisteredRule;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use function in_array;
use function is_string;
use function sprintf;

/**
* @implements Rule<Node\FunctionLike>
*/
#[RegisteredRule(level: 0)]
final class InvalidParameterNameRule implements Rule
{

public function getNodeType(): string
{
return Node\FunctionLike::class;
}

public function processNode(Node $node, Scope $scope): array
{
$errors = [];

foreach ($node->getParams() as $param) {
if (!$param->var instanceof Node\Expr\Variable) {
continue;
}

if (!is_string($param->var->name)) {
continue;
}

$variableName = $param->var->name;

if (in_array($variableName, Scope::SUPERGLOBAL_VARIABLES, true)) {
$errors[] = RuleErrorBuilder::message(sprintf('Cannot re-assign auto-global variable $%s.', $variableName))
->line($param->getStartLine())
->identifier('parameter.invalidExpr')
->nonIgnorable()
->build();
} elseif ($variableName === 'this') {
$errors[] = RuleErrorBuilder::message('Cannot use $this as parameter.')
->line($param->getStartLine())
->identifier('parameter.invalidExpr')
->nonIgnorable()
->build();
}

}

return $errors;
}

}
65 changes: 65 additions & 0 deletions tests/PHPStan/Rules/Functions/InvalidParameterNameRuleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rules\Functions;

use PHPStan\Rules\Rule;
use PHPStan\Testing\RuleTestCase;

/**
* @extends RuleTestCase<InvalidParameterNameRule>
*/
class InvalidParameterNameRuleTest extends RuleTestCase
{

protected function getRule(): Rule
{
return new InvalidParameterNameRule();
}

public function testRule(): void
{
$this->analyse([__DIR__ . '/data/bug-14241.php'], [
[
'Cannot re-assign auto-global variable $_FILES.',
5,
],
[
'Cannot re-assign auto-global variable $_GET.',
7,
],
[
'Cannot re-assign auto-global variable $_POST.',
7,
],
[
'Cannot re-assign auto-global variable $_SERVER.',
13,
],
[
'Cannot re-assign auto-global variable $_SESSION.',
15,
],
[
'Cannot re-assign auto-global variable $_COOKIE.',
18,
],
[
'Cannot re-assign auto-global variable $_REQUEST.',
20,
],
[
'Cannot re-assign auto-global variable $_ENV.',
22,
],
[
'Cannot re-assign auto-global variable $GLOBALS.',
24,
],
[
'Cannot use $this as parameter.',
26,
],
]);
}

}
26 changes: 26 additions & 0 deletions tests/PHPStan/Rules/Functions/data/bug-14241.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php declare(strict_types = 1);

namespace Bug14241;

function doFoo($_FILES): void {}

function doBar($_GET, $_POST): void {}

function doBaz($ok): void {}

class Foo
{
public function doFoo($_SERVER): void {}

public static function doBar($_SESSION): void {}
}

$f = function ($_COOKIE): void {};

$g = fn ($_REQUEST) => $_REQUEST;

function doQux($_ENV): void {}

function doQuux($GLOBALS): void {}

function doThis($this) {}
Loading