Complex classes like Configuration 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 Configuration, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
28 | class Configuration |
||
29 | { |
||
30 | private const PREFIX = 'prefix'; |
||
31 | private const FINDER_KEYWORD = 'finders'; |
||
32 | private const PATCHERS_KEYWORD = 'patchers'; |
||
33 | private const WHITELIST_KEYWORD = 'whitelist'; |
||
34 | private const GLOBAL_NAMESPACE_KEYWORD = 'global_namespace_whitelist'; |
||
35 | |||
36 | private const KEYWORDS = [ |
||
37 | self::PREFIX, |
||
38 | self::FINDER_KEYWORD, |
||
39 | self::PATCHERS_KEYWORD, |
||
40 | self::WHITELIST_KEYWORD, |
||
41 | self::GLOBAL_NAMESPACE_KEYWORD, |
||
42 | ]; |
||
43 | |||
44 | private $path; |
||
45 | private $prefix; |
||
46 | private $filesWithContents; |
||
47 | private $patchers; |
||
48 | private $whitelist; |
||
49 | |||
50 | /** |
||
51 | * @param string|null $path Absolute path to the configuration file. |
||
52 | * @param string[] $paths List of paths to append besides the one configured |
||
53 | * |
||
54 | * @return self |
||
55 | */ |
||
56 | public static function load(string $path = null, array $paths = []): self |
||
86 | |||
87 | /** |
||
88 | * @param string|null $path Absolute path to the configuration file loaded. |
||
89 | * @param string|null $prefix The prefix applied. |
||
90 | * @param [string, string][] $filesWithContents Array of tuple with the first argument being the file path and the second its contents |
||
91 | * @param callable[] $patchers List of closures which can alter the content of the files being |
||
92 | * scoped. |
||
93 | * @param string[] $whitelist List of classes that will not be scoped. |
||
94 | * @param Closure $globalNamespaceWhitelisters Closure taking a class name from the global namespace as an argument and |
||
|
|||
95 | * returning a boolean which if `true` means the class should be scoped |
||
96 | * (i.e. is ignored) or scoped otherwise. |
||
97 | */ |
||
98 | private function __construct( |
||
111 | |||
112 | public function withPaths(array $paths): self |
||
130 | |||
131 | public function withPrefix(?string $prefix): self |
||
143 | |||
144 | public function getPath(): string |
||
148 | |||
149 | public function getPrefix(): ?string |
||
153 | |||
154 | public function getFilesWithContents(): array |
||
158 | |||
159 | /** |
||
160 | * @return callable[] |
||
161 | */ |
||
162 | public function getPatchers(): array |
||
166 | |||
167 | public function getWhitelist(): array |
||
171 | |||
172 | private static function validateConfigKeys(array $config): void |
||
179 | |||
180 | private static function validateConfigKey(string $key): void |
||
191 | |||
192 | /** |
||
193 | * If the prefix is set to null in the config file/argument then a random prefix is being used. However if set to |
||
194 | * empty, the configuration will use a null prefix. |
||
195 | * |
||
196 | * TL:DR; setting the prefix is a big confusing because it is not properly split in "set prefix" & prefix strategy". |
||
197 | */ |
||
198 | private static function retrievePrefix(array $config): ?string |
||
210 | |||
211 | private static function retrievePatchers(array $config): array |
||
243 | |||
244 | private static function retrieveWhitelist(array $config): array |
||
276 | |||
277 | private static function retrieveFinders(array $config): array |
||
311 | |||
312 | /** |
||
313 | * @param string[] $paths |
||
314 | * |
||
315 | * @return iterable |
||
316 | */ |
||
317 | private static function retrieveFilesFromPaths(array $paths): iterable |
||
353 | |||
354 | /** |
||
355 | * @param Iterator $files |
||
356 | * |
||
357 | * @return [string, string][] Array of tuple with the first argument being the file path and the second its contents |
||
358 | */ |
||
359 | private static function retrieveFilesWithContents(Iterator $files): array |
||
391 | } |
||
392 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.
Consider the following example. The parameter
$ireland
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was changed, but the annotation was not.