Passed
Pull Request — master (#652)
by Théo
02:03
created

Container::getScoperFactory()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 0
dl 0
loc 11
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the humbug/php-scoper package.
7
 *
8
 * Copyright (c) 2017 Théo FIDRY <[email protected]>,
9
 *                    Pádraic Brady <[email protected]>
10
 *
11
 * For the full copyright and license information, please view the LICENSE
12
 * file that was distributed with this source code.
13
 */
14
15
namespace Humbug\PhpScoper;
16
17
use Humbug\PhpScoper\Configuration\ConfigurationFactory;
18
use Humbug\PhpScoper\Configuration\RegexChecker;
19
use Humbug\PhpScoper\Configuration\SymbolsConfigurationFactory;
20
use Humbug\PhpScoper\PhpParser\Printer\Printer;
21
use Humbug\PhpScoper\PhpParser\Printer\StandardPrinter;
22
use Humbug\PhpScoper\Scoper\ScoperFactory;
23
use Humbug\PhpScoper\Symbol\EnrichedReflectorFactory;
24
use Humbug\PhpScoper\Symbol\Reflector;
25
use PhpParser\Lexer;
26
use PhpParser\Parser;
27
use PhpParser\ParserFactory;
28
use PhpParser\PrettyPrinter\Standard;
29
use Symfony\Component\Filesystem\Filesystem;
30
31
final class Container
32
{
33
    private Filesystem $filesystem;
34
    private ConfigurationFactory $configFactory;
35
    private Parser $parser;
36
    private Reflector $reflector;
37
    private ScoperFactory $scoperFactory;
38
    private EnrichedReflectorFactory $enrichedReflectorFactory;
39
    private Printer $printer;
40
    private Lexer $lexer;
41
42
    public function getFileSystem(): Filesystem
43
    {
44
        if (!isset($this->filesystem)) {
45
            $this->filesystem = new Filesystem();
46
        }
47
48
        return $this->filesystem;
49
    }
50
51
    public function getConfigurationFactory(): ConfigurationFactory
52
    {
53
        if (!isset($this->configFactory)) {
54
            $this->configFactory = new ConfigurationFactory(
55
                $this->getFileSystem(),
56
                new SymbolsConfigurationFactory(
57
                    new RegexChecker(),
58
                ),
59
            );
60
        }
61
62
        return $this->configFactory;
63
    }
64
65
    public function getScoperFactory(): ScoperFactory
66
    {
67
        if (!isset($this->scoperFactory)) {
68
            $this->scoperFactory = new ScoperFactory(
69
                $this->getParser(),
70
                $this->getEnrichedReflectorFactory(),
71
                $this->getPrinter(),
72
            );
73
        }
74
75
        return $this->scoperFactory;
76
    }
77
78
    public function getParser(): Parser
79
    {
80
        if (!isset($this->parser)) {
81
            $this->parser = (new ParserFactory())->create(
82
                ParserFactory::PREFER_PHP7,
83
                $this->getLexer(),
84
            );
85
        }
86
87
        return $this->parser;
88
    }
89
90
    public function getReflector(): Reflector
91
    {
92
        if (!isset($this->reflector)) {
93
            $this->reflector = Reflector::createWithPhpStormStubs();
94
        }
95
96
        return $this->reflector;
97
    }
98
99
    public function getEnrichedReflectorFactory(): EnrichedReflectorFactory
100
    {
101
        if (!isset($this->enrichedReflectorFactory)) {
102
            $this->enrichedReflectorFactory = new EnrichedReflectorFactory(
103
                $this->getReflector(),
104
            );
105
        }
106
107
        return $this->enrichedReflectorFactory;
108
    }
109
110
    public function getPrinter(): Printer
111
    {
112
        if (!isset($this->printer)) {
113
            $this->printer = new StandardPrinter(
114
                new Standard(),
115
            );
116
        }
117
118
        return $this->printer;
119
    }
120
121
    public function getLexer(): Lexer
122
    {
123
        if (!isset($this->lexer)) {
124
            $this->lexer = new Lexer();
125
        }
126
127
        return $this->lexer;
128
    }
129
}
130