Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add RedefinedParametersRule #2755

Merged
merged 1 commit into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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);
};
}

}