Complex classes like Hydrator 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 Hydrator, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | class Hydrator implements ConfigurableProcessor |
||
12 | { |
||
13 | use \Karma\Logging\LoggerAware; |
||
14 | |||
15 | const |
||
16 | TODO_VALUE = '__TODO__', |
||
17 | FIXME_VALUE = '__FIXME__', |
||
18 | VARIABLE_REGEX = '~<%(?P<variableName>[A-Za-z0-9_\.\-]+)%>~'; |
||
19 | |||
20 | private |
||
21 | $sources, |
||
22 | $suffix, |
||
23 | $reader, |
||
24 | $dryRun, |
||
25 | $enableBackup, |
||
26 | $finder, |
||
27 | $formatterProvider, |
||
28 | $currentFormatterName, |
||
29 | $currentTargetFile, |
||
30 | $systemEnvironment, |
||
31 | $unusedVariables, |
||
32 | $unvaluedVariables, |
||
33 | $target, |
||
34 | $nonDistFilesOverwriteAllowed, |
||
35 | $hydratedFiles; |
||
36 | |||
37 | 87 | public function __construct(Filesystem $sources, Filesystem $target, Configuration $reader, Finder $finder, FormatterProvider $formatterProvider = null) |
|
64 | |||
65 | 87 | public function setSuffix(string $suffix) |
|
71 | |||
72 | 2 | public function setDryRun(bool $value = true): ConfigurableProcessor |
|
78 | |||
79 | 1 | public function enableBackup(bool $value = true): ConfigurableProcessor |
|
85 | |||
86 | 15 | public function allowNonDistFilesOverwrite(bool $nonDistFilesOverwriteAllowed = true) |
|
92 | |||
93 | 3 | public function setFormatterProvider(FormatterProvider $formatterProvider) |
|
99 | |||
100 | 5 | public function setSystemEnvironment(?string $environment): ConfigurableProcessor |
|
106 | |||
107 | 82 | public function hydrate(string $environment): void |
|
126 | |||
127 | 86 | private function collectFiles(): iterable |
|
133 | |||
134 | 3 | private function copyNonDistFiles(): void |
|
143 | |||
144 | 3 | private function collectNonDistFiles(): iterable |
|
150 | |||
151 | |||
152 | 82 | private function hydrateFile(string $file, string $environment): void |
|
184 | |||
185 | 60 | private function hasBeenHydrated(string $file): bool |
|
189 | |||
190 | 82 | private function parseFileDirectives(string $file, string & $fileContent, string $environment): int |
|
201 | |||
202 | 82 | private function parseFormatterDirective(string $file, string $fileContent): void |
|
203 | { |
||
204 | 82 | if($count = preg_match_all('~<%\s*karma:formatter\s*=\s*(?P<formatterName>[^%]+)%>~', $fileContent, $matches)) |
|
205 | { |
||
206 | 3 | if($count !== 1) |
|
207 | { |
||
208 | 1 | throw new \RuntimeException(sprintf( |
|
209 | 1 | 'Syntax error in %s : only one formatter directive is allowed (%d found)', |
|
210 | $file, |
||
211 | $count |
||
212 | )); |
||
213 | } |
||
214 | |||
215 | 2 | $this->currentFormatterName = strtolower(trim($matches['formatterName'][0])); |
|
216 | } |
||
217 | 81 | } |
|
218 | |||
219 | 81 | private function parseListDirective(string $file, string & $fileContent, string $environment): int |
|
254 | |||
255 | 80 | private function lookingForSyntaxErrorInListDirective(string $file, string $fileContent): void |
|
263 | |||
264 | 30 | private function generateContentForListDirective(string $variable, string $environment, string $delimiter, array $wrapper): string |
|
286 | |||
287 | 63 | private function removeFileDirectives($fileContent) |
|
291 | |||
292 | 63 | private function injectValues(string $sourceFile, string $content, string $environment, int & $replacementCounter = 0): string |
|
304 | |||
305 | 60 | private function readValueToInject(string $variableName, string $environment) |
|
320 | |||
321 | 59 | private function checkValueIsAllowed(string $variableName, string $environment, $value): void |
|
322 | { |
||
323 | 59 | if($value === self::FIXME_VALUE) |
|
324 | { |
||
325 | 1 | throw new \RuntimeException(sprintf( |
|
326 | 1 | 'Missing value for variable %s in environment %s (FIXME marker found)', |
|
327 | $variableName, |
||
328 | $environment |
||
329 | )); |
||
330 | } |
||
331 | |||
332 | 58 | if($value === self::TODO_VALUE) |
|
333 | { |
||
334 | 2 | $this->unvaluedVariables[] = $variableName; |
|
335 | } |
||
336 | 58 | } |
|
337 | |||
338 | 63 | private function getFormatterForCurrentTargetFile(): Formatter |
|
344 | |||
345 | 63 | private function injectScalarValues(string & $content, string $environment): int |
|
346 | { |
||
347 | 63 | $formatter = $this->getFormatterForCurrentTargetFile(); |
|
348 | |||
349 | 63 | $content = preg_replace_callback(self::VARIABLE_REGEX, function(array $matches) use($environment, $formatter) |
|
350 | { |
||
351 | 34 | $value = $this->readValueToInject($matches['variableName'], $environment); |
|
352 | |||
353 | 33 | if(is_array($value)) |
|
354 | { |
||
355 | // don't replace lists at this time |
||
356 | 14 | return $matches[0]; |
|
357 | } |
||
358 | |||
359 | 26 | return $formatter->format($value); |
|
360 | |||
361 | 63 | }, $content, -1, $count); |
|
362 | |||
363 | 62 | return $count; |
|
364 | } |
||
365 | |||
366 | 62 | private function injectListValues(string & $content, string $environment): int |
|
367 | { |
||
368 | 62 | $formatter = $this->getFormatterForCurrentTargetFile(); |
|
369 | 62 | $replacementCounter = 0; |
|
370 | |||
371 | 62 | $eol = $this->detectEol($content); |
|
372 | |||
373 | 62 | while(preg_match(self::VARIABLE_REGEX, $content)) |
|
374 | { |
||
375 | 15 | $lines = explode($eol, $content); |
|
376 | 15 | $result = []; |
|
377 | |||
378 | 15 | foreach($lines as $lineNumber => $line) |
|
379 | { |
||
380 | 15 | if(preg_match(self::VARIABLE_REGEX, $line, $matches)) |
|
381 | { |
||
382 | 15 | $values = $this->readValueToInject($matches['variableName'], $environment); |
|
383 | |||
384 | 15 | if(!is_array($values)) |
|
385 | { |
||
386 | 1 | throw new \RuntimeException(sprintf( |
|
387 | 1 | "Nested variable detected [%s] while writing %s at line %d", |
|
388 | 1 | $matches['variableName'], |
|
389 | 1 | $this->currentTargetFile, |
|
390 | $lineNumber |
||
391 | )); |
||
392 | } |
||
393 | |||
394 | 14 | $replacementCounter++; |
|
395 | 14 | foreach($values as $value) |
|
396 | { |
||
397 | 14 | $result[] = preg_replace(self::VARIABLE_REGEX, $formatter->format($value), $line, 1); |
|
398 | } |
||
399 | |||
400 | 14 | continue; |
|
401 | } |
||
402 | |||
403 | 8 | $result[] = $line; |
|
404 | } |
||
405 | |||
406 | 14 | $content = implode($eol, $result); |
|
407 | } |
||
408 | |||
409 | 61 | return $replacementCounter; |
|
410 | } |
||
411 | |||
412 | 62 | private function detectEol(string $content): string |
|
426 | |||
427 | 60 | private function backupFile(string $targetFile): void |
|
438 | |||
439 | 4 | public function rollback(): void |
|
448 | |||
449 | 2 | private function rollbackFile(string $file): void |
|
467 | |||
468 | 9 | public function getUnusedVariables(): array |
|
472 | |||
473 | 60 | private function markVariableAsUsed(string $variableName): void |
|
480 | |||
481 | 9 | public function getUnvaluedVariables(): array |
|
485 | |||
486 | public function hydratedFiles(): array |
||
490 | } |
||
491 |
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.