Total Complexity | 53 |
Total Lines | 431 |
Duplicated Lines | 0 % |
Changes | 3 | ||
Bugs | 0 | Features | 0 |
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 |
||
57 | final class ConfigurationFactory |
||
58 | { |
||
59 | private Filesystem $fileSystem; |
||
60 | private ConfigurationWhitelistFactory $configurationWhitelistFactory; |
||
61 | |||
62 | public function __construct( |
||
63 | Filesystem $fileSystem, |
||
64 | ConfigurationWhitelistFactory $configurationWhitelistFactory |
||
65 | ) { |
||
66 | $this->fileSystem = $fileSystem; |
||
67 | $this->configurationWhitelistFactory = $configurationWhitelistFactory; |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * @param string|null $path Absolute path to the configuration file. |
||
72 | * @param string[] $paths List of paths to append besides the one configured |
||
73 | */ |
||
74 | public function create(?string $path = null, array $paths = []): Configuration |
||
75 | { |
||
76 | if (null === $path) { |
||
77 | $config = []; |
||
78 | } else { |
||
79 | $config = $this->loadConfigFile($path); |
||
80 | } |
||
81 | |||
82 | self::validateConfigKeys($config); |
||
83 | |||
84 | $prefix = self::retrievePrefix($config); |
||
85 | |||
86 | $whitelistedFiles = null === $path |
||
87 | ? [] |
||
88 | : $this->retrieveWhitelistedFiles( |
||
89 | dirname($path), |
||
90 | $config, |
||
91 | ); |
||
92 | |||
93 | $patchers = self::retrievePatchers($config); |
||
94 | |||
95 | array_unshift($patchers, new SymfonyPatcher()); |
||
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 | $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->getPatchers(), |
||
136 | $config->getWhitelist(), |
||
137 | $config->getInternalClasses(), |
||
138 | $config->getInternalFunctions(), |
||
139 | $config->getInternalConstants(), |
||
140 | ); |
||
141 | } |
||
142 | |||
143 | public function createWithPrefix(Configuration $config, string $prefix): Configuration |
||
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 callable[] |
||
242 | */ |
||
243 | private static function retrievePatchers(array $config): array |
||
274 | } |
||
275 | |||
276 | /** |
||
277 | * @return string[] Absolute paths |
||
278 | */ |
||
279 | private function retrieveWhitelistedFiles(string $dirPath, array $config): array |
||
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 |
||
434 | } |
||
435 | |||
436 | /** |
||
437 | * @return array{string[], string[], string[]} |
||
|
|||
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 |
||
483 | } |
||
484 | |||
485 | private static function generateRandomPrefix(): string |
||
486 | { |
||
487 | return '_PhpScoper'.bin2hex(random_bytes(6)); |
||
488 | } |
||
489 | } |
||
490 |