| Total Complexity | 49 |
| Total Lines | 396 |
| 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 | public const DEFAULT_FILE_NAME = 'scoper.inc.php'; |
||
| 59 | |||
| 60 | private Filesystem $fileSystem; |
||
| 61 | private SymbolsConfigurationFactory $configurationWhitelistFactory; |
||
| 62 | |||
| 63 | public function __construct( |
||
| 64 | Filesystem $fileSystem, |
||
| 65 | SymbolsConfigurationFactory $configurationWhitelistFactory |
||
| 66 | ) { |
||
| 67 | $this->fileSystem = $fileSystem; |
||
| 68 | $this->configurationWhitelistFactory = $configurationWhitelistFactory; |
||
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @param non-empty-string|null $path Absolute canonical path to the configuration file. |
||
|
|
|||
| 73 | * @param list<non-empty-string> $paths List of absolute canonical paths to append besides the one configured |
||
| 74 | */ |
||
| 75 | public function create(?string $path = null, array $paths = []): Configuration |
||
| 76 | { |
||
| 77 | if (null === $path) { |
||
| 78 | $config = []; |
||
| 79 | } else { |
||
| 80 | $config = $this->loadConfigFile($path); |
||
| 81 | } |
||
| 82 | |||
| 83 | self::validateConfigKeys($config); |
||
| 84 | |||
| 85 | $prefix = self::retrievePrefix($config); |
||
| 86 | $outputDir = self::retrieveOutputDir($config); |
||
| 87 | |||
| 88 | $excludedFiles = null === $path |
||
| 89 | ? [] |
||
| 90 | : $this->retrieveExcludedFiles( |
||
| 91 | dirname($path), |
||
| 92 | $config, |
||
| 93 | ); |
||
| 94 | |||
| 95 | $patchers = self::retrievePatchers($config); |
||
| 96 | |||
| 97 | array_unshift($patchers, new SymfonyPatcher()); |
||
| 98 | array_unshift($patchers, new ComposerPatcher()); |
||
| 99 | |||
| 100 | $symbolsConfiguration = $this->configurationWhitelistFactory->createSymbolsConfiguration($config); |
||
| 101 | |||
| 102 | $finders = self::retrieveFinders($config); |
||
| 103 | $filesFromPaths = self::retrieveFilesFromPaths($paths); |
||
| 104 | $filesWithContents = self::retrieveFilesWithContents(chain($filesFromPaths, ...$finders)); |
||
| 105 | |||
| 106 | return new Configuration( |
||
| 107 | $path, |
||
| 108 | $outputDir, |
||
| 109 | $prefix, |
||
| 110 | $filesWithContents, |
||
| 111 | self::retrieveFilesWithContents($excludedFiles), |
||
| 112 | new PatcherChain($patchers), |
||
| 113 | $symbolsConfiguration, |
||
| 114 | ); |
||
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @param string[] $paths |
||
| 119 | */ |
||
| 120 | public function createWithPaths(Configuration $config, array $paths): Configuration |
||
| 121 | { |
||
| 122 | $filesWithContents = self::retrieveFilesWithContents( |
||
| 123 | chain( |
||
| 124 | self::retrieveFilesFromPaths( |
||
| 125 | array_unique($paths, SORT_STRING), |
||
| 126 | ), |
||
| 127 | ), |
||
| 128 | ); |
||
| 129 | |||
| 130 | return new Configuration( |
||
| 131 | $config->getPath(), |
||
| 132 | $config->getOutputDir(), |
||
| 133 | $config->getPrefix(), |
||
| 134 | array_merge( |
||
| 135 | $config->getFilesWithContents(), |
||
| 136 | $filesWithContents, |
||
| 137 | ), |
||
| 138 | $config->getExcludedFilesWithContents(), |
||
| 139 | $config->getPatcher(), |
||
| 140 | $config->getSymbolsConfiguration(), |
||
| 141 | ); |
||
| 142 | } |
||
| 143 | |||
| 144 | public function createWithPrefix(Configuration $config, string $prefix): Configuration |
||
| 145 | { |
||
| 146 | $prefix = self::retrievePrefix([ConfigurationKeys::PREFIX_KEYWORD => $prefix]); |
||
| 147 | |||
| 148 | return new Configuration( |
||
| 149 | $config->getPath(), |
||
| 150 | $config->getOutputDir(), |
||
| 151 | $prefix, |
||
| 152 | $config->getFilesWithContents(), |
||
| 153 | $config->getExcludedFilesWithContents(), |
||
| 154 | $config->getPatcher(), |
||
| 155 | $config->getSymbolsConfiguration(), |
||
| 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 | /** |
||
| 229 | * @return non-empty-string |
||
| 230 | */ |
||
| 231 | private static function retrievePrefix(array $config): string |
||
| 232 | { |
||
| 233 | $prefix = trim((string) ($config[ConfigurationKeys::PREFIX_KEYWORD] ?? '')); |
||
| 234 | |||
| 235 | return '' === $prefix ? self::generateRandomPrefix() : $prefix; |
||
| 236 | |||
| 237 | } |
||
| 238 | |||
| 239 | /** |
||
| 240 | * @return non-empty-string|null |
||
| 241 | */ |
||
| 242 | private static function retrieveOutputDir(array $config): ?string |
||
| 247 | |||
| 248 | } |
||
| 249 | |||
| 250 | /** |
||
| 251 | * @return array<(callable(string,string,string): string)|Patcher> |
||
| 252 | */ |
||
| 253 | private static function retrievePatchers(array $config): array |
||
| 254 | { |
||
| 255 | if (!array_key_exists(ConfigurationKeys::PATCHERS_KEYWORD, $config)) { |
||
| 256 | return []; |
||
| 257 | } |
||
| 258 | |||
| 259 | $patchers = $config[ConfigurationKeys::PATCHERS_KEYWORD]; |
||
| 260 | |||
| 261 | if (!is_array($patchers)) { |
||
| 262 | throw new InvalidArgumentException( |
||
| 263 | sprintf( |
||
| 264 | 'Expected patchers to be an array of callables, found "%s" instead.', |
||
| 265 | gettype($patchers), |
||
| 266 | ), |
||
| 267 | ); |
||
| 268 | } |
||
| 269 | |||
| 270 | foreach ($patchers as $index => $patcher) { |
||
| 271 | if (is_callable($patcher)) { |
||
| 272 | continue; |
||
| 273 | } |
||
| 274 | |||
| 275 | throw new InvalidArgumentException( |
||
| 276 | sprintf( |
||
| 277 | 'Expected patchers to be an array of callables, the "%d" element is not.', |
||
| 278 | $index, |
||
| 279 | ), |
||
| 280 | ); |
||
| 281 | } |
||
| 282 | |||
| 283 | return $patchers; |
||
| 284 | } |
||
| 285 | |||
| 286 | /** |
||
| 287 | * @return string[] Absolute paths |
||
| 288 | */ |
||
| 289 | private function retrieveExcludedFiles(string $dirPath, array $config): array |
||
| 290 | { |
||
| 291 | if (!array_key_exists(ConfigurationKeys::EXCLUDED_FILES_KEYWORD, $config)) { |
||
| 292 | return []; |
||
| 293 | } |
||
| 294 | |||
| 295 | $excludedFiles = $config[ConfigurationKeys::EXCLUDED_FILES_KEYWORD]; |
||
| 296 | |||
| 297 | if (!is_array($excludedFiles)) { |
||
| 298 | throw new InvalidArgumentException( |
||
| 299 | sprintf( |
||
| 300 | 'Expected excluded files to be an array of strings, found "%s" instead.', |
||
| 301 | gettype($excludedFiles), |
||
| 302 | ), |
||
| 303 | ); |
||
| 304 | } |
||
| 305 | |||
| 306 | foreach ($excludedFiles as $index => $file) { |
||
| 307 | if (!is_string($file)) { |
||
| 308 | throw new InvalidArgumentException( |
||
| 309 | sprintf( |
||
| 310 | 'Expected excluded files to be an array of string, the "%d" element is not.', |
||
| 311 | $index, |
||
| 312 | ), |
||
| 313 | ); |
||
| 314 | } |
||
| 315 | |||
| 316 | if (!$this->fileSystem->isAbsolutePath($file)) { |
||
| 317 | $file = $dirPath.DIRECTORY_SEPARATOR.$file; |
||
| 318 | } |
||
| 319 | |||
| 320 | $excludedFiles[$index] = realpath($file); |
||
| 321 | } |
||
| 322 | |||
| 323 | return array_filter($excludedFiles); |
||
| 324 | } |
||
| 325 | |||
| 326 | /** |
||
| 327 | * @return Finder[] |
||
| 328 | */ |
||
| 329 | private static function retrieveFinders(array $config): array |
||
| 330 | { |
||
| 331 | if (!array_key_exists(ConfigurationKeys::FINDER_KEYWORD, $config)) { |
||
| 332 | return []; |
||
| 333 | } |
||
| 334 | |||
| 335 | $finders = $config[ConfigurationKeys::FINDER_KEYWORD]; |
||
| 336 | |||
| 337 | if (!is_array($finders)) { |
||
| 338 | throw new InvalidArgumentException( |
||
| 339 | sprintf( |
||
| 340 | 'Expected finders to be an array of "%s", found "%s" instead.', |
||
| 341 | Finder::class, |
||
| 342 | gettype($finders), |
||
| 343 | ), |
||
| 344 | ); |
||
| 345 | } |
||
| 346 | |||
| 347 | foreach ($finders as $index => $finder) { |
||
| 348 | if ($finder instanceof Finder) { |
||
| 349 | continue; |
||
| 350 | } |
||
| 351 | |||
| 352 | throw new InvalidArgumentException( |
||
| 353 | sprintf( |
||
| 354 | 'Expected finders to be an array of "%s", the "%d" element is not.', |
||
| 355 | Finder::class, |
||
| 356 | $index, |
||
| 357 | ), |
||
| 358 | ); |
||
| 359 | } |
||
| 360 | |||
| 361 | return $finders; |
||
| 362 | } |
||
| 363 | |||
| 364 | /** |
||
| 365 | * @param string[] $paths |
||
| 366 | * |
||
| 367 | * @return iterable<SplFileInfo> |
||
| 368 | */ |
||
| 369 | private static function retrieveFilesFromPaths(array $paths): iterable |
||
| 370 | { |
||
| 371 | if ([] === $paths) { |
||
| 372 | return []; |
||
| 373 | } |
||
| 374 | |||
| 375 | $pathsToSearch = []; |
||
| 376 | $filesToAppend = []; |
||
| 377 | |||
| 378 | foreach ($paths as $path) { |
||
| 379 | if (!file_exists($path)) { |
||
| 380 | throw new RuntimeException( |
||
| 381 | sprintf( |
||
| 382 | 'Could not find the file "%s".', |
||
| 383 | $path, |
||
| 384 | ), |
||
| 385 | ); |
||
| 386 | } |
||
| 387 | |||
| 388 | if (is_dir($path)) { |
||
| 389 | $pathsToSearch[] = $path; |
||
| 390 | } else { |
||
| 391 | $filesToAppend[] = $path; |
||
| 392 | } |
||
| 393 | } |
||
| 394 | |||
| 395 | $finder = new Finder(); |
||
| 396 | |||
| 397 | $finder->files() |
||
| 398 | ->in($pathsToSearch) |
||
| 399 | ->append($filesToAppend) |
||
| 400 | ->filter( |
||
| 401 | static fn (SplFileInfo $fileInfo) => $fileInfo->isLink() ? false : null, |
||
| 402 | ) |
||
| 403 | ->sortByName(); |
||
| 404 | |||
| 405 | return $finder; |
||
| 406 | } |
||
| 407 | |||
| 408 | /** |
||
| 409 | * @param iterable<SplFileInfo|string> $files |
||
| 410 | * |
||
| 411 | * @return array<string, array{string, string}> Array of tuple with the first argument being the file path and the second its contents |
||
| 412 | */ |
||
| 413 | private static function retrieveFilesWithContents(iterable $files): array |
||
| 444 | } |
||
| 445 | |||
| 446 | /** |
||
| 447 | * @return non-empty-string |
||
| 448 | */ |
||
| 449 | private static function generateRandomPrefix(): string |
||
| 450 | { |
||
| 451 | return '_PhpScoper'.bin2hex(random_bytes(6)); |
||
| 452 | } |
||
| 453 | } |
||
| 454 |