Passed
Push — master ( 812a1d...639186 )
by Théo
02:22
created

ComposerConfiguration::retrieveVendorDir()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 5
nc 3
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the box project.
7
 *
8
 * (c) Kevin Herrera <[email protected]>
9
 *     Théo Fidry <[email protected]>
10
 *
11
 * This source file is subject to the MIT license that is bundled
12
 * with this source code in the file LICENSE.
13
 */
14
15
namespace KevinGH\Box\Composer;
16
17
use function KevinGH\Box\FileSystem\make_path_absolute;
18
19
/**
20
 * @private
21
 */
22
final class ComposerConfiguration
23
{
24
    /**
25
     * Attempts to locate the `composer.json` and `composer.lock` files in the provided base-path in order to collect
26
     * all the dev packages.
27
     *
28
     * @return string[] Dev package paths
29
     */
30
    public static function retrieveDevPackages(string $basePath, ?array $composerJsonDecodedContents, ?array $composerLockDecodedContents): array
31
    {
32
        if (null === $composerJsonDecodedContents || null === $composerLockDecodedContents) {
33
            return [];
34
        }
35
36
        return self::getDevPackagePaths(
37
            $basePath,
38
            $composerJsonDecodedContents,
39
            $composerLockDecodedContents
40
        );
41
    }
42
43
    /**
44
     * @return string[] Dev packages paths
45
     */
46
    private static function getDevPackagePaths(
47
        string $basePath,
48
        array $composerJsonDecodedContents,
49
        array $composerLockDecodedContents
50
    ): array {
51
        $vendorDir = make_path_absolute(
52
            self::retrieveVendorDir($composerJsonDecodedContents),
53
            $basePath
54
        );
55
56
        $packageNames = self::retrieveDevPackageNames($composerLockDecodedContents);
57
58
        return array_filter(
59
            array_map(
60
                function (string $packageName) use ($vendorDir): ?string {
61
                    $realPath = realpath($vendorDir.DIRECTORY_SEPARATOR.$packageName);
62
63
                    return false !== $realPath ? $realPath : null;
64
                },
65
                $packageNames
66
            )
67
        );
68
    }
69
70
    private static function retrieveVendorDir(array $composerJsonDecodedContents): string
71
    {
72
        if (!array_key_exists('config', $composerJsonDecodedContents)) {
73
            return 'vendor';
74
        }
75
76
        if (!array_key_exists('vendor-dir', $composerJsonDecodedContents['config'])) {
77
            return 'vendor';
78
        }
79
80
        return $composerJsonDecodedContents['config']['vendor-dir'];
81
    }
82
83
    /**
84
     * @return string[] Names of the dev packages
85
     */
86
    private static function retrieveDevPackageNames(array $composerLockDecodedContents): array
87
    {
88
        if (!array_key_exists('packages-dev', $composerLockDecodedContents)) {
89
            return [];
90
        }
91
92
        return array_map(
93
            function (array $package): string {
94
                return $package['name'];
95
            },
96
            $composerLockDecodedContents['packages-dev']
97
        );
98
    }
99
}
100