Skip to content

Commit 93fedc4

Browse files
committed
Speedup #1: Reorganize ext symbol kinds only once
1 parent 491d3e9 commit 93fedc4

File tree

3 files changed

+23
-21
lines changed

3 files changed

+23
-21
lines changed

Diff for: src/Analyser.php

+11-3
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,13 @@ class Analyser
118118
*/
119119
private $extensionSymbols;
120120

121+
/**
122+
* lowercase symbol name => kind
123+
*
124+
* @var array<string, SymbolKind::*>
125+
*/
126+
private $extensionSymbolKinds;
127+
121128
/**
122129
* @param array<string, ClassLoader> $classLoaders vendorDir => ClassLoader (e.g. result of \Composer\Autoload\ClassLoader::getRegisteredLoaders())
123130
* @param array<string, bool> $composerJsonDependencies package or ext-* => is dev dependency
@@ -385,9 +392,7 @@ private function getUsedSymbolsInFile(string $filePath): array
385392
}
386393

387394
return (new UsedSymbolExtractor($code))->parseUsedSymbols(
388-
array_keys($this->extensionSymbols[SymbolKind::CLASSLIKE]),
389-
array_keys($this->extensionSymbols[SymbolKind::FUNCTION]),
390-
array_keys($this->extensionSymbols[SymbolKind::CONSTANT])
395+
$this->extensionSymbolKinds
391396
);
392397
}
393398

@@ -547,6 +552,7 @@ private function initExistingSymbols(): void
547552
$this->ignoredSymbols[$constantName] = true;
548553
} else {
549554
$this->extensionSymbols[SymbolKind::CONSTANT][$constantName] = $extensionName;
555+
$this->extensionSymbolKinds[$constantName] = SymbolKind::CONSTANT;
550556
}
551557
}
552558
}
@@ -568,6 +574,7 @@ private function initExistingSymbols(): void
568574
$this->ignoredSymbols[$functionName] = true;
569575
} else {
570576
$this->extensionSymbols[SymbolKind::FUNCTION][$functionName] = $extensionName;
577+
$this->extensionSymbolKinds[$functionName] = SymbolKind::FUNCTION;
571578
}
572579
}
573580
}
@@ -590,6 +597,7 @@ private function initExistingSymbols(): void
590597
$this->ignoredSymbols[$classLikeName] = true;
591598
} else {
592599
$this->extensionSymbols[SymbolKind::CLASSLIKE][$classLikeName] = $extensionName;
600+
$this->extensionSymbolKinds[strtolower($classLikeName)] = SymbolKind::CLASSLIKE;
593601
}
594602
}
595603
}

Diff for: src/UsedSymbolExtractor.php

+2-14
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22

33
namespace ShipMonk\ComposerDependencyAnalyser;
44

5-
use function array_fill_keys;
6-
use function array_map;
7-
use function array_merge;
85
use function count;
96
use function explode;
107
use function is_array;
@@ -70,24 +67,15 @@ public function __construct(string $code)
7067
* It does not produce any local names in current namespace
7168
* - this results in very limited functionality in files without namespace
7269
*
73-
* @param list<string> $extClasses
74-
* @param list<string> $extFunctions
75-
* @param list<string> $extConstants
70+
* @param array<string, SymbolKind::*> $extensionSymbols
7671
* @return array<SymbolKind::*, array<string, list<int>>>
7772
* @license Inspired by https://github.com/doctrine/annotations/blob/2.0.0/lib/Doctrine/Common/Annotations/TokenParser.php
7873
*/
7974
public function parseUsedSymbols(
80-
array $extClasses,
81-
array $extFunctions,
82-
array $extConstants
75+
array $extensionSymbols
8376
): array
8477
{
8578
$usedSymbols = [];
86-
$extensionSymbols = array_merge(
87-
array_fill_keys(array_map('strtolower', $extClasses), SymbolKind::CLASSLIKE),
88-
array_fill_keys(array_map('strtolower', $extFunctions), SymbolKind::FUNCTION),
89-
array_fill_keys(array_map('strtolower', $extConstants), SymbolKind::CONSTANT)
90-
);
9179
$useStatements = [];
9280
$useStatementKinds = [];
9381

Diff for: tests/UsedSymbolExtractorTest.php

+10-4
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
namespace ShipMonk\ComposerDependencyAnalyser;
44

55
use PHPUnit\Framework\TestCase;
6-
use function array_map;
76
use function file_get_contents;
7+
use function strtolower;
88
use const PHP_VERSION_ID;
99

1010
class UsedSymbolExtractorTest extends TestCase
@@ -24,9 +24,15 @@ public function test(string $path, array $expectedUsages): void
2424
self::assertSame(
2525
$expectedUsages,
2626
$extractor->parseUsedSymbols(
27-
['PDO'],
28-
array_map('strtolower', ['json_encode', 'DDTrace\active_span', 'DDTrace\root_span']),
29-
['LIBXML_ERR_FATAL', 'LIBXML_ERR_ERROR', 'DDTrace\DBM_PROPAGATION_FULL']
27+
[
28+
strtolower('PDO') => SymbolKind::CLASSLIKE,
29+
strtolower('json_encode') => SymbolKind::FUNCTION,
30+
strtolower('DDTrace\active_span') => SymbolKind::FUNCTION,
31+
strtolower('DDTrace\root_span') => SymbolKind::FUNCTION,
32+
strtolower('LIBXML_ERR_FATAL') => SymbolKind::CONSTANT,
33+
strtolower('LIBXML_ERR_ERROR') => SymbolKind::CONSTANT,
34+
strtolower('DDTrace\DBM_PROPAGATION_FULL') => SymbolKind::CONSTANT,
35+
]
3036
)
3137
);
3238
}

0 commit comments

Comments
 (0)