Passed
Pull Request — master (#480)
by Théo
02:10
created

Container::getScoper()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 10
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 19
rs 9.9332
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\Lexer;
25
use PhpParser\Parser;
26
use PhpParser\ParserFactory;
27
use Symfony\Component\Filesystem\Filesystem;
28
29
final class Container
30
{
31
    private Filesystem $filesystem;
32
    private Parser $parser;
33
    private Reflector $reflector;
34
    private Scoper $scoper;
35
36
    public function getFileSystem(): Filesystem
37
    {
38
        if (!isset($this->filesystem)) {
39
            $this->filesystem = new Filesystem();
40
        }
41
42
        return $this->filesystem;
43
    }
44
45
    public function getScoper(): Scoper
46
    {
47
        if (!isset($this->scoper)) {
48
            $this->scoper = new PatchScoper(
49
                new PhpScoper(
50
                    $this->getParser(),
51
                    new JsonFileScoper(
52
                        new InstalledPackagesScoper(
53
                            new SymfonyScoper(
54
                                new NullScoper()
55
                            )
56
                        )
57
                    ),
58
                    new TraverserFactory($this->getReflector())
59
                )
60
            );
61
        }
62
63
        return $this->scoper;
64
    }
65
66
    public function getParser(): Parser
67
    {
68
        if (!isset($this->parser)) {
69
            $this->parser = (new ParserFactory())->create(ParserFactory::ONLY_PHP7, new Lexer());
70
        }
71
72
        return $this->parser;
73
    }
74
75
    public function getReflector(): Reflector
76
    {
77
        if (!isset($this->reflector)) {
78
            $this->reflector = new Reflector();
79
        }
80
81
        return $this->reflector;
82
    }
83
}
84