Completed
Pull Request — 1.x (#352)
by Akihito
01:20
created

CompileDiScripts::scanClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BEAR\Package\Compiler;
6
7
use BEAR\AppMeta\AbstractAppMeta;
8
use BEAR\Package\Compiler;
9
use BEAR\Resource\NamedParameterInterface;
10
use BEAR\Sunday\Extension\Application\AppInterface;
11
use Doctrine\Common\Annotations\Reader;
12
13
use function assert;
14
use function class_exists;
15
16
/**
17
 * Compiler Component
18
 */
19
final class CompileDiScripts
20
{
21
    /** @var Compiler */
22
    private $compiler;
23
24
    /** @var ScanClass */
25
    private $compilerScanClass;
26
27
    public function __construct(Compiler $compiler, ScanClass $compilerScanClass)
28
    {
29
        $this->compiler = $compiler;
30
        $this->compilerScanClass = $compilerScanClass;
31
    }
32
33
    public function __invoke(AbstractAppMeta $appMeta): void
34
    {
35
        $reader = $this->compiler->getInjector()->getInstance(Reader::class);
36
        assert($reader instanceof Reader);
37
        $namedParams = $this->compiler->getInjector()->getInstance(NamedParameterInterface::class);
38
        assert($namedParams instanceof NamedParameterInterface);
39
        // create DI factory class and AOP compiled class for all resources and save $app cache.
40
        $app = $this->compiler->getInjector()->getInstance(AppInterface::class);
41
        assert($app instanceof AppInterface);
42
43
        // check resource injection and create annotation cache
44
        $metas = $appMeta->getResourceListGenerator();
45
        /** @var array{0: string, 1:string} $meta */
46
        foreach ($metas as $meta) {
47
            [$className] = $meta;
0 ignored issues
show
Bug introduced by
The variable $className does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
48
            assert(class_exists($className));
49
            $this->scanClass($reader, $namedParams, $className);
50
        }
51
    }
52
53
    /**
54
     * Save annotation and method meta information
55
     *
56
     * @param class-string<T> $className
0 ignored issues
show
Documentation introduced by
The doc-type class-string<T> could not be parsed: Unknown type name "class-string" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
57
     *
58
     * @template T
59
     */
60
    private function scanClass(Reader $reader, NamedParameterInterface $namedParams, string $className): void
61
    {
62
        ($this->compilerScanClass)($reader, $namedParams, $className);
63
    }
64
}
65