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
27 changes: 18 additions & 9 deletions src/Rules/Classes/DuplicateClassDeclarationRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
final class DuplicateClassDeclarationRule implements Rule
{

/** @var array<class-string|trait-string, list<ReflectionClass>>|null */
private ?array $classMap = null;

public function __construct(private Reflector $reflector, private RelativePathHelper $relativePathHelper)
{
}
Expand All @@ -38,21 +41,27 @@ public function processNode(Node $node, Scope $scope): array
{
$thisClass = $node->getClassReflection();
$className = $thisClass->getName();
$allClasses = $this->reflector->reflectAllClasses();
$filteredClasses = [];
foreach ($allClasses as $reflectionClass) {
if ($reflectionClass->getName() !== $className) {
continue;
}
Comment on lines -43 to -46

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

before this PR, the rule iterated all reflected classes, for every class found while analysis.

after the PR we built a map once and than lookup said map so we don't need to iterate the possible big array of all reflected classes over and over.


$filteredClasses[] = $reflectionClass;
// this rule runs at the very end of the analysis,
// so all classes already have been discovered at this point.
if ($this->classMap === null) {
$this->classMap = [];

$allClasses = $this->reflector->reflectAllClasses();
foreach ($allClasses as $reflectionClass) {
$reflectionClassName = $reflectionClass->getName();
if (!isset($this->classMap[$reflectionClassName])) {
$this->classMap[$reflectionClassName] = [];
}
$this->classMap[$reflectionClassName][] = $reflectionClass;
}
}

if (count($filteredClasses) < 2) {
if (!isset($this->classMap[$className]) || count($this->classMap[$className]) < 2) {
return [];
}

$filteredClasses = array_filter($filteredClasses, static fn (ReflectionClass $class) => $class->getStartLine() !== $thisClass->getNativeReflection()->getStartLine());
$filteredClasses = array_filter($this->classMap[$className], static fn (ReflectionClass $class) => $class->getStartLine() !== $thisClass->getNativeReflection()->getStartLine());

$identifierType = strtolower($thisClass->getClassTypeDescription());

Expand Down
Loading