Loader   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 24
dl 0
loc 55
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A default() 0 6 1
A make() 0 16 2
A generate() 0 7 1
A __construct() 0 8 1
1
<?php declare(strict_types=1);
2
3
namespace Stratadox\PhpGenerics;
4
5
use Doctrine\Common\Annotations\AnnotationException;
6
use Stratadox\PhpGenerics\Dissector\TypeInformation;
7
use Stratadox\PhpGenerics\Generator\PhpSourceGenerator;
8
use Stratadox\PhpGenerics\Generator\SourceGenerator;
9
use Stratadox\PhpGenerics\Generator\Type\TypeArguments;
10
use Stratadox\PhpGenerics\Generator\TypeArgumentFactory;
11
use Stratadox\PhpGenerics\Writer\FileWriter;
12
use function get_declared_classes;
13
use function in_array;
14
15
class Loader
16
{
17
    /** @var SourceGenerator */
18
    private $generator;
19
    /** @var FileWriter */
20
    private $writer;
21
    /** @var TypeArgumentFactory */
22
    private $types;
23
24
    public function __construct(
25
        SourceGenerator $generator,
26
        FileWriter $writer,
27
        TypeArgumentFactory $types
28
    ) {
29
        $this->generator = $generator;
30
        $this->writer = $writer;
31
        $this->types = $types;
32
    }
33
34
    /** @throws AnnotationException */
35
    public static function default(): self
36
    {
37
        return new self(
38
            PhpSourceGenerator::default(),
39
            FileWriter::default(),
40
            new TypeArgumentFactory()
41
        );
42
    }
43
44
    public function generate(TypeInformation $generic): void
45
    {
46
        $this->make(
47
            $generic->className(),
48
            $generic->namespace(),
49
            $generic->baseClass(),
50
            $this->types->for(...$generic->typeArguments())
51
        );
52
    }
53
54
    private function make(
55
        string $class,
56
        string $namespace,
57
        string $base,
58
        TypeArguments $typeArguments
59
    ): void {
60
        if (in_array($namespace . '\\' . $class, get_declared_classes())) {
61
            return;
62
        }
63
        $this->writer->write($namespace, $class, $this->generator->generate(
64
            $namespace,
65
            $class,
66
            $base,
67
            $typeArguments
68
        ));
69
        require $this->writer->pathFor($namespace, $class);
70
    }
71
}
72