Skip to content
Merged
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
30 changes: 20 additions & 10 deletions src/Rules/Functions/DuplicateFunctionDeclarationRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
final class DuplicateFunctionDeclarationRule implements Rule
{

/** @var array<non-empty-string, list<ReflectionFunction>>|null */
private ?array $functionMap = null;

public function __construct(private Reflector $reflector, private RelativePathHelper $relativePathHelper)
{
}
Expand All @@ -35,25 +38,32 @@ public function getNodeType(): string
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;
}
$functionName = $thisFunction->getName();

$filteredFunctions[] = $reflectionFunction;
// this rule runs at the very end of the analysis,
// so all function already have been discovered at this point.
if ($this->functionMap === null) {
$this->functionMap = [];

$allFunctions = $this->reflector->reflectAllFunctions();
foreach ($allFunctions as $reflectionFunction) {
$reflectionFunctionName = $reflectionFunction->getName();
if (!isset($this->functionMap[$reflectionFunctionName])) {
$this->functionMap[$reflectionFunctionName] = [];
}
$this->functionMap[$reflectionFunctionName][] = $reflectionFunction;
}
}

if (count($filteredFunctions) < 2) {
if (!isset($this->functionMap[$functionName]) || count($this->functionMap[$functionName]) < 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)),
$functionName,
implode("\n", array_map(fn (ReflectionFunction $function) => sprintf('- %s:%d', $this->relativePathHelper->getRelativePath($function->getFileName() ?? 'unknown'), $function->getStartLine()), $this->functionMap[$functionName])),
))->identifier('function.duplicate')->build(),
];
}
Expand Down
Loading