Passed
Push — master ( fecc04...e5efab )
by Théo
03:04
created

ApplicationFactory   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 19

Test Coverage

Coverage 5.88%

Importance

Changes 0
Metric Value
dl 0
loc 68
ccs 2
cts 34
cp 0.0588
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 19

5 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 14 1
A getVersion() 0 12 3
A createScoper() 0 16 1
A createParser() 0 4 1
A createReflector() 0 15 1
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 Humbug\PhpScoper\Scoper\SymfonyScoper;
28
use PackageVersions\Versions;
29
use PhpParser\Parser;
30
use PhpParser\ParserFactory;
31
use Roave\BetterReflection\Reflector\ClassReflector;
32
use Roave\BetterReflection\Reflector\FunctionReflector;
33
use Roave\BetterReflection\SourceLocator\Ast\Locator;
34
use Roave\BetterReflection\SourceLocator\Type\MemoizingSourceLocator;
35
use Roave\BetterReflection\SourceLocator\Type\PhpInternalSourceLocator;
36
use Symfony\Component\Filesystem\Filesystem;
37
38
class ApplicationFactory
39
{
40
    public function create(): Application
41
    {
42
        $app = new Application('PHP Scoper', static::getVersion());
43
44
        $app->addCommands([
45
            new AddPrefixCommand(
46
                new Filesystem(),
47
                static::createScoper()
48
            ),
49
            new InitCommand(),
50
        ]);
51
52
        return $app;
53
    }
54
55
    protected static function getVersion(): string
56
    {
57
        if (0 === strpos(__FILE__, 'phar:')) {
58
            return '@git_version_placeholder@';
59
        }
60
61
        $rawVersion = Versions::getVersion('humbug/php-scoper');
62
63
        [$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...
64
65
        return (1 === preg_match('/9{7}/', $prettyVersion)) ? $commitHash : $prettyVersion;
66
    }
67
68
    protected static function createScoper(): Scoper
69
    {
70
        return new PatchScoper(
71
            new PhpScoper(
72
                static::createParser(),
73
                new JsonFileScoper(
74
                    new InstalledPackagesScoper(
75
                        new SymfonyScoper(
76
                            new NullScoper()
77
                        )
78
                    )
79
                ),
80
                new TraverserFactory(static::createReflector())
81
            )
82
        );
83
    }
84
85 553
    protected static function createParser(): Parser
86
    {
87 553
        return (new ParserFactory())->create(ParserFactory::ONLY_PHP7);
88
    }
89
90
    protected static function createReflector(): Reflector
91
    {
92
        $phpParser = static::createParser();
93
        $astLocator = new Locator($phpParser);
94
95
        $sourceLocator = new MemoizingSourceLocator(
96
            new PhpInternalSourceLocator($astLocator)
97
        );
98
        $classReflector = new ClassReflector($sourceLocator);
99
100
        return new Reflector(
101
            $classReflector,
102
            new FunctionReflector($sourceLocator, $classReflector)
103
        );
104
    }
105
}
106