Passed
Push — master ( b5e489...38c4d8 )
by Pol
02:43
created

Config::findFilesToIncludeInConfiguration()   B

Complexity

Conditions 7
Paths 11

Size

Total Lines 77
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 32
CRAP Score 7.2833

Importance

Changes 0
Metric Value
cc 7
eloc 42
nc 11
nop 1
dl 0
loc 77
ccs 32
cts 39
cp 0.8205
crap 7.2833
rs 8.3146
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
use Symfony\Component\Finder\SplFileInfo;
9
10
final class Config
11
{
12
    /**
13
     * Find the files to include in the configuration.
14
     *
15
     * @param string $cwd
16
     *   The current working directory.
17
     *
18
     * @return string[]
19
     *   The list of all the YAML files to include.
20
     */
21 1
    public static function findFilesToIncludeInConfiguration($cwd)
22
    {
23
        // Get the vendor-bin property from the composer.json.
24 1
        $composer = Taskman::getComposerFromDirectory($cwd);
25
26 1
        if (false === $composer->getComposer()) {
27
            return [];
28
        }
29
30 1
        $vendorDir = $composer->getConfig('vendor-dir') ?? $cwd . '/vendor';
31
32
        // Keep a reference of the default filename that we need to load from
33
        // each packages.
34
        $filesToLoad = [
35 1
            'taskman.yml.dist',
36
            'taskman.yml',
37
        ];
38
39
        // Load default paths.
40
        $filesystemPaths = [
41 1
            __DIR__ . '/../config/default.yml',
42
            __DIR__ . '/../default.yml',
43 1
            static::getLocalConfigurationFilepath(),
44
        ];
45
46 1
        $composerLock = \json_decode(
47 1
            \file_get_contents($cwd . '/composer.lock'),
48 1
            true
49 1
        ) + ['packages' => [], 'packages-dev' => []];
50
51 1
        $packageDirectories = \array_filter(
52 1
            \array_map(
53
                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...
54 1
                    return \realpath($vendorDir . '/' . $package['name']);
55 1
                },
56 1
                \array_merge(
57 1
                    $composerLock['packages'],
58 1
                    $composerLock['packages-dev']
59
                )
60
            )
61
        );
62
63 1
        $packageDirectories[] = $cwd;
64
65
        // Loop over each composer.json, deduct the package directory and probe for files to include.
66
        // @var SplFileInfo $file
67 1
        foreach ($packageDirectories as $packageDirectory) {
68 1
            foreach ($filesToLoad as $taskmanFile) {
69 1
                foreach ([$packageDirectory, $cwd] as $directory) {
70 1
                    $candidateFile = $directory . '/' . $taskmanFile;
71 1
                    $filesystemPaths[$candidateFile] = $candidateFile;
72
                }
73
            }
74
75 1
            $composer = Taskman::getComposerFromDirectory(
76 1
                $packageDirectory
77
            );
78
79 1
            $extra = $composer->getExtra();
80
81 1
            if (empty($extra)) {
82 1
                continue;
83
            }
84
85
            $extra += ['taskman' => []];
86
            $extra['taskman'] += ['files' => []];
87
88
            foreach ($extra['taskman']['files'] as $commandFile) {
89
                $filesystemPaths[$commandFile] = $commandFile;
90
                $commandFile = $packageDirectory . '/' . $commandFile;
91
                $filesystemPaths[$commandFile] = $commandFile;
92
            }
93
        }
94
95 1
        return \array_filter(
96 1
            static::resolveImports(...\array_values($filesystemPaths)),
97 1
            'file_exists'
98
        );
99
    }
100
101
    /**
102
     * Get the local configuration filepath.
103
     *
104
     * @param string $configuration_file
105
     *   The default filepath.
106
     *
107
     * @return null|string
108
     *   The local configuration file path, or null if it doesn't exist.
109
     */
110 1
    public static function getLocalConfigurationFilepath($configuration_file = 'phptaskman/taskman.yml')
111
    {
112 1
        if ($config = \getenv('PHPTASKMAN_CONFIG')) {
113
            return $config;
114
        }
115
116 1
        if ($config = \getenv('XDG_CONFIG_HOME')) {
117
            return $config . '/' . $configuration_file;
118
        }
119
120 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...
121 1
            return \getenv('HOME') . '/.config/' . $configuration_file;
122
        }
123
124
        return null;
125
    }
126
127
    /**
128
     * Resolve YAML configurations files containing imports.
129
     *
130
     * Handles circular dependencies by ignoring them.
131
     *
132
     * @param string[] ...$filepaths
133
     *   A list of YML filepath to parse.
134
     *
135
     * @return string[]
136
     *   The list of all the YAML files to include.
137
     */
138 1
    public static function resolveImports(...$filepaths)
139
    {
140 1
        return (new YamlRecursivePathsFinder($filepaths))
141 1
            ->getAllPaths();
142
    }
143
}
144