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

Container::getLexer()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 7
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\Scoper\ScoperFactory;
21
use Humbug\PhpScoper\Symbol\EnrichedReflectorFactory;
22
use Humbug\PhpScoper\Symbol\Reflector;
23
use PhpParser\Lexer;
24
use PhpParser\Parser;
25
use PhpParser\ParserFactory;
26
use PhpParser\PrettyPrinter\Standard;
27
use PhpParser\PrettyPrinterAbstract;
28
use Symfony\Component\Filesystem\Filesystem;
29
30
final class Container
31
{
32
    private Filesystem $filesystem;
33
    private ConfigurationFactory $configFactory;
34
    private Parser $parser;
35
    private Lexer $lexer;
36
    private PrettyPrinterAbstract $printer;
37
    private Reflector $reflector;
38
    private ScoperFactory $scoperFactory;
39
    private EnrichedReflectorFactory $enrichedReflectorFactory;
40
41
    public function getFileSystem(): Filesystem
42
    {
43
        if (!isset($this->filesystem)) {
44
            $this->filesystem = new Filesystem();
45
        }
46
47
        return $this->filesystem;
48
    }
49
50
    public function getConfigurationFactory(): ConfigurationFactory
51
    {
52
        if (!isset($this->configFactory)) {
53
            $this->configFactory = new ConfigurationFactory(
54
                $this->getFileSystem(),
55
                new SymbolsConfigurationFactory(
56
                    new RegexChecker(),
57
                ),
58
            );
59
        }
60
61
        return $this->configFactory;
62
    }
63
64
    public function getScoperFactory(): ScoperFactory
65
    {
66
        if (!isset($this->scoperFactory)) {
67
            $this->scoperFactory = new ScoperFactory(
68
                $this->getParser(),
69
                $this->getPrinter(),
70
                $this->getEnrichedReflectorFactory(),
71
            );
72
        }
73
74
        return $this->scoperFactory;
75
    }
76
77
    public function getParser(): Parser
78
    {
79
        if (!isset($this->parser)) {
80
            $this->parser = (new ParserFactory())->create(
81
                ParserFactory::PREFER_PHP7,
82
                $this->getLexer(),
83
            );
84
        }
85
86
        return $this->parser;
87
    }
88
89
    public function getLexer(): Lexer
90
    {
91
        if (!isset($this->lexer)) {
92
            $this->lexer = new Lexer();
93
        }
94
95
        return $this->lexer;
96
    }
97
98
    public function getPrinter(): PrettyPrinterAbstract
99
    {
100
        if (!isset($this->printer)) {
101
            $this->printer = new Standard();
102
        }
103
104
        return $this->printer;
105
    }
106
107
    public function getReflector(): Reflector
108
    {
109
        if (!isset($this->reflector)) {
110
            $this->reflector = Reflector::createWithPhpStormStubs();
111
        }
112
113
        return $this->reflector;
114
    }
115
116
    public function getEnrichedReflectorFactory(): EnrichedReflectorFactory
117
    {
118
        if (!isset($this->enrichedReflectorFactory)) {
119
            $this->enrichedReflectorFactory = new EnrichedReflectorFactory(
120
                $this->getReflector(),
121
            );
122
        }
123
124
        return $this->enrichedReflectorFactory;
125
    }
126
}
127