| Total Complexity | 47 |
| Total Lines | 376 |
| 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 SymbolsConfigurationFactory $configurationWhitelistFactory; |
||
| 60 | |||
| 61 | public function __construct( |
||
| 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 | $excludedFiles = null === $path |
||
| 86 | ? [] |
||
| 87 | : $this->retrieveExcludedFiles( |
||
| 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 | $symbolsConfiguration = $this->configurationWhitelistFactory->createSymbolsConfiguration($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($excludedFiles), |
||
| 108 | new PatcherChain($patchers), |
||
| 109 | $symbolsConfiguration, |
||
| 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, SORT_STRING), |
||
| 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->getExcludedFilesWithContents(), |
||
| 134 | $config->getPatcher(), |
||
| 135 | $config->getSymbolsConfiguration(), |
||
| 136 | ); |
||
| 137 | } |
||
| 138 | |||
| 139 | public function createWithPrefix(Configuration $config, string $prefix): Configuration |
||
| 140 | { |
||
| 141 | $prefix = self::retrievePrefix([ConfigurationKeys::PREFIX_KEYWORD => $prefix]); |
||
| 142 | |||
| 143 | return new Configuration( |
||
| 144 | $config->getPath(), |
||
| 145 | $prefix, |
||
| 146 | $config->getFilesWithContents(), |
||
| 147 | $config->getExcludedFilesWithContents(), |
||
| 148 | $config->getPatcher(), |
||
| 149 | $config->getSymbolsConfiguration(), |
||
| 150 | ); |
||
| 151 | } |
||
| 152 | |||
| 153 | private function loadConfigFile(string $path): array |
||
| 198 | } |
||
| 199 | |||
| 200 | private static function validateConfigKeys(array $config): void |
||
| 201 | { |
||
| 202 | array_map( |
||
| 203 | static fn (string $key) => self::validateConfigKey($key), |
||
| 204 | array_keys($config), |
||
| 205 | ); |
||
| 206 | } |
||
| 207 | |||
| 208 | private static function validateConfigKey(string $key): void |
||
| 218 | ), |
||
| 219 | ); |
||
| 220 | } |
||
| 221 | |||
| 222 | private static function retrievePrefix(array $config): string |
||
| 223 | { |
||
| 224 | $prefix = trim((string) ($config[ConfigurationKeys::PREFIX_KEYWORD] ?? '')); |
||
| 225 | |||
| 226 | if ('' === $prefix) { |
||
| 227 | return self::generateRandomPrefix(); |
||
| 228 | } |
||
| 229 | |||
| 230 | return $prefix; |
||
| 231 | } |
||
| 232 | |||
| 233 | /** |
||
| 234 | * @return array<(callable(string,string,string): string)|Patcher> |
||
|
|
|||
| 235 | */ |
||
| 236 | private static function retrievePatchers(array $config): array |
||
| 237 | { |
||
| 238 | if (!array_key_exists(ConfigurationKeys::PATCHERS_KEYWORD, $config)) { |
||
| 239 | return []; |
||
| 240 | } |
||
| 241 | |||
| 242 | $patchers = $config[ConfigurationKeys::PATCHERS_KEYWORD]; |
||
| 243 | |||
| 244 | if (!is_array($patchers)) { |
||
| 245 | throw new InvalidArgumentException( |
||
| 246 | sprintf( |
||
| 247 | 'Expected patchers to be an array of callables, found "%s" instead.', |
||
| 248 | gettype($patchers), |
||
| 249 | ), |
||
| 250 | ); |
||
| 251 | } |
||
| 252 | |||
| 253 | foreach ($patchers as $index => $patcher) { |
||
| 254 | if (is_callable($patcher)) { |
||
| 255 | continue; |
||
| 256 | } |
||
| 257 | |||
| 258 | throw new InvalidArgumentException( |
||
| 259 | sprintf( |
||
| 260 | 'Expected patchers to be an array of callables, the "%d" element is not.', |
||
| 261 | $index, |
||
| 262 | ), |
||
| 263 | ); |
||
| 264 | } |
||
| 265 | |||
| 266 | return $patchers; |
||
| 267 | } |
||
| 268 | |||
| 269 | /** |
||
| 270 | * @return string[] Absolute paths |
||
| 271 | */ |
||
| 272 | private function retrieveExcludedFiles(string $dirPath, array $config): array |
||
| 273 | { |
||
| 274 | if (!array_key_exists(ConfigurationKeys::EXCLUDED_FILES_KEYWORD, $config)) { |
||
| 275 | return []; |
||
| 276 | } |
||
| 277 | |||
| 278 | $excludedFiles = $config[ConfigurationKeys::EXCLUDED_FILES_KEYWORD]; |
||
| 279 | |||
| 280 | if (!is_array($excludedFiles)) { |
||
| 281 | throw new InvalidArgumentException( |
||
| 282 | sprintf( |
||
| 283 | 'Expected excluded files to be an array of strings, found "%s" instead.', |
||
| 284 | gettype($excludedFiles), |
||
| 285 | ), |
||
| 286 | ); |
||
| 287 | } |
||
| 288 | |||
| 289 | foreach ($excludedFiles as $index => $file) { |
||
| 290 | if (!is_string($file)) { |
||
| 291 | throw new InvalidArgumentException( |
||
| 292 | sprintf( |
||
| 293 | 'Expected excluded files to be an array of string, the "%d" element is not.', |
||
| 294 | $index, |
||
| 295 | ), |
||
| 296 | ); |
||
| 297 | } |
||
| 298 | |||
| 299 | if (!$this->fileSystem->isAbsolutePath($file)) { |
||
| 300 | $file = $dirPath.DIRECTORY_SEPARATOR.$file; |
||
| 301 | } |
||
| 302 | |||
| 303 | $excludedFiles[$index] = realpath($file); |
||
| 304 | } |
||
| 305 | |||
| 306 | return array_filter($excludedFiles); |
||
| 307 | } |
||
| 308 | |||
| 309 | /** |
||
| 310 | * @return Finder[] |
||
| 311 | */ |
||
| 312 | private static function retrieveFinders(array $config): array |
||
| 313 | { |
||
| 314 | if (!array_key_exists(ConfigurationKeys::FINDER_KEYWORD, $config)) { |
||
| 315 | return []; |
||
| 316 | } |
||
| 317 | |||
| 318 | $finders = $config[ConfigurationKeys::FINDER_KEYWORD]; |
||
| 319 | |||
| 320 | if (!is_array($finders)) { |
||
| 321 | throw new InvalidArgumentException( |
||
| 322 | sprintf( |
||
| 323 | 'Expected finders to be an array of "%s", found "%s" instead.', |
||
| 324 | Finder::class, |
||
| 325 | gettype($finders), |
||
| 326 | ), |
||
| 327 | ); |
||
| 328 | } |
||
| 329 | |||
| 330 | foreach ($finders as $index => $finder) { |
||
| 331 | if ($finder instanceof Finder) { |
||
| 332 | continue; |
||
| 333 | } |
||
| 334 | |||
| 335 | throw new InvalidArgumentException( |
||
| 336 | sprintf( |
||
| 337 | 'Expected finders to be an array of "%s", the "%d" element is not.', |
||
| 338 | Finder::class, |
||
| 339 | $index, |
||
| 340 | ), |
||
| 341 | ); |
||
| 342 | } |
||
| 343 | |||
| 344 | return $finders; |
||
| 345 | } |
||
| 346 | |||
| 347 | /** |
||
| 348 | * @param string[] $paths |
||
| 349 | * |
||
| 350 | * @return iterable<SplFileInfo> |
||
| 351 | */ |
||
| 352 | private static function retrieveFilesFromPaths(array $paths): iterable |
||
| 353 | { |
||
| 354 | if ([] === $paths) { |
||
| 355 | return []; |
||
| 356 | } |
||
| 357 | |||
| 358 | $pathsToSearch = []; |
||
| 359 | $filesToAppend = []; |
||
| 360 | |||
| 361 | foreach ($paths as $path) { |
||
| 362 | if (!file_exists($path)) { |
||
| 363 | throw new RuntimeException( |
||
| 364 | sprintf( |
||
| 365 | 'Could not find the file "%s".', |
||
| 366 | $path, |
||
| 367 | ), |
||
| 368 | ); |
||
| 369 | } |
||
| 370 | |||
| 371 | if (is_dir($path)) { |
||
| 372 | $pathsToSearch[] = $path; |
||
| 373 | } else { |
||
| 374 | $filesToAppend[] = $path; |
||
| 375 | } |
||
| 376 | } |
||
| 377 | |||
| 378 | $finder = new Finder(); |
||
| 379 | |||
| 380 | $finder->files() |
||
| 381 | ->in($pathsToSearch) |
||
| 382 | ->append($filesToAppend) |
||
| 383 | ->filter( |
||
| 384 | static fn (SplFileInfo $fileInfo) => $fileInfo->isLink() ? false : null, |
||
| 385 | ) |
||
| 386 | ->sortByName(); |
||
| 387 | |||
| 388 | return $finder; |
||
| 389 | } |
||
| 390 | |||
| 391 | /** |
||
| 392 | * @param iterable<SplFileInfo|string> $files |
||
| 393 | * |
||
| 394 | * @return array<string, array{string, string}> Array of tuple with the first argument being the file path and the second its contents |
||
| 395 | */ |
||
| 396 | private static function retrieveFilesWithContents(iterable $files): array |
||
| 427 | } |
||
| 428 | |||
| 429 | private static function generateRandomPrefix(): string |
||
| 430 | { |
||
| 431 | return '_PhpScoper'.bin2hex(random_bytes(6)); |
||
| 432 | } |
||
| 433 | } |
||
| 434 |