Skip to content

Commit

Permalink
Add RedefinedParametersRule
Browse files Browse the repository at this point in the history
  • Loading branch information
paulbalandan authored and ondrejmirtes committed Nov 21, 2023
1 parent 78aab76 commit b77f894
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 0 deletions.
1 change: 1 addition & 0 deletions conf/config.level0.neon
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ rules:
- PHPStan\Rules\Functions\InnerFunctionRule
- PHPStan\Rules\Functions\ParamAttributesRule
- PHPStan\Rules\Functions\PrintfParametersRule
- PHPStan\Rules\Functions\RedefinedParametersRule
- PHPStan\Rules\Functions\ReturnNullsafeByRefRule
- PHPStan\Rules\Functions\VariadicParametersDeclarationRule
- PHPStan\Rules\Keywords\ContinueBreakInLoopRule
Expand Down
57 changes: 57 additions & 0 deletions src/Rules/Functions/RedefinedParametersRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rules\Functions;

use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use function count;
use function is_string;
use function sprintf;

/**
* @implements Rule<Node\FunctionLike>
*/
class RedefinedParametersRule implements Rule
{

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

public function processNode(Node $node, Scope $scope): array
{
$params = $node->getParams();

if (count($params) <= 1) {
return [];
}

$vars = [];
$errors = [];

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

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

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

if (!isset($vars[$var])) {
$vars[$var] = true;
continue;
}

$errors[] = RuleErrorBuilder::message(sprintf('Redefinition of parameter $%s.', $var))->nonIgnorable()->build();
}

return $errors;
}

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

namespace PHPStan\Rules\Functions;

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

/**
* @extends RuleTestCase<RedefinedParametersRule>
*/
class RedefinedParametersRuleTest extends RuleTestCase
{

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

public function testRule(): void
{
$this->analyse([__DIR__ . '/data/redefined-parameters.php'], [
[
'Redefinition of parameter $foo.',
11,
],
[
'Redefinition of parameter $bar.',
13,
],
[
'Redefinition of parameter $baz.',
15,
],
]);
}

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

namespace RedefinedParameters;

class HelloWorld
{

/**
* @return \Closure(string, int): int
*/
public function foo(string $foo, string $foo): \Closure
{
$callback = static fn (int $bar, array $bar): int => (int) $bar;

return function (string $baz, int $baz) use ($callback): int {
return $callback($baz, []);
};
}

/**
* @return \Closure(string, bool): int
*/
public function bar(string $pipe, int $count): \Closure
{
$cb = fn (int $a, string $b): int => $a + (int) $b;

return function (string $c, bool $d) use ($cb, $pipe, $count): int {
return $cb((int) $d, $c) + $cb($count, $pipe);
};
}

}

0 comments on commit b77f894

Please sign in to comment.