Skip to content

Commit

Permalink
Fix performance issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Busygin committed Jan 21, 2022
1 parent c172050 commit 1218179
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 20 deletions.
59 changes: 59 additions & 0 deletions src/DependencyEmitter/FQDNIndexNode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

declare(strict_types=1);

namespace Qossmic\Deptrac\DependencyEmitter;

class FQDNIndexNode
{
private bool $FQDN = false;

/**
* @var array<string, FQDNIndexNode>
*/
private array $nodes = [];

public function isFQDN(): bool
{
return $this->FQDN;
}

/**
* @param string[] $keys
*/
public function getNestedNode(array $keys): ?self
{
$index = $this;
foreach ($keys as $key) {
$node = $index->nodes[$key] ?? null;
if (null === $node) {
return null;
}

$index = $node;
}

return $index;
}

/**
* @param string[] $keys
*/
public function setNestedNode(array $keys): void
{
if (0 === count($keys)) {
$this->FQDN = true;

return;
}

$key = array_shift($keys);
$node = $this->nodes[$key] ?? null;
if (null === $node) {
$node = new self();
$this->nodes[$key] = $node;
}

$node->setNestedNode($keys);
}
}
41 changes: 21 additions & 20 deletions src/DependencyEmitter/UsesDependencyEmitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,26 @@ public function getName(): string

public function applyDependencies(AstMap $astMap, Result $dependencyResult): void
{
$references = array_merge($astMap->getAstClassReferences(), $astMap->getAstFunctionReferences());
$referencesFQDN = array_map(
static function ($ref) {
return $ref->getTokenName()->toString();
},
$references
);

$FQDNIndex = new FQDNIndexNode();
foreach ($referencesFQDN as $reference) {
$path = explode('\\', $reference);
$FQDNIndex->setNestedNode($path);
}

foreach ($astMap->getAstFileReferences() as $fileReference) {
$dependencies = $fileReference->getDependencies();
foreach ($fileReference->getAstClassReferences() as $astClassReference) {
foreach ($dependencies as $emittedDependency) {
if (AstDependency::USE === $emittedDependency->getType() &&
$this->isFQDN($emittedDependency, $astMap)
$this->isFQDN($emittedDependency, $FQDNIndex)
) {
$dependencyResult->addDependency(
new Dependency(
Expand All @@ -38,28 +52,15 @@ public function applyDependencies(AstMap $astMap, Result $dependencyResult): voi
}
}

protected function isFQDN(AstDependency $dependency, AstMap $astMap): bool
protected function isFQDN(AstDependency $dependency, FQDNIndexNode $FQDNIndex): bool
{
$dependencyFQDN = $dependency->getTokenName()->toString();
$references = array_merge($astMap->getAstClassReferences(), $astMap->getAstFunctionReferences());

$isFQDN = false;
$isNamespace = false;

foreach ($references as $reference) {
$referenceFQDN = $reference->getTokenName()->toString();

if ($referenceFQDN === $dependencyFQDN) {
$isFQDN = true;
}

if (0 === strpos($referenceFQDN, $dependencyFQDN, 0) &&
$referenceFQDN !== $dependencyFQDN
) {
$isNamespace = true;
}
$path = explode('\\', $dependencyFQDN);
$value = $FQDNIndex->getNestedNode($path);
if (null === $value) {
return true;
}

return !$isNamespace || $isFQDN;
return $value->isFQDN();
}
}

0 comments on commit 1218179

Please sign in to comment.