diff --git a/src/Analyser.php b/src/Analyser.php index fa024de..1f5b549 100644 --- a/src/Analyser.php +++ b/src/Analyser.php @@ -118,6 +118,13 @@ class Analyser */ private $extensionSymbols; + /** + * lowercase symbol name => kind + * + * @var array + */ + private $extensionSymbolKinds; + /** * @param array $classLoaders vendorDir => ClassLoader (e.g. result of \Composer\Autoload\ClassLoader::getRegisteredLoaders()) * @param array $composerJsonDependencies package or ext-* => is dev dependency @@ -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 ); } @@ -547,6 +552,7 @@ private function initExistingSymbols(): void $this->ignoredSymbols[$constantName] = true; } else { $this->extensionSymbols[SymbolKind::CONSTANT][$constantName] = $extensionName; + $this->extensionSymbolKinds[$constantName] = SymbolKind::CONSTANT; } } } @@ -568,6 +574,7 @@ private function initExistingSymbols(): void $this->ignoredSymbols[$functionName] = true; } else { $this->extensionSymbols[SymbolKind::FUNCTION][$functionName] = $extensionName; + $this->extensionSymbolKinds[$functionName] = SymbolKind::FUNCTION; } } } @@ -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; } } } diff --git a/src/UsedSymbolExtractor.php b/src/UsedSymbolExtractor.php index 74d007c..b16a27f 100644 --- a/src/UsedSymbolExtractor.php +++ b/src/UsedSymbolExtractor.php @@ -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; @@ -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 $extClasses - * @param list $extFunctions - * @param list $extConstants + * @param array $extensionSymbols * @return array>> * @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 = []; diff --git a/tests/UsedSymbolExtractorTest.php b/tests/UsedSymbolExtractorTest.php index 74192a4..016b605 100644 --- a/tests/UsedSymbolExtractorTest.php +++ b/tests/UsedSymbolExtractorTest.php @@ -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 @@ -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, + ] ) ); }