Passed
Pull Request — master (#632)
by Théo
02:05
created

ConfigurationFactory   B

Complexity

Total Complexity 49

Size/Duplication

Total Lines 396
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 184
dl 0
loc 396
rs 8.48
c 0
b 0
f 0
wmc 49

15 Methods

Rating   Name   Duplication   Size   Complexity  
B loadConfigFile() 0 45 8
A validateConfigKey() 0 10 2
A validateConfigKeys() 0 5 1
A __construct() 0 6 1
A retrieveOutputDir() 0 5 2
A retrieveFilesWithContents() 0 31 5
A createWithPaths() 0 21 1
A retrieveExcludedFiles() 0 35 6
B retrieveFilesFromPaths() 0 37 6
A create() 0 39 3
A retrievePatchers() 0 31 5
A retrieveFinders() 0 33 5
A createWithPrefix() 0 12 1
A retrievePrefix() 0 5 2
A generateRandomPrefix() 0 3 1

How to fix   Complexity   

Complex Class

Complex classes like ConfigurationFactory often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use ConfigurationFactory, and based on these observations, apply Extract Interface, too.

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\Configuration;
16
17
use Humbug\PhpScoper\Patcher\ComposerPatcher;
18
use Humbug\PhpScoper\Patcher\Patcher;
19
use Humbug\PhpScoper\Patcher\PatcherChain;
20
use Humbug\PhpScoper\Patcher\SymfonyPatcher;
21
use InvalidArgumentException;
22
use RuntimeException;
23
use SplFileInfo;
24
use Symfony\Component\Filesystem\Filesystem;
25
use Symfony\Component\Finder\Finder;
26
use function array_filter;
27
use function array_key_exists;
28
use function array_keys;
29
use function array_map;
30
use function array_merge;
31
use function array_unique;
32
use function array_unshift;
33
use function array_values;
34
use function bin2hex;
35
use function dirname;
36
use function file_exists;
37
use function gettype;
38
use function Humbug\PhpScoper\chain;
39
use function in_array;
40
use function is_array;
41
use function is_callable;
42
use function is_dir;
43
use function is_file;
44
use function is_link;
45
use function is_readable;
46
use function is_string;
47
use function random_bytes;
48
use function readlink as native_readlink;
49
use function realpath;
50
use function Safe\file_get_contents;
51
use function Safe\sprintf;
52
use function trim;
53
use const DIRECTORY_SEPARATOR;
54
use const SORT_STRING;
55
56
final class ConfigurationFactory
57
{
58
    public const DEFAULT_FILE_NAME = 'scoper.inc.php';
59
60
    private Filesystem $fileSystem;
61
    private SymbolsConfigurationFactory $configurationWhitelistFactory;
62
63
    public function __construct(
64
        Filesystem $fileSystem,
65
        SymbolsConfigurationFactory $configurationWhitelistFactory
66
    ) {
67
        $this->fileSystem = $fileSystem;
68
        $this->configurationWhitelistFactory = $configurationWhitelistFactory;
69
    }
70
71
    /**
72
     * @param non-empty-string|null  $path  Absolute canonical path to the configuration file.
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...
73
     * @param list<non-empty-string> $paths List of absolute canonical paths to append besides the one configured
74
     */
75
    public function create(?string $path = null, array $paths = []): Configuration
76
    {
77
        if (null === $path) {
78
            $config = [];
79
        } else {
80
            $config = $this->loadConfigFile($path);
81
        }
82
83
        self::validateConfigKeys($config);
84
85
        $prefix = self::retrievePrefix($config);
86
        $outputDir = self::retrieveOutputDir($config);
87
88
        $excludedFiles = null === $path
89
            ? []
90
            : $this->retrieveExcludedFiles(
91
                dirname($path),
92
                $config,
93
            );
94
95
        $patchers = self::retrievePatchers($config);
96
97
        array_unshift($patchers, new SymfonyPatcher());
98
        array_unshift($patchers, new ComposerPatcher());
99
100
        $symbolsConfiguration = $this->configurationWhitelistFactory->createSymbolsConfiguration($config);
101
102
        $finders = self::retrieveFinders($config);
103
        $filesFromPaths = self::retrieveFilesFromPaths($paths);
104
        $filesWithContents = self::retrieveFilesWithContents(chain($filesFromPaths, ...$finders));
105
106
        return new Configuration(
107
            $path,
108
            $outputDir,
109
            $prefix,
110
            $filesWithContents,
111
            self::retrieveFilesWithContents($excludedFiles),
112
            new PatcherChain($patchers),
113
            $symbolsConfiguration,
114
        );
115
    }
116
117
    /**
118
     * @param string[] $paths
119
     */
120
    public function createWithPaths(Configuration $config, array $paths): Configuration
121
    {
122
        $filesWithContents = self::retrieveFilesWithContents(
123
            chain(
124
                self::retrieveFilesFromPaths(
125
                    array_unique($paths, SORT_STRING),
126
                ),
127
            ),
128
        );
129
130
        return new Configuration(
131
            $config->getPath(),
132
            $config->getOutputDir(),
133
            $config->getPrefix(),
134
            array_merge(
135
                $config->getFilesWithContents(),
136
                $filesWithContents,
137
            ),
138
            $config->getExcludedFilesWithContents(),
139
            $config->getPatcher(),
140
            $config->getSymbolsConfiguration(),
141
        );
142
    }
143
144
    public function createWithPrefix(Configuration $config, string $prefix): Configuration
145
    {
146
        $prefix = self::retrievePrefix([ConfigurationKeys::PREFIX_KEYWORD => $prefix]);
147
148
        return new Configuration(
149
            $config->getPath(),
150
            $config->getOutputDir(),
151
            $prefix,
152
            $config->getFilesWithContents(),
153
            $config->getExcludedFilesWithContents(),
154
            $config->getPatcher(),
155
            $config->getSymbolsConfiguration(),
156
        );
157
    }
158
159
    private function loadConfigFile(string $path): array
160
    {
161
        if (!$this->fileSystem->isAbsolutePath($path)) {
162
            throw new InvalidArgumentException(
163
                sprintf(
164
                    'Expected the path of the configuration file to load to be an absolute path, got "%s" instead',
165
                    $path,
166
                ),
167
            );
168
        }
169
170
        if (!file_exists($path)) {
171
            throw new InvalidArgumentException(
172
                sprintf(
173
                    'Expected the path of the configuration file to exists but the file "%s" could not be found',
174
                    $path,
175
                ),
176
            );
177
        }
178
179
        $isADirectoryLink = is_link($path)
180
            && false !== native_readlink($path)
181
            && is_file(native_readlink($path));
182
183
        if (!$isADirectoryLink && !is_file($path)) {
184
            throw new InvalidArgumentException(
185
                sprintf(
186
                    'Expected the path of the configuration file to be a file but "%s" appears to be a directory.',
187
                    $path,
188
                ),
189
            );
190
        }
191
192
        $config = include $path;
193
194
        if (!is_array($config)) {
195
            throw new InvalidArgumentException(
196
                sprintf(
197
                    'Expected configuration to be an array, found "%s" instead.',
198
                    gettype($config),
199
                ),
200
            );
201
        }
202
203
        return $config;
204
    }
205
206
    private static function validateConfigKeys(array $config): void
207
    {
208
        array_map(
209
            static fn (string $key) => self::validateConfigKey($key),
210
            array_keys($config),
211
        );
212
    }
213
214
    private static function validateConfigKey(string $key): void
215
    {
216
        if (in_array($key, ConfigurationKeys::KEYWORDS, true)) {
217
            return;
218
        }
219
220
        throw new InvalidArgumentException(
221
            sprintf(
222
                'Invalid configuration key value "%s" found.',
223
                $key,
224
            ),
225
        );
226
    }
227
228
    /**
229
     * @return non-empty-string
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...
230
     */
231
    private static function retrievePrefix(array $config): string
232
    {
233
        $prefix = trim((string) ($config[ConfigurationKeys::PREFIX_KEYWORD] ?? ''));
234
235
        return '' === $prefix ? self::generateRandomPrefix() : $prefix;
236
237
    }
238
239
    /**
240
     * @return non-empty-string|null
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...
241
     */
242
    private static function retrieveOutputDir(array $config): ?string
243
    {
244
        $outputDir = trim((string) ($config[ConfigurationKeys::OUTPUT_DIR_KEYWORD] ?? ''));
245
246
        return '' === $outputDir ? null : $outputDir;
247
248
    }
249
250
    /**
251
     * @return array<(callable(string,string,string): string)|Patcher>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<(callable(string,s...ring): string)|Patcher> at position 3 could not be parsed: Expected ')' at position 3, but found 'callable'.
Loading history...
252
     */
253
    private static function retrievePatchers(array $config): array
254
    {
255
        if (!array_key_exists(ConfigurationKeys::PATCHERS_KEYWORD, $config)) {
256
            return [];
257
        }
258
259
        $patchers = $config[ConfigurationKeys::PATCHERS_KEYWORD];
260
261
        if (!is_array($patchers)) {
262
            throw new InvalidArgumentException(
263
                sprintf(
264
                    'Expected patchers to be an array of callables, found "%s" instead.',
265
                    gettype($patchers),
266
                ),
267
            );
268
        }
269
270
        foreach ($patchers as $index => $patcher) {
271
            if (is_callable($patcher)) {
272
                continue;
273
            }
274
275
            throw new InvalidArgumentException(
276
                sprintf(
277
                    'Expected patchers to be an array of callables, the "%d" element is not.',
278
                    $index,
279
                ),
280
            );
281
        }
282
283
        return $patchers;
284
    }
285
286
    /**
287
     * @return string[] Absolute paths
288
     */
289
    private function retrieveExcludedFiles(string $dirPath, array $config): array
290
    {
291
        if (!array_key_exists(ConfigurationKeys::EXCLUDED_FILES_KEYWORD, $config)) {
292
            return [];
293
        }
294
295
        $excludedFiles = $config[ConfigurationKeys::EXCLUDED_FILES_KEYWORD];
296
297
        if (!is_array($excludedFiles)) {
298
            throw new InvalidArgumentException(
299
                sprintf(
300
                    'Expected excluded files to be an array of strings, found "%s" instead.',
301
                    gettype($excludedFiles),
302
                ),
303
            );
304
        }
305
306
        foreach ($excludedFiles as $index => $file) {
307
            if (!is_string($file)) {
308
                throw new InvalidArgumentException(
309
                    sprintf(
310
                        'Expected excluded files to be an array of string, the "%d" element is not.',
311
                        $index,
312
                    ),
313
                );
314
            }
315
316
            if (!$this->fileSystem->isAbsolutePath($file)) {
317
                $file = $dirPath.DIRECTORY_SEPARATOR.$file;
318
            }
319
320
            $excludedFiles[$index] = realpath($file);
321
        }
322
323
        return array_filter($excludedFiles);
324
    }
325
326
    /**
327
     * @return Finder[]
328
     */
329
    private static function retrieveFinders(array $config): array
330
    {
331
        if (!array_key_exists(ConfigurationKeys::FINDER_KEYWORD, $config)) {
332
            return [];
333
        }
334
335
        $finders = $config[ConfigurationKeys::FINDER_KEYWORD];
336
337
        if (!is_array($finders)) {
338
            throw new InvalidArgumentException(
339
                sprintf(
340
                    'Expected finders to be an array of "%s", found "%s" instead.',
341
                    Finder::class,
342
                    gettype($finders),
343
                ),
344
            );
345
        }
346
347
        foreach ($finders as $index => $finder) {
348
            if ($finder instanceof Finder) {
349
                continue;
350
            }
351
352
            throw new InvalidArgumentException(
353
                sprintf(
354
                    'Expected finders to be an array of "%s", the "%d" element is not.',
355
                    Finder::class,
356
                    $index,
357
                ),
358
            );
359
        }
360
361
        return $finders;
362
    }
363
364
    /**
365
     * @param string[] $paths
366
     *
367
     * @return iterable<SplFileInfo>
368
     */
369
    private static function retrieveFilesFromPaths(array $paths): iterable
370
    {
371
        if ([] === $paths) {
372
            return [];
373
        }
374
375
        $pathsToSearch = [];
376
        $filesToAppend = [];
377
378
        foreach ($paths as $path) {
379
            if (!file_exists($path)) {
380
                throw new RuntimeException(
381
                    sprintf(
382
                        'Could not find the file "%s".',
383
                        $path,
384
                    ),
385
                );
386
            }
387
388
            if (is_dir($path)) {
389
                $pathsToSearch[] = $path;
390
            } else {
391
                $filesToAppend[] = $path;
392
            }
393
        }
394
395
        $finder = new Finder();
396
397
        $finder->files()
398
            ->in($pathsToSearch)
399
            ->append($filesToAppend)
400
            ->filter(
401
                static fn (SplFileInfo $fileInfo) => $fileInfo->isLink() ? false : null,
402
            )
403
            ->sortByName();
404
405
        return $finder;
406
    }
407
408
    /**
409
     * @param iterable<SplFileInfo|string> $files
410
     *
411
     * @return array<string, array{string, string}> Array of tuple with the first argument being the file path and the second its contents
412
     */
413
    private static function retrieveFilesWithContents(iterable $files): array
414
    {
415
        $filesWithContents = [];
416
417
        foreach ($files as $filePathOrFileInfo) {
418
            $filePath = $filePathOrFileInfo instanceof SplFileInfo
419
                ? $filePathOrFileInfo->getRealPath()
420
                : realpath($filePathOrFileInfo);
421
422
            if (!$filePath) {
423
                throw new RuntimeException(
424
                    sprintf(
425
                        'Could not find the file "%s".',
426
                        (string) $filePathOrFileInfo,
427
                    ),
428
                );
429
            }
430
431
            if (!is_readable($filePath)) {
432
                throw new RuntimeException(
433
                    sprintf(
434
                        'Could not read the file "%s".',
435
                        $filePath,
436
                    ),
437
                );
438
            }
439
440
            $filesWithContents[$filePath] = [$filePath, file_get_contents($filePath)];
441
        }
442
443
        return $filesWithContents;
444
    }
445
446
    /**
447
     * @return non-empty-string
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...
448
     */
449
    private static function generateRandomPrefix(): string
450
    {
451
        return '_PhpScoper'.bin2hex(random_bytes(6));
452
    }
453
}
454