Lines 55.55% 5 / 9
Methods 75.00% 3 / 4
Classes 0.00% 0 / 1
Name Lines Methods CRAP
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 findManifestClass 0.00% 0 / 4 0.00% 0 / 1 20
 findManifestClassAmong 100.00% 2 / 2 100.00% 1 / 1 2
 holdsGeneratedClasses 100.00% 2 / 2 100.00% 1 / 1 2
10final readonly class GeneratedManifestLocator
11{
12    private const string MANIFEST = 'Manifest';
13
14    private const array ROOTS = ['Actions', 'Services'];
15
16    public function __construct(private ReflectionProvider $reflection) {}
17
18    // The project's Composer loader is the one that loaded this extension; a PHAR brings a loader of its own.
19    public function findManifestClass(): ?string
20    {
21        foreach (ClassLoader::getRegisteredLoaders() as $vendorDirectory => $loader) {
22            if (!str_starts_with($vendorDirectory, 'phar://') && $loader->findFile(self::class) !== false) {
23                return $this->findManifestClassAmong(array_keys($loader->getPrefixesPsr4()));
24            }
25        }
26
27        return null;
28    }
29
30    // Probed through static reflection only: a tree from an older major must not be loaded.
31    /** @param list<string> $namespacePrefixes */
32    public function findManifestClassAmong(array $namespacePrefixes): ?string
33    {
34        $found = array_values(array_filter($namespacePrefixes, $this->holdsGeneratedClasses(...)));
35
36        return \count($found) === 1 ? $found[0] . self::MANIFEST : null;
37    }
38
39    private function holdsGeneratedClasses(string $namespacePrefix): bool
40    {
41        return $this->reflection->hasClass($namespacePrefix . self::MANIFEST)
42            && array_any(self::ROOTS, fn(string $root): bool => $this->reflection->hasClass($namespacePrefix . $root));
43    }
44}