LocateDependenciesViaComposer   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 23
c 2
b 0
f 0
dl 0
loc 53
rs 10
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A runInDirectory() 0 9 1
A __invoke() 0 21 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Roave\BackwardCompatibility\LocateDependencies;
6
7
use Assert\Assert;
8
use Composer\Installer;
9
use Roave\BetterReflection\SourceLocator\Ast\Locator;
10
use Roave\BetterReflection\SourceLocator\SourceStubber\ReflectionSourceStubber;
11
use Roave\BetterReflection\SourceLocator\Type\AggregateSourceLocator;
12
use Roave\BetterReflection\SourceLocator\Type\Composer\Factory\MakeLocatorForInstalledJson;
13
use Roave\BetterReflection\SourceLocator\Type\PhpInternalSourceLocator;
14
use Roave\BetterReflection\SourceLocator\Type\SourceLocator;
15
use function assert;
16
use function Safe\chdir;
17
use function Safe\getcwd;
18
19
final class LocateDependenciesViaComposer implements LocateDependencies
20
{
21
    /** @var Locator */
22
    private $astLocator;
23
24
    /** @var callable */
25
    private $makeComposerInstaller;
26
27
    /**
28
     * @psalm-param callable () : Installer $makeComposerInstaller
29
     */
30
    public function __construct(
31
        callable $makeComposerInstaller,
32
        Locator $astLocator
33
    ) {
34
        // This is needed because the CWD of composer cannot be changed at runtime, but only at startup
35
        $this->makeComposerInstaller = $makeComposerInstaller;
36
        $this->astLocator            = $astLocator;
37
    }
38
39
    public function __invoke(string $installationPath) : SourceLocator
40
    {
41
        Assert::that($installationPath . '/composer.json')->file();
42
43
        $this->runInDirectory(function () use ($installationPath) : void {
44
            $installer = ($this->makeComposerInstaller)($installationPath);
45
46
            assert($installer instanceof Installer);
47
48
            // Some defaults needed for this specific implementation:
49
            $installer->setDevMode(false);
50
            $installer->setDumpAutoloader(false);
51
            $installer->setRunScripts(false);
52
            $installer->setIgnorePlatformRequirements(true);
53
54
            $installer->run();
55
        }, $installationPath);
56
57
        return new AggregateSourceLocator([
58
            (new MakeLocatorForInstalledJson())->__invoke($installationPath, $this->astLocator),
59
            new PhpInternalSourceLocator($this->astLocator, new ReflectionSourceStubber()),
60
        ]);
61
    }
62
63
    private function runInDirectory(callable $callable, string $directoryOfExecution) : void
64
    {
65
        $originalDirectory = getcwd();
66
67
        try {
68
            chdir($directoryOfExecution);
69
            $callable();
70
        } finally {
71
            chdir($originalDirectory);
72
        }
73
    }
74
}
75