Passed
Push — master ( e0a8ac...1d0e74 )
by Théo
02:19
created

ConfigurationFactory   C

Complexity

Total Complexity 53

Size/Duplication

Total Lines 432
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 208
dl 0
loc 432
rs 6.96
c 0
b 0
f 0
wmc 53

16 Methods

Rating   Name   Duplication   Size   Complexity  
B loadConfigFile() 0 45 8
A validateConfigKey() 0 10 2
A validateConfigKeys() 0 5 1
A retrieveFilesWithContents() 0 31 5
A createWithPaths() 0 23 1
A retrieveAllInternalSymbols() 0 6 1
A __construct() 0 6 1
B retrieveFilesFromPaths() 0 37 6
A create() 0 38 3
A retrievePatchers() 0 31 5
A retrieveFinders() 0 33 5
A createWithPrefix() 0 14 1
A retrievePrefix() 0 9 2
A retrieveInternalSymbols() 0 32 5
A generateRandomPrefix() 0 3 1
A retrieveWhitelistedFiles() 0 35 6

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