Passed
Push — master ( 59e393...5fcd0b )
by Pol
02:49
created

Config::findFilesToIncludeInConfiguration()   B

Complexity

Conditions 9
Paths 12

Size

Total Lines 86
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 34
CRAP Score 9.4027

Importance

Changes 0
Metric Value
cc 9
eloc 44
nc 12
nop 1
dl 0
loc 86
ccs 34
cts 41
cp 0.8293
crap 9.4027
rs 7.6604
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types = 1);
4
5
namespace PhpTaskman\Core\Config;
6
7
use PhpTaskman\Core\Taskman;
8
9
final class Config
10
{
11
    /**
12
     * Find the files to include in the configuration.
13
     *
14
     * @param string $cwd
15
     *   The current working directory.
16
     *
17
     * @return string[]
18
     *   The list of all the YAML files to include.
19
     */
20 1
    public static function findFilesToIncludeInConfiguration($cwd)
21
    {
22
        // Get the vendor-bin property from the composer.json.
23 1
        $composer = Taskman::getComposerFromDirectory($cwd);
24
25 1
        if (null === $composer) {
26
            return [];
27
        }
28
29 1
        $vendorDir = $composer->get('vendor-dir') ?? $cwd . '/vendor';
30
31
        // Keep a reference of the default filename that we need to load from
32
        // each packages.
33
        $filesToLoad = [
34 1
            'taskman.yml.dist',
35
            'taskman.yml',
36
        ];
37
38
        // Load default paths.
39
        $filesystemPaths = [
40 1
            __DIR__ . '/../config/default.yml',
41
            __DIR__ . '/../default.yml',
42 1
            static::getLocalConfigurationFilepath(),
43
        ];
44
45
        // Check if composer.lock exists.
46 1
        $composerLockPath = \realpath($cwd . '/composer.lock');
47
48 1
        if (false === $composerLockPath) {
49
            return [];
50
        }
51
52
        // Convert the composer.lock into a Config object.
53 1
        $composerLockConfig = Taskman::getConfigFromArray(\json_decode(
54 1
            \file_get_contents($cwd . '/composer.lock'),
55 1
            true
56
        ));
57
58
        // Get the dependencies packages directories.
59 1
        $packageDirectories = \array_filter(
60 1
            \array_map(
61
                static function ($package) use ($cwd, $vendorDir) {
0 ignored issues
show
Unused Code introduced by
The import $cwd is not used and could be removed.

This check looks for imports that have been defined, but are not used in the scope.

Loading history...
62 1
                    return \realpath($vendorDir . '/' . $package['name']);
63 1
                },
64 1
                \array_merge(
65 1
                    $composerLockConfig->get('packages', []),
66 1
                    $composerLockConfig->get('packages-dev', [])
67
                )
68
            )
69
        );
70
71 1
        $packageDirectories[] = $cwd;
72
73
        // Loop over each composer.json, deduct the package directory and probe for files to include.
74 1
        foreach ($packageDirectories as $packageDirectory) {
75 1
            foreach ($filesToLoad as $taskmanFile) {
76 1
                foreach ([$packageDirectory, $cwd] as $directory) {
77 1
                    $candidateFile = $directory . '/' . $taskmanFile;
78 1
                    $filesystemPaths[$candidateFile] = $candidateFile;
79
                }
80
            }
81
82 1
            $composer = Taskman::getComposerFromDirectory(
83 1
                $packageDirectory
84
            );
85
86 1
            if (null === $composer) {
87
                continue;
88
            }
89
90 1
            $extraFiles = $composer->get('extra.taskman.files', []);
91
92 1
            if (empty($extraFiles)) {
93 1
                continue;
94
            }
95
96
            foreach ($extraFiles as $commandFile) {
97
                $filesystemPaths[$commandFile] = $commandFile;
98
                $commandFile = $packageDirectory . '/' . $commandFile;
99
                $filesystemPaths[$commandFile] = $commandFile;
100
            }
101
        }
102
103 1
        return \array_filter(
104 1
            static::resolveImports(...\array_values($filesystemPaths)),
105 1
            'file_exists'
106
        );
107
    }
108
109
    /**
110
     * Get the local configuration filepath.
111
     *
112
     * @param string $configuration_file
113
     *   The default filepath.
114
     *
115
     * @return null|string
116
     *   The local configuration file path, or null if it doesn't exist.
117
     */
118 1
    public static function getLocalConfigurationFilepath($configuration_file = 'phptaskman/taskman.yml')
119
    {
120 1
        if ($config = \getenv('PHPTASKMAN_CONFIG')) {
121
            return $config;
122
        }
123
124 1
        if ($config = \getenv('XDG_CONFIG_HOME')) {
125
            return $config . '/' . $configuration_file;
126
        }
127
128 1
        if ($home = \getenv('HOME')) {
0 ignored issues
show
Unused Code introduced by
The assignment to $home is dead and can be removed.
Loading history...
129 1
            return \getenv('HOME') . '/.config/' . $configuration_file;
130
        }
131
132
        return null;
133
    }
134
135
    /**
136
     * Resolve YAML configurations files containing imports.
137
     *
138
     * Handles circular dependencies by ignoring them.
139
     *
140
     * @param string[] ...$filepaths
141
     *   A list of YML filepath to parse.
142
     *
143
     * @return string[]
144
     *   The list of all the YAML files to include.
145
     */
146 1
    public static function resolveImports(...$filepaths)
147
    {
148 1
        return (new YamlRecursivePathsFinder($filepaths))
149 1
            ->getAllPaths();
150
    }
151
}
152