Passed
Pull Request — master (#48)
by Marco
02:59
created

LocateDependenciesViaComposer   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
dl 0
loc 103
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
A sourceLocatorFromAutoloadFiles() 0 22 1
A runInDirectory() 0 9 1
B __invoke() 0 27 1
A sourceLocatorFromAutoloadStatic() 0 15 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Roave\ApiCompare\LocateDependencies;
6
7
use Assert\Assert;
8
use Composer\Installer;
9
use Roave\ApiCompare\SourceLocator\StaticClassMapSourceLocator;
10
use Roave\BetterReflection\Reflection\ReflectionClass;
11
use Roave\BetterReflection\Reflection\ReflectionProperty;
12
use Roave\BetterReflection\Reflector\ClassReflector;
13
use Roave\BetterReflection\SourceLocator\Ast\Locator;
14
use Roave\BetterReflection\SourceLocator\Type\AggregateSourceLocator;
15
use Roave\BetterReflection\SourceLocator\Type\PhpInternalSourceLocator;
16
use Roave\BetterReflection\SourceLocator\Type\SingleFileSourceLocator;
17
use Roave\BetterReflection\SourceLocator\Type\SourceLocator;
18
use function array_map;
19
use function array_values;
20
use function assert;
21
use function chdir;
22
use function getcwd;
23
use function realpath;
24
use function reset;
25
26
final class LocateDependenciesViaComposer implements LocateDependencies
27
{
28
    /** @var Installer */
29
    private $installer;
30
31
    /** @var Locator */
32
    private $astLocator;
33
34
    public function __construct(Installer $installer, Locator $astLocator)
35
    {
36
        $this->installer  = $installer;
37
        $this->astLocator = $astLocator;
38
39
        // Some defaults needed for this specific implementation:
40
        $this->installer->setDevMode(false);
41
        $this->installer->setDumpAutoloader(true);
42
        $this->installer->setRunScripts(false);
43
        $this->installer->setOptimizeAutoloader(true);
44
        $this->installer->setClassMapAuthoritative(true);
45
        $this->installer->setIgnorePlatformRequirements(true);
46
    }
47
48
    public function __invoke(string $installationPath) : SourceLocator
49
    {
50
        Assert::that($installationPath)->directory();
51
        Assert::that($installationPath . '/composer.json')->file();
52
53
        $this->runInDirectory(function () : void {
54
            $this->installer->run();
55
        }, $installationPath);
56
57
        $autoloadStatic = $installationPath . '/vendor/composer/autoload_static.php';
58
59
        Assert::that($autoloadStatic)->file();
60
61
        $autoloadMappingClasses = (new ClassReflector(new SingleFileSourceLocator(
62
            $autoloadStatic,
63
            $this->astLocator
64
        )))->getAllClasses();
65
66
        Assert::that($autoloadMappingClasses)->count(1);
67
68
        /** @var ReflectionClass $generatedAutoloadClass */
69
        $generatedAutoloadClass = reset($autoloadMappingClasses);
70
71
        return new AggregateSourceLocator([
72
            $this->sourceLocatorFromAutoloadStatic($generatedAutoloadClass),
73
            $this->sourceLocatorFromAutoloadFiles($generatedAutoloadClass),
74
            new PhpInternalSourceLocator($this->astLocator),
75
        ]);
76
    }
77
78
    private function sourceLocatorFromAutoloadStatic(ReflectionClass $autoloadStatic) : SourceLocator
79
    {
80
        $classMapProperty = $autoloadStatic->getProperty('classMap');
81
82
        Assert::that($classMapProperty)->notNull();
83
84
        assert($classMapProperty instanceof ReflectionProperty);
85
        $classMap = $classMapProperty->getDefaultValue();
86
87
        Assert::that($classMap)
88
              ->isArray()
89
              ->all()
90
              ->file();
91
92
        return new StaticClassMapSourceLocator($classMap, $this->astLocator);
93
    }
94
95
    private function sourceLocatorFromAutoloadFiles(ReflectionClass $autoloadStatic) : SourceLocator
96
    {
97
        $filesMapProperty = $autoloadStatic->getProperty('files');
98
99
        Assert::that($filesMapProperty)->notNull();
100
101
        assert($filesMapProperty instanceof ReflectionProperty);
102
        $filesMap = $filesMapProperty->getDefaultValue();
103
104
        Assert::that($filesMap)
105
              ->isArray()
106
              ->all()
107
              ->file();
108
109
        return new AggregateSourceLocator(array_values(array_map(
110
            function (string $path) : SourceLocator {
111
                return new SingleFileSourceLocator(
112
                    realpath($path),
113
                    $this->astLocator
114
                );
115
            },
116
            $filesMap
117
        )));
118
    }
119
120
    private function runInDirectory(callable $callable, string $directoryOfExecution) : void
121
    {
122
        $originalDirectory = getcwd();
123
124
        try {
125
            chdir($directoryOfExecution);
126
            $callable();
127
        } finally {
128
            chdir($originalDirectory);
129
        }
130
    }
131
}
132