Passed
Push — master ( d45875...f59561 )
by Théo
02:44
created

ApplicationFactory::createReflector()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
ccs 0
cts 9
cp 0
crap 2
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\Console;
16
17
use Humbug\PhpScoper\Console\Command\AddPrefixCommand;
18
use Humbug\PhpScoper\Console\Command\InitCommand;
19
use Humbug\PhpScoper\PhpParser\TraverserFactory;
20
use Humbug\PhpScoper\Reflector;
21
use Humbug\PhpScoper\Scoper;
22
use Humbug\PhpScoper\Scoper\Composer\InstalledPackagesScoper;
23
use Humbug\PhpScoper\Scoper\Composer\JsonFileScoper;
24
use Humbug\PhpScoper\Scoper\NullScoper;
25
use Humbug\PhpScoper\Scoper\PatchScoper;
26
use Humbug\PhpScoper\Scoper\PhpScoper;
27
use PackageVersions\Versions;
28
use PhpParser\Parser;
29
use PhpParser\ParserFactory;
30
use Roave\BetterReflection\Reflector\ClassReflector;
31
use Roave\BetterReflection\Reflector\FunctionReflector;
32
use Roave\BetterReflection\SourceLocator\Ast\Locator;
33
use Roave\BetterReflection\SourceLocator\Type\MemoizingSourceLocator;
34
use Roave\BetterReflection\SourceLocator\Type\PhpInternalSourceLocator;
35
use Symfony\Component\Filesystem\Filesystem;
36
37
class ApplicationFactory
38
{
39
    public function create(): Application
40
    {
41
        $app = new Application('PHP Scoper', static::getVersion());
42
43
        $app->addCommands([
44
            new AddPrefixCommand(
45
                new Filesystem(),
46
                static::createScoper()
47
            ),
48
            new InitCommand(),
49
        ]);
50
51
        return $app;
52
    }
53
54
    protected static function getVersion(): string
55
    {
56
        if (0 === strpos(__FILE__, 'phar:')) {
57
            return '@git_version_placeholder@';
58
        }
59
60
        $rawVersion = Versions::getVersion('humbug/php-scoper');
61
62
        [$prettyVersion, $commitHash] = explode('@', $rawVersion);
0 ignored issues
show
Bug introduced by
The variable $prettyVersion does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $commitHash does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
63
64
        return (1 === preg_match('/9{7}/', $prettyVersion)) ? $commitHash : $prettyVersion;
65
    }
66
67
    protected static function createScoper(): Scoper
68
    {
69
        return new PatchScoper(
70
            new PhpScoper(
71
                static::createParser(),
72
                new JsonFileScoper(
73
                    new InstalledPackagesScoper(
74
                        new NullScoper()
75
                    )
76
                ),
77
                new TraverserFactory(static::createReflector())
78
            )
79
        );
80
    }
81
82 471
    protected static function createParser(): Parser
83
    {
84 471
        return (new ParserFactory())->create(ParserFactory::ONLY_PHP7);
85
    }
86
87
    protected static function createReflector(): Reflector
88
    {
89
        $phpParser = static::createParser();
90
        $astLocator = new Locator($phpParser);
91
92
        $sourceLocator = new MemoizingSourceLocator(
93
            new PhpInternalSourceLocator($astLocator)
94
        );
95
        $classReflector = new ClassReflector($sourceLocator);
96
97
        return new Reflector(
98
            $classReflector,
99
            new FunctionReflector($sourceLocator, $classReflector)
100
        );
101
    }
102
}
103