| 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 |
||
| 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 |
||
| 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 |
||
| 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> |
||
|
|
|||
| 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 |
||
| 433 | } |
||
| 434 | |||
| 435 | /** |
||
| 436 | * @return array{string[], string[], string[]} |
||
| 437 | */ |
||
| 438 | private static function retrieveAllInternalSymbols(array $config): array |
||
| 444 | ]; |
||
| 445 | } |
||
| 446 | |||
| 447 | /** |
||
| 448 | * @return string[] |
||
| 449 | */ |
||
| 450 | private static function retrieveInternalSymbols(array $config, string $key): array |
||
| 451 | { |
||
| 482 | } |
||
| 483 | |||
| 484 | private static function generateRandomPrefix(): string |
||
| 487 | } |
||
| 488 | } |
||
| 489 |