Completed
Push — master ( e28430...c0c2e2 )
by Marco
05:49 queued 11s
created

LocateComposerPackageDirectDependenciesSourceFiles   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 22
c 1
b 0
f 0
dl 0
loc 51
ccs 24
cts 24
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 19 4
A getInstalledPackages() 0 18 3
1
<?php
2
3
namespace ComposerRequireChecker\FileLocator;
4
5
use ComposerRequireChecker\Exception\DependenciesNotInstalledException;
6
use ComposerRequireChecker\Exception\NotReadableException;
7
use ComposerRequireChecker\JsonLoader;
8
use Generator;
9
use function array_key_exists;
10
use function file_get_contents;
11
use function json_decode;
12
13
final class LocateComposerPackageDirectDependenciesSourceFiles
14
{
15 13
    public function __invoke(string $composerJsonPath): Generator
16
    {
17 13
        $packageDir = dirname($composerJsonPath);
18
19 13
        $composerJson = json_decode(file_get_contents($composerJsonPath), true);
20 13
        $configVendorDir = $composerJson['config']['vendor-dir'] ?? 'vendor';
21 13
        $vendorDirs = [];
22 13
        foreach ($composerJson['require'] ?? [] as $vendorName => $vendorRequiredVersion) {
23 9
            $vendorDirs[$vendorName] = $packageDir . '/' . $configVendorDir . '/' . $vendorName;
24
        };
25
26 13
        $installedPackages = $this->getInstalledPackages($packageDir . '/' . $configVendorDir);
27
28 12
        foreach ($vendorDirs as $vendorName => $vendorDir) {
29 9
            if (!array_key_exists($vendorName, $installedPackages)) {
30 5
                continue;
31
            }
32
33 8
            yield from (new LocateComposerPackageSourceFiles())->__invoke($installedPackages[$vendorName], $vendorDir);
34
        }
35 12
    }
36
37
38
    /**
39
     * Lookup each vendor package's composer.json info from installed.json
40
     *
41
     * @param string $vendorDir
42
     *
43
     * @return array Keys are the package name and value is the composer.json as an array
44
     * @throws DependenciesNotInstalledException When composer install/update has not been run
45
     */
46 13
    private function getInstalledPackages(string $vendorDir): array
47
    {
48
        try {
49 13
            $installedData = (new JsonLoader($vendorDir . '/composer/installed.json'))->getData();
50 1
        } catch (NotReadableException $e) {
51 1
            $message = 'The composer dependencies have not been installed, run composer install/update first';
52 1
            throw new DependenciesNotInstalledException($message);
53
        }
54
55 12
        $installedPackages = [];
56
57 12
        $packages = $installedData['packages'] ?? $installedData;
58 12
        foreach ($packages as $vendorJson) {
59 8
            $vendorName = $vendorJson['name'];
60 8
            $installedPackages[$vendorName] = $vendorJson;
61
        }
62
63 12
        return $installedPackages;
64
    }
65
}
66