ConfigLoader   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 213
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 87
dl 0
loc 213
rs 10
c 1
b 0
f 0
wmc 19

9 Methods

Rating   Name   Duplication   Size   Complexity  
A loadDefaultConfig() 0 29 2
A __construct() 0 8 1
A loadConfig() 0 45 5
A logConfigFilePathFound() 0 26 3
A configurePrefix() 0 10 2
A loadConfigWithoutConfigFile() 0 12 1
A canonicalizePath() 0 11 2
A configurePaths() 0 14 2
A loadConfiguration() 0 12 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the humbug/php-scoper package.
7
 *
8
 * Copyright (c) 2017 Théo FIDRY <[email protected]>,
9
 *                    Pádraic Brady <[email protected]>
10
 *
11
 * For the full copyright and license information, please view the LICENSE
12
 * file that was distributed with this source code.
13
 */
14
15
namespace Humbug\PhpScoper\Console;
16
17
use Fidry\Console\Command\CommandRegistry;
18
use Fidry\Console\Input\IO;
19
use Humbug\PhpScoper\Configuration\Configuration;
20
use Humbug\PhpScoper\Configuration\ConfigurationFactory;
21
use Symfony\Component\Console\Exception\RuntimeException;
22
use Symfony\Component\Console\Input\StringInput;
23
use Symfony\Component\Console\Output\OutputInterface;
24
use Symfony\Component\Filesystem\Filesystem;
25
use Symfony\Component\Filesystem\Path;
26
use function assert;
27
use function count;
28
use function file_exists;
29
use function Safe\sprintf;
30
use function trim;
31
use const DIRECTORY_SEPARATOR;
32
33
/**
34
 * @private
35
 */
36
final class ConfigLoader
37
{
38
    private CommandRegistry $commandRegistry;
39
    private Filesystem $fileSystem;
40
    private ConfigurationFactory $configFactory;
41
42
    public function __construct(
43
        CommandRegistry $commandRegistry,
44
        Filesystem $fileSystem,
45
        ConfigurationFactory $configFactory
46
    ) {
47
        $this->commandRegistry = $commandRegistry;
48
        $this->fileSystem = $fileSystem;
49
        $this->configFactory = $configFactory;
50
    }
51
52
    /**
53
     * @param non-empty-string|null  $configFilePath        Canonical absolute path
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string|null at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string|null.
Loading history...
54
     * @param non-empty-string       $defaultConfigFilePath
55
     * @param list<non-empty-string> $paths                 List of canonical absolute paths
56
     */
57
    public function loadConfig(
58
        IO $io,
59
        string $prefix,
60
        bool $noConfig,
61
        ?string $configFilePath,
62
        string $defaultConfigFilePath,
63
        bool $isInitCommandExecuted,
64
        array $paths,
65
        string $cwd
66
    ): Configuration {
67
        $prefix = trim($prefix);
68
        $defaultConfigFilePath = $this->canonicalizePath($defaultConfigFilePath, $cwd);
69
70
        if ($noConfig) {
71
            return $this->loadConfigWithoutConfigFile(
72
                $io,
73
                $prefix,
74
                $paths,
75
                $cwd,
76
            );
77
        }
78
79
        if (null === $configFilePath && !$isInitCommandExecuted) {
80
            $configFilePath = $this->loadDefaultConfig(
81
                $io,
82
                $defaultConfigFilePath,
83
            );
84
85
            if (null === $configFilePath) {
86
                return $this->loadConfig(
87
                    $io,
88
                    $prefix,
89
                    $noConfig,
90
                    $configFilePath,
91
                    $defaultConfigFilePath,
92
                    true,
93
                    $paths,
94
                    $cwd,
95
                );
96
            }
97
        }
98
99
        self::logConfigFilePathFound($io, $configFilePath);
100
101
        return $this->loadConfiguration($configFilePath, $prefix, $paths, $cwd);
102
    }
103
104
    /**
105
     * @param list<non-empty-string> $paths
0 ignored issues
show
Bug introduced by
The type Humbug\PhpScoper\Console\list was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
106
     */
107
    private function loadConfigWithoutConfigFile(
108
        IO $io,
109
        string $prefix,
110
        array $paths,
111
        string $cwd
112
    ): Configuration {
113
        $io->writeln(
114
            'Loading without configuration file.',
115
            OutputInterface::VERBOSITY_DEBUG,
116
        );
117
118
        return $this->loadConfiguration(null, $prefix, $paths, $cwd);
119
    }
120
121
    /**
122
     * @param non-empty-string $defaultConfigFilePath
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string.
Loading history...
123
     *
124
     * @return non-empty-string|null Config file path when found otherwise executes the init command
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string|null at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string|null.
Loading history...
125
     */
126
    private function loadDefaultConfig(IO $io, string $defaultConfigFilePath): ?string
127
    {
128
        $configFilePath = $defaultConfigFilePath;
129
130
        if (file_exists($configFilePath)) {
131
            return $configFilePath;
132
        }
133
134
        $initInput = new StringInput('');
135
        $initInput->setInteractive($io->isInteractive());
136
137
        $this->commandRegistry
138
            ->getCommand('init')
139
            ->execute(
140
                new IO(
141
                    $initInput,
142
                    $io->getOutput(),
143
                ),
144
            );
145
146
        $io->writeln(
147
            sprintf(
0 ignored issues
show
Deprecated Code introduced by
The function Safe\sprintf() has been deprecated: The Safe version of this function is no longer needed in PHP 8.0+ ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

147
            /** @scrutinizer ignore-deprecated */ sprintf(

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
148
                'Config file "<comment>%s</comment>" not found. Skipping.',
149
                $configFilePath,
150
            ),
151
            OutputInterface::VERBOSITY_DEBUG,
152
        );
153
154
        return null;
155
    }
156
157
    private static function logConfigFilePathFound(IO $io, ?string $configFilePath): void
158
    {
159
        if (null === $configFilePath) {
160
            $io->writeln(
161
                'Loading without configuration file.',
162
                OutputInterface::VERBOSITY_DEBUG,
163
            );
164
165
            return;
166
        }
167
168
        if (!file_exists($configFilePath)) {
169
            throw new RuntimeException(
170
                sprintf(
0 ignored issues
show
Deprecated Code introduced by
The function Safe\sprintf() has been deprecated: The Safe version of this function is no longer needed in PHP 8.0+ ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

170
                /** @scrutinizer ignore-deprecated */ sprintf(

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
171
                    'Could not find the configuration file "%s".',
172
                    $configFilePath,
173
                ),
174
            );
175
        }
176
177
        $io->writeln(
178
            sprintf(
0 ignored issues
show
Deprecated Code introduced by
The function Safe\sprintf() has been deprecated: The Safe version of this function is no longer needed in PHP 8.0+ ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

178
            /** @scrutinizer ignore-deprecated */ sprintf(

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
179
                'Using the configuration file "%s".',
180
                $configFilePath,
181
            ),
182
            OutputInterface::VERBOSITY_DEBUG,
183
        );
184
    }
185
186
    /**
187
     * @param non-empty-string|null  $configFilePath
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string|null at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string|null.
Loading history...
188
     * @param list<non-empty-string> $paths
189
     */
190
    private function loadConfiguration(
191
        ?string $configFilePath,
192
        string $prefix,
193
        array $paths,
194
        string $cwd
195
    ): Configuration {
196
        return $this->configurePaths(
197
            $this->configurePrefix(
198
                $this->configFactory->create($configFilePath, $paths),
199
                $prefix,
200
            ),
201
            $cwd,
202
        );
203
    }
204
205
    private function configurePrefix(Configuration $config, string $prefix): Configuration
206
    {
207
        if ('' !== $prefix) {
208
            return $this->configFactory->createWithPrefix(
209
                $config,
210
                $prefix,
211
            );
212
        }
213
214
        return $config;
215
    }
216
217
    private function configurePaths(
218
        Configuration $config,
219
        string $cwd
220
    ): Configuration {
221
        // Use the current working directory as the path if no file has been
222
        // found
223
        if (0 === count($config->getFilesWithContents())) {
224
            return $this->configFactory->createWithPaths(
225
                $config,
226
                [$cwd],
227
            );
228
        }
229
230
        return $config;
231
    }
232
233
    /**
234
     * @param non-empty-string $path
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string.
Loading history...
235
     *
236
     * @return non-empty-string Absolute canonical path
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string.
Loading history...
237
     */
238
    private function canonicalizePath(string $path, string $cwd): string
239
    {
240
        $canonicalPath = Path::canonicalize(
241
            $this->fileSystem->isAbsolutePath($path)
242
                ? $path
243
                : $cwd.DIRECTORY_SEPARATOR.$path,
244
        );
245
246
        assert('' !== $canonicalPath);
247
248
        return $canonicalPath;
249
    }
250
}
251