-
Notifications
You must be signed in to change notification settings - Fork 490
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bleeding edge - detect duplicate functions
- Loading branch information
1 parent
0d7db44
commit 17e4b74
Showing
6 changed files
with
139 additions
and
18 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,59 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Functions; | ||
|
||
use PhpParser\Node; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\BetterReflection\Reflection\ReflectionFunction; | ||
use PHPStan\BetterReflection\Reflector\Reflector; | ||
use PHPStan\File\RelativePathHelper; | ||
use PHPStan\Node\InFunctionNode; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Rules\RuleErrorBuilder; | ||
use function array_map; | ||
use function count; | ||
use function implode; | ||
use function sprintf; | ||
|
||
/** | ||
* @implements Rule<InFunctionNode> | ||
*/ | ||
class DuplicateFunctionDeclarationRule implements Rule | ||
{ | ||
|
||
public function __construct(private Reflector $reflector, private RelativePathHelper $relativePathHelper) | ||
{ | ||
} | ||
|
||
public function getNodeType(): string | ||
{ | ||
return InFunctionNode::class; | ||
} | ||
|
||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
$thisFunction = $node->getFunctionReflection(); | ||
$allFunctions = $this->reflector->reflectAllFunctions(); | ||
$filteredFunctions = []; | ||
foreach ($allFunctions as $reflectionFunction) { | ||
if ($reflectionFunction->getName() !== $thisFunction->getName()) { | ||
continue; | ||
} | ||
|
||
$filteredFunctions[] = $reflectionFunction; | ||
} | ||
|
||
if (count($filteredFunctions) < 2) { | ||
return []; | ||
} | ||
|
||
return [ | ||
RuleErrorBuilder::message(sprintf( | ||
"Function %s declared multiple times:\n%s", | ||
$thisFunction->getName(), | ||
implode("\n", array_map(fn (ReflectionFunction $function) => sprintf('- %s:%d', $this->relativePathHelper->getRelativePath($function->getFileName() ?? 'unknown'), $function->getStartLine()), $filteredFunctions)), | ||
))->build(), | ||
]; | ||
} | ||
|
||
} |
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
49 changes: 49 additions & 0 deletions
49
tests/PHPStan/Rules/Functions/DuplicateFunctionDeclarationRuleTest.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,49 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Functions; | ||
|
||
use PHPStan\BetterReflection\Reflector\DefaultReflector; | ||
use PHPStan\File\SimpleRelativePathHelper; | ||
use PHPStan\Reflection\BetterReflection\SourceLocator\FileNodesFetcher; | ||
use PHPStan\Reflection\BetterReflection\SourceLocator\OptimizedSingleFileSourceLocator; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Testing\RuleTestCase; | ||
|
||
/** | ||
* @extends RuleTestCase<DuplicateFunctionDeclarationRule> | ||
*/ | ||
class DuplicateFunctionDeclarationRuleTest extends RuleTestCase | ||
{ | ||
|
||
private const FILENAME = __DIR__ . '/data/duplicate-function.php'; | ||
|
||
protected function getRule(): Rule | ||
{ | ||
return new DuplicateFunctionDeclarationRule( | ||
new DefaultReflector(new OptimizedSingleFileSourceLocator( | ||
self::getContainer()->getByType(FileNodesFetcher::class), | ||
self::FILENAME, | ||
)), | ||
new SimpleRelativePathHelper(__DIR__ . '/data'), | ||
); | ||
} | ||
|
||
public function testRule(): void | ||
{ | ||
$this->analyse([self::FILENAME], [ | ||
[ | ||
"Function DuplicateFunctionDeclaration\\foo declared multiple times:\n- duplicate-function.php:10\n- duplicate-function.php:15\n- duplicate-function.php:20", | ||
10, | ||
], | ||
[ | ||
"Function DuplicateFunctionDeclaration\\foo declared multiple times:\n- duplicate-function.php:10\n- duplicate-function.php:15\n- duplicate-function.php:20", | ||
15, | ||
], | ||
[ | ||
"Function DuplicateFunctionDeclaration\\foo declared multiple times:\n- duplicate-function.php:10\n- duplicate-function.php:15\n- duplicate-function.php:20", | ||
20, | ||
], | ||
]); | ||
} | ||
|
||
} |
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,23 @@ | ||
<?php | ||
|
||
namespace DuplicateFunctionDeclaration; | ||
|
||
function notDuplicate() | ||
{ | ||
|
||
} | ||
|
||
function foo() | ||
{ | ||
|
||
} | ||
|
||
function foo() | ||
{ | ||
|
||
} | ||
|
||
function foo() | ||
{ | ||
|
||
} |