Skip to content

Commit

Permalink
Speedup #1: Reorganize ext symbol kinds only once
Browse files Browse the repository at this point in the history
  • Loading branch information
janedbal committed Oct 15, 2024
1 parent 491d3e9 commit 774bc40
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 21 deletions.
14 changes: 11 additions & 3 deletions src/Analyser.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,13 @@ class Analyser
*/
private $extensionSymbols;

/**
* lowercase symbol name => kind
*
* @var array<string, SymbolKind::*>
*/
private $extensionSymbolKinds;

/**
* @param array<string, ClassLoader> $classLoaders vendorDir => ClassLoader (e.g. result of \Composer\Autoload\ClassLoader::getRegisteredLoaders())
* @param array<string, bool> $composerJsonDependencies package or ext-* => is dev dependency
Expand Down Expand Up @@ -385,9 +392,7 @@ private function getUsedSymbolsInFile(string $filePath): array
}

return (new UsedSymbolExtractor($code))->parseUsedSymbols(
array_keys($this->extensionSymbols[SymbolKind::CLASSLIKE]),
array_keys($this->extensionSymbols[SymbolKind::FUNCTION]),
array_keys($this->extensionSymbols[SymbolKind::CONSTANT])
$this->extensionSymbolKinds
);
}

Expand Down Expand Up @@ -547,6 +552,7 @@ private function initExistingSymbols(): void
$this->ignoredSymbols[$constantName] = true;
} else {
$this->extensionSymbols[SymbolKind::CONSTANT][$constantName] = $extensionName;
$this->extensionSymbolKinds[strtolower($constantName)] = SymbolKind::CONSTANT;
}
}
}
Expand All @@ -568,6 +574,7 @@ private function initExistingSymbols(): void
$this->ignoredSymbols[$functionName] = true;
} else {
$this->extensionSymbols[SymbolKind::FUNCTION][$functionName] = $extensionName;
$this->extensionSymbolKinds[$functionName] = SymbolKind::FUNCTION;
}
}
}
Expand All @@ -590,6 +597,7 @@ private function initExistingSymbols(): void
$this->ignoredSymbols[$classLikeName] = true;
} else {
$this->extensionSymbols[SymbolKind::CLASSLIKE][$classLikeName] = $extensionName;
$this->extensionSymbolKinds[strtolower($classLikeName)] = SymbolKind::CLASSLIKE;
}
}
}
Expand Down
16 changes: 2 additions & 14 deletions src/UsedSymbolExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

namespace ShipMonk\ComposerDependencyAnalyser;

use function array_fill_keys;
use function array_map;
use function array_merge;
use function count;
use function explode;
use function is_array;
Expand Down Expand Up @@ -70,24 +67,15 @@ public function __construct(string $code)
* It does not produce any local names in current namespace
* - this results in very limited functionality in files without namespace
*
* @param list<string> $extClasses
* @param list<string> $extFunctions
* @param list<string> $extConstants
* @param array<string, SymbolKind::*> $extensionSymbols
* @return array<SymbolKind::*, array<string, list<int>>>
* @license Inspired by https://github.com/doctrine/annotations/blob/2.0.0/lib/Doctrine/Common/Annotations/TokenParser.php
*/
public function parseUsedSymbols(
array $extClasses,
array $extFunctions,
array $extConstants
array $extensionSymbols
): array
{
$usedSymbols = [];
$extensionSymbols = array_merge(
array_fill_keys(array_map('strtolower', $extClasses), SymbolKind::CLASSLIKE),
array_fill_keys(array_map('strtolower', $extFunctions), SymbolKind::FUNCTION),
array_fill_keys(array_map('strtolower', $extConstants), SymbolKind::CONSTANT)
);
$useStatements = [];
$useStatementKinds = [];

Expand Down
14 changes: 10 additions & 4 deletions tests/UsedSymbolExtractorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
namespace ShipMonk\ComposerDependencyAnalyser;

use PHPUnit\Framework\TestCase;
use function array_map;
use function file_get_contents;
use function strtolower;
use const PHP_VERSION_ID;

class UsedSymbolExtractorTest extends TestCase
Expand All @@ -24,9 +24,15 @@ public function test(string $path, array $expectedUsages): void
self::assertSame(
$expectedUsages,
$extractor->parseUsedSymbols(
['PDO'],
array_map('strtolower', ['json_encode', 'DDTrace\active_span', 'DDTrace\root_span']),
['LIBXML_ERR_FATAL', 'LIBXML_ERR_ERROR', 'DDTrace\DBM_PROPAGATION_FULL']
[
strtolower('PDO') => SymbolKind::CLASSLIKE,
strtolower('json_encode') => SymbolKind::FUNCTION,
strtolower('DDTrace\active_span') => SymbolKind::FUNCTION,
strtolower('DDTrace\root_span') => SymbolKind::FUNCTION,
strtolower('LIBXML_ERR_FATAL') => SymbolKind::CONSTANT,
strtolower('LIBXML_ERR_ERROR') => SymbolKind::CONSTANT,
strtolower('DDTrace\DBM_PROPAGATION_FULL') => SymbolKind::CONSTANT,
]
)
);
}
Expand Down

0 comments on commit 774bc40

Please sign in to comment.