-
Notifications
You must be signed in to change notification settings - Fork 481
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
78aab76
commit b77f894
Showing
4 changed files
with
127 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
37
tests/PHPStan/Rules/Functions/RedefinedParametersRuleTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
32
tests/PHPStan/Rules/Functions/data/redefined-parameters.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; | ||
} | ||
|
||
} |