Passed
Pull Request — master (#564)
by Théo
02:41
created

ConfigurationFactory::retrieveInternalSymbols()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 32
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 18
nc 5
nop 2
dl 0
loc 32
rs 9.3554
c 0
b 0
f 0
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\PatcherChain;
19
use Humbug\PhpScoper\Patcher\SymfonyPatcher;
20
use InvalidArgumentException;
21
use RuntimeException;
22
use SplFileInfo;
23
use Symfony\Component\Filesystem\Filesystem;
24
use Symfony\Component\Finder\Finder;
25
use function array_filter;
26
use function array_key_exists;
27
use function array_keys;
28
use function array_map;
29
use function array_merge;
30
use function array_unique;
31
use function array_unshift;
32
use function array_values;
33
use function bin2hex;
34
use function dirname;
35
use function file_exists;
36
use function gettype;
37
use function Humbug\PhpScoper\chain;
38
use function in_array;
39
use function is_array;
40
use function is_callable;
41
use function is_dir;
42
use function is_file;
43
use function is_link;
44
use function is_readable;
45
use function is_string;
46
use function random_bytes;
47
use function readlink as native_readlink;
48
use function realpath;
49
use function Safe\file_get_contents;
50
use function Safe\sprintf;
51
use function trim;
52
use const DIRECTORY_SEPARATOR;
53
use const SORT_STRING;
54
55
final class ConfigurationFactory
56
{
57
    private Filesystem $fileSystem;
58
    private ConfigurationWhitelistFactory $configurationWhitelistFactory;
59
60
    public function __construct(
61
        Filesystem $fileSystem,
62
        ConfigurationWhitelistFactory $configurationWhitelistFactory
63
    ) {
64
        $this->fileSystem = $fileSystem;
65
        $this->configurationWhitelistFactory = $configurationWhitelistFactory;
66
    }
67
68
    /**
69
     * @param string|null $path  Absolute path to the configuration file.
70
     * @param string[]    $paths List of paths to append besides the one configured
71
     */
72
    public function create(?string $path = null, array $paths = []): Configuration
73
    {
74
        if (null === $path) {
75
            $config = [];
76
        } else {
77
            $config = $this->loadConfigFile($path);
78
        }
79
80
        self::validateConfigKeys($config);
81
82
        $prefix = self::retrievePrefix($config);
83
84
        $whitelistedFiles = null === $path
85
            ? []
86
            : $this->retrieveWhitelistedFiles(
87
                dirname($path),
88
                $config,
89
            );
90
91
        $patchers = self::retrievePatchers($config);
92
93
        array_unshift($patchers, new SymfonyPatcher());
94
        array_unshift($patchers, new ComposerPatcher());
95
96
        $whitelist = $this->configurationWhitelistFactory->createWhitelist($config);
97
98
        $finders = self::retrieveFinders($config);
99
        $filesFromPaths = self::retrieveFilesFromPaths($paths);
100
        $filesWithContents = self::retrieveFilesWithContents(chain($filesFromPaths, ...$finders));
101
102
        return new Configuration(
103
            $path,
104
            $prefix,
105
            $filesWithContents,
106
            self::retrieveFilesWithContents($whitelistedFiles),
107
            new PatcherChain($patchers),
108
            $whitelist,
109
            ...self::retrieveAllInternalSymbols($config),
110
        );
111
    }
112
113
    /**
114
     * @param string[] $paths
115
     */
116
    public function createWithPaths(Configuration $config, array $paths): Configuration
117
    {
118
        $filesWithContents = self::retrieveFilesWithContents(
119
            chain(
120
                self::retrieveFilesFromPaths(
121
                    array_unique($paths),
122
                ),
123
            ),
124
        );
125
126
        return new Configuration(
127
            $config->getPath(),
128
            $config->getPrefix(),
129
            array_merge(
130
                $config->getFilesWithContents(),
131
                $filesWithContents,
132
            ),
133
            $config->getWhitelistedFilesWithContents(),
134
            $config->getPatcher(),
135
            $config->getWhitelist(),
136
            $config->getInternalClasses(),
137
            $config->getInternalFunctions(),
138
            $config->getInternalConstants(),
139
        );
140
    }
141
142
    public function createWithPrefix(Configuration $config, string $prefix): Configuration
143
    {
144
        $prefix = self::retrievePrefix([ConfigurationKeys::PREFIX_KEYWORD => $prefix]);
145
146
        return new Configuration(
147
            $config->getPath(),
148
            $prefix,
149
            $config->getFilesWithContents(),
150
            $config->getWhitelistedFilesWithContents(),
151
            $config->getPatcher(),
152
            $config->getWhitelist(),
153
            $config->getInternalClasses(),
154
            $config->getInternalFunctions(),
155
            $config->getInternalConstants(),
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
    private static function retrievePrefix(array $config): string
229
    {
230
        $prefix = trim((string) ($config[ConfigurationKeys::PREFIX_KEYWORD] ?? ''));
231
232
        if ('' === $prefix) {
233
            return self::generateRandomPrefix();
234
        }
235
236
        return $prefix;
237
    }
238
239
    /**
240
     * @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...
241
     */
242
    private static function retrievePatchers(array $config): array
243
    {
244
        if (!array_key_exists(ConfigurationKeys::PATCHERS_KEYWORD, $config)) {
245
            return [];
246
        }
247
248
        $patchers = $config[ConfigurationKeys::PATCHERS_KEYWORD];
249
250
        if (!is_array($patchers)) {
251
            throw new InvalidArgumentException(
252
                sprintf(
253
                    'Expected patchers to be an array of callables, found "%s" instead.',
254
                    gettype($patchers),
255
                ),
256
            );
257
        }
258
259
        foreach ($patchers as $index => $patcher) {
260
            if (is_callable($patcher)) {
261
                continue;
262
            }
263
264
            throw new InvalidArgumentException(
265
                sprintf(
266
                    'Expected patchers to be an array of callables, the "%d" element is not.',
267
                    $index,
268
                ),
269
            );
270
        }
271
272
        return $patchers;
273
    }
274
275
    /**
276
     * @return string[] Absolute paths
277
     */
278
    private function retrieveWhitelistedFiles(string $dirPath, array $config): array
279
    {
280
        if (!array_key_exists(ConfigurationKeys::WHITELISTED_FILES_KEYWORD, $config)) {
281
            return [];
282
        }
283
284
        $whitelistedFiles = $config[ConfigurationKeys::WHITELISTED_FILES_KEYWORD];
285
286
        if (!is_array($whitelistedFiles)) {
287
            throw new InvalidArgumentException(
288
                sprintf(
289
                    'Expected whitelisted files to be an array of strings, found "%s" instead.',
290
                    gettype($whitelistedFiles),
291
                ),
292
            );
293
        }
294
295
        foreach ($whitelistedFiles as $index => $file) {
296
            if (!is_string($file)) {
297
                throw new InvalidArgumentException(
298
                    sprintf(
299
                        'Expected whitelisted files to be an array of string, the "%d" element is not.',
300
                        $index,
301
                    ),
302
                );
303
            }
304
305
            if (!$this->fileSystem->isAbsolutePath($file)) {
306
                $file = $dirPath.DIRECTORY_SEPARATOR.$file;
307
            }
308
309
            $whitelistedFiles[$index] = realpath($file);
310
        }
311
312
        return array_filter($whitelistedFiles);
313
    }
314
315
    /**
316
     * @return Finder[]
317
     */
318
    private static function retrieveFinders(array $config): array
319
    {
320
        if (!array_key_exists(ConfigurationKeys::FINDER_KEYWORD, $config)) {
321
            return [];
322
        }
323
324
        $finders = $config[ConfigurationKeys::FINDER_KEYWORD];
325
326
        if (!is_array($finders)) {
327
            throw new InvalidArgumentException(
328
                sprintf(
329
                    'Expected finders to be an array of "%s", found "%s" instead.',
330
                    Finder::class,
331
                    gettype($finders),
332
                ),
333
            );
334
        }
335
336
        foreach ($finders as $index => $finder) {
337
            if ($finder instanceof Finder) {
338
                continue;
339
            }
340
341
            throw new InvalidArgumentException(
342
                sprintf(
343
                    'Expected finders to be an array of "%s", the "%d" element is not.',
344
                    Finder::class,
345
                    $index,
346
                ),
347
            );
348
        }
349
350
        return $finders;
351
    }
352
353
    /**
354
     * @param string[] $paths
355
     *
356
     * @return iterable<SplFileInfo>
357
     */
358
    private static function retrieveFilesFromPaths(array $paths): iterable
359
    {
360
        if ([] === $paths) {
361
            return [];
362
        }
363
364
        $pathsToSearch = [];
365
        $filesToAppend = [];
366
367
        foreach ($paths as $path) {
368
            if (!file_exists($path)) {
369
                throw new RuntimeException(
370
                    sprintf(
371
                        'Could not find the file "%s".',
372
                        $path,
373
                    ),
374
                );
375
            }
376
377
            if (is_dir($path)) {
378
                $pathsToSearch[] = $path;
379
            } else {
380
                $filesToAppend[] = $path;
381
            }
382
        }
383
384
        $finder = new Finder();
385
386
        $finder->files()
387
            ->in($pathsToSearch)
388
            ->append($filesToAppend)
389
            ->filter(
390
                static fn (SplFileInfo $fileInfo) => $fileInfo->isLink() ? false : null,
391
            )
392
            ->sortByName();
393
394
        return $finder;
395
    }
396
397
    /**
398
     * @param iterable<SplFileInfo|string> $files
399
     *
400
     * @return array<string, array{string, string}> Array of tuple with the first argument being the file path and the second its contents
401
     */
402
    private static function retrieveFilesWithContents(iterable $files): array
403
    {
404
        $filesWithContents = [];
405
406
        foreach ($files as $filePathOrFileInfo) {
407
            $filePath = $filePathOrFileInfo instanceof SplFileInfo
408
                ? $filePathOrFileInfo->getRealPath()
409
                : realpath($filePathOrFileInfo);
410
411
            if (!$filePath) {
412
                throw new RuntimeException(
413
                    sprintf(
414
                        'Could not find the file "%s".',
415
                        (string) $filePathOrFileInfo,
416
                    ),
417
                );
418
            }
419
420
            if (!is_readable($filePath)) {
421
                throw new RuntimeException(
422
                    sprintf(
423
                        'Could not read the file "%s".',
424
                        $filePath,
425
                    ),
426
                );
427
            }
428
429
            $filesWithContents[$filePath] = [$filePath, file_get_contents($filePath)];
430
        }
431
432
        return $filesWithContents;
433
    }
434
435
    /**
436
     * @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...
437
     */
438
    private static function retrieveAllInternalSymbols(array $config): array
439
    {
440
        return [
441
            self::retrieveInternalSymbols($config, ConfigurationKeys::CLASSES_INTERNAL_SYMBOLS_KEYWORD),
442
            self::retrieveInternalSymbols($config, ConfigurationKeys::FUNCTIONS_INTERNAL_SYMBOLS_KEYWORD),
443
            self::retrieveInternalSymbols($config, ConfigurationKeys::CONSTANTS_INTERNAL_SYMBOLS_KEYWORD),
444
        ];
445
    }
446
447
    /**
448
     * @return string[]
449
     */
450
    private static function retrieveInternalSymbols(array $config, string $key): array
451
    {
452
        if (!array_key_exists($key, $config)) {
453
            return [];
454
        }
455
456
        $symbols = $config[$key];
457
458
        if (!is_array($symbols)) {
459
            throw new InvalidArgumentException(
460
                sprintf(
461
                    'Expected "%s" to be an array of strings, got "%s" instead.',
462
                    $key,
463
                    gettype($symbols),
464
                ),
465
            );
466
        }
467
468
        foreach ($symbols as $index => $symbol) {
469
            if (!is_string($symbol)) {
470
                throw new InvalidArgumentException(
471
                    sprintf(
472
                        'Expected "%s" to be an array of strings, got "%s" for the element with the index "%s".',
473
                        $key,
474
                        gettype($symbol),
475
                        $index,
476
                    ),
477
                );
478
            }
479
        }
480
481
        return array_values(array_unique($symbols, SORT_STRING));
482
    }
483
484
    private static function generateRandomPrefix(): string
485
    {
486
        return '_PhpScoper'.bin2hex(random_bytes(6));
487
    }
488
}
489