Passed
Push — master ( bb58bc...1c054e )
by Théo
02:40 queued 01:10
created

Container::getParser()   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\PhpParser\TraverserFactory;
18
use Humbug\PhpScoper\Scoper\Composer\InstalledPackagesScoper;
19
use Humbug\PhpScoper\Scoper\Composer\JsonFileScoper;
20
use Humbug\PhpScoper\Scoper\NullScoper;
21
use Humbug\PhpScoper\Scoper\PatchScoper;
22
use Humbug\PhpScoper\Scoper\PhpScoper;
23
use Humbug\PhpScoper\Scoper\SymfonyScoper;
24
use PhpParser\Parser;
25
use PhpParser\ParserFactory;
26
use Roave\BetterReflection\Reflector\ClassReflector;
27
use Roave\BetterReflection\SourceLocator\Ast\Locator;
28
use Roave\BetterReflection\SourceLocator\Type\MemoizingSourceLocator;
29
use Roave\BetterReflection\SourceLocator\Type\PhpInternalSourceLocator;
30
31
final class Container
32
{
33
    private $parser;
34
    private $reflector;
35
    private $scoper;
36
37
    public function getScoper(): Scoper
38
    {
39
        if (null === $this->scoper) {
40
            $this->scoper = new PatchScoper(
41
                new PhpScoper(
42
                    $this->getParser(),
43
                    new JsonFileScoper(
44
                        new InstalledPackagesScoper(
45
                            new SymfonyScoper(
46
                                new NullScoper()
47
                            )
48
                        )
49
                    ),
50
                    new TraverserFactory($this->getReflector())
51
                )
52
            );
53
        }
54
55
        return $this->scoper;
56
    }
57
58
    public function getParser(): Parser
59
    {
60
        if (null === $this->parser) {
61
            $this->parser = (new ParserFactory())->create(ParserFactory::ONLY_PHP7);
62
        }
63
64
        return $this->parser;
65
    }
66
67
    public function getReflector(): Reflector
68
    {
69
        if (null === $this->reflector) {
70
            $phpParser = $this->getParser();
71
            $astLocator = new Locator($phpParser);
72
73
            $sourceLocator = new MemoizingSourceLocator(
74
                new PhpInternalSourceLocator($astLocator)
75
            );
76
            $classReflector = new ClassReflector($sourceLocator);
77
78
            $this->reflector = new Reflector($classReflector);
79
        }
80
81
        return $this->reflector;
82
    }
83
}
84