Total Complexity | 53 |
Total Lines | 432 |
Duplicated Lines | 0 % |
Changes | 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 |
||
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 |
||
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 |
||
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> |
||
|
|||
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 |
||
434 | } |
||
435 | |||
436 | /** |
||
437 | * @return array{string[], string[], string[]} |
||
438 | */ |
||
439 | private static function retrieveAllInternalSymbols(array $config): array |
||
445 | ]; |
||
446 | } |
||
447 | |||
448 | /** |
||
449 | * @return string[] |
||
450 | */ |
||
451 | private static function retrieveInternalSymbols(array $config, string $key): array |
||
452 | { |
||
483 | } |
||
484 | |||
485 | private static function generateRandomPrefix(): string |
||
488 | } |
||
489 | } |
||
490 |