Lines 92.00% 23 / 25
Functions and Methods 85.71% 6 / 7
Classes and Traits 0.00% 0 / 1
Name Lines Functions and Methods CRAP Classes and Traits
ManifestCatalogLoader 92.00% 23 / 25 85.71% 6 / 7 15.12 0.00% 0 / 1
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 findCatalogTrackedBy 100.00% 2 / 2 100.00% 1 / 1 1
 findCatalog 100.00% 2 / 2 100.00% 1 / 1 1
 findOlderMajorNamespaceTrackedBy 100.00% 4 / 4 100.00% 1 / 1 2
 findOlderMajorNamespace 100.00% 2 / 2 100.00% 1 / 1 1
 loadOnce 100.00% 3 / 3 100.00% 1 / 1 2
 loadCatalog 81.81% 9 / 11 0.00% 0 / 1 7.29
1<?php
2
3declare(strict_types=1);
4
5namespace Stewart\PHPStan\Manifest;
6
7use PHPStan\Analyser\DependencyTracker;
8use PHPStan\Analyser\Scope;
9use PHPStan\Reflection\ReflectionProvider;
10use Stewart\Contracts\Generated\Manifest;
11use Stewart\PHPStan\Cache\ManifestResultCacheValue;
12
13// PHPStan runs with the project's autoloader, so the generated Manifest is read as plain PHP, once per analysis.
14final class ManifestCatalogLoader
15{
16    private ?ManifestCatalog $catalog = null;
17
18    private ?string $olderMajorNamespace = null;
19
20    private bool $loaded = false;
21
22    public function __construct(
23        private readonly ?string $manifestClass,
24        private readonly ReflectionProvider $reflection,
25        private readonly GeneratedManifestLocator $locator,
26    ) {}
27
28    // Tracked also while there is no Manifest, so the first make generate brings the findings.
29    /** @param Scope&DependencyTracker $scope */
30    public function findCatalogTrackedBy(Scope $scope): ?ManifestCatalog
31    {
32        $scope->trackValueDependency(ManifestResultCacheValue::class, ManifestResultCacheValue::MANIFEST_KEY);
33
34        return $this->findCatalog();
35    }
36
37    public function findCatalog(): ?ManifestCatalog
38    {
39        $this->loadOnce();
40
41        return $this->catalog;
42    }
43
44    // Tracked only while it reports, so regenerating a current tree does not invalidate every file.
45    /** @param Scope&DependencyTracker $scope */
46    public function findOlderMajorNamespaceTrackedBy(Scope $scope): ?string
47    {
48        $namespace = $this->findOlderMajorNamespace();
49
50        if ($namespace !== null) {
51            $scope->trackValueDependency(ManifestResultCacheValue::class, ManifestResultCacheValue::MANIFEST_KEY);
52        }
53
54        return $namespace;
55    }
56
57    public function findOlderMajorNamespace(): ?string
58    {
59        $this->loadOnce();
60
61        return $this->olderMajorNamespace;
62    }
63
64    private function loadOnce(): void
65    {
66        if (!$this->loaded) {
67            $this->loaded = true;
68            $this->catalog = $this->loadCatalog();
69        }
70    }
71
72    private function loadCatalog(): ?ManifestCatalog
73    {
74        $class = $this->manifestClass ?? $this->locator->findManifestClass();
75
76        if ($class === null) {
77            return null;
78        }
79
80        $separator = strrpos($class, '\\');
81        $namespace = $separator === false ? '' : substr($class, 0, $separator);
82
83        // Static reflection first: an older tree's classes fail to load against today's contracts, and PHP cannot
84        // catch that.
85        if (!$this->reflection->hasClass($namespace . '\\Actions')) {
86            $this->olderMajorNamespace = $this->reflection->hasClass($namespace . '\\Services') ? $namespace : null;
87
88            return null;
89        }
90
91        if (!class_exists($class) || !is_a($class, Manifest::class, true)) {
92            return null;
93        }
94
95        return ManifestCatalog::fromLists($class::listEntityIds(), $class::listIgnoredEntityIds(), $class::listActionNames());
96    }
97}