Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 |
||
| 9 | class Hydrator implements ConfigurableProcessor |
||
| 10 | { |
||
| 11 | use \Karma\Logging\LoggerAware; |
||
| 12 | |||
| 13 | const |
||
| 14 | TODO_VALUE = '__TODO__', |
||
| 15 | FIXME_VALUE = '__FIXME__', |
||
| 16 | VARIABLE_REGEX = '~<%(?P<variableName>[A-Za-z0-9_\.\-]+)%>~'; |
||
| 17 | |||
| 18 | private |
||
| 19 | $sources, |
||
|
|
|||
| 20 | $suffix, |
||
| 21 | $reader, |
||
| 22 | $dryRun, |
||
| 23 | $enableBackup, |
||
| 24 | $finder, |
||
| 25 | $formatterProvider, |
||
| 26 | $currentFormatterName, |
||
| 27 | $currentTargetFile, |
||
| 28 | $systemEnvironment, |
||
| 29 | $unusedVariables, |
||
| 30 | $unvaluedVariables; |
||
| 31 | |||
| 32 | public function __construct(Filesystem $sources, Configuration $reader, Finder $finder, FormatterProvider $formatterProvider = null) |
||
| 33 | { |
||
| 34 | $this->logger = new NullLogger(); |
||
| 35 | |||
| 36 | $this->sources = $sources; |
||
| 37 | $this->reader = $reader; |
||
| 38 | $this->finder = $finder; |
||
| 39 | |||
| 40 | $this->suffix = Application::DEFAULT_DISTFILE_SUFFIX; |
||
| 41 | $this->dryRun = false; |
||
| 42 | $this->enableBackup = false; |
||
| 43 | |||
| 44 | $this->formatterProvider = $formatterProvider; |
||
| 45 | if($this->formatterProvider === null) |
||
| 46 | { |
||
| 47 | $this->formatterProvider = new NullProvider(); |
||
| 48 | } |
||
| 49 | |||
| 50 | $this->currentFormatterName = null; |
||
| 51 | $this->currentTargetFile = null; |
||
| 52 | $this->systemEnvironment = null; |
||
| 53 | $this->unusedVariables = array_flip($reader->getAllVariables()); |
||
| 54 | $this->unvaluedVariables = array(); |
||
| 55 | } |
||
| 56 | |||
| 57 | public function setSuffix($suffix) |
||
| 58 | { |
||
| 59 | $this->suffix = $suffix; |
||
| 60 | |||
| 61 | return $this; |
||
| 62 | } |
||
| 63 | |||
| 64 | public function setDryRun($value = true) |
||
| 65 | { |
||
| 66 | $this->dryRun = (bool) $value; |
||
| 67 | |||
| 68 | return $this; |
||
| 69 | } |
||
| 70 | |||
| 71 | public function enableBackup($value = true) |
||
| 72 | { |
||
| 73 | $this->enableBackup = (bool) $value; |
||
| 74 | |||
| 75 | return $this; |
||
| 76 | } |
||
| 77 | |||
| 78 | public function setFormatterProvider(FormatterProvider $formatterProvider) |
||
| 79 | { |
||
| 80 | $this->formatterProvider = $formatterProvider; |
||
| 81 | |||
| 82 | return $this; |
||
| 83 | } |
||
| 84 | |||
| 85 | public function setSystemEnvironment($environment) |
||
| 86 | { |
||
| 87 | $this->systemEnvironment = $environment; |
||
| 88 | |||
| 89 | return $this; |
||
| 90 | } |
||
| 91 | |||
| 92 | public function hydrate($environment) |
||
| 93 | { |
||
| 94 | $distFiles = $this->collectDistFiles(); |
||
| 95 | |||
| 96 | foreach($distFiles as $file) |
||
| 97 | { |
||
| 98 | $this->hydrateFile($file, $environment); |
||
| 99 | } |
||
| 100 | |||
| 101 | $this->info(sprintf( |
||
| 102 | '%d files generated', |
||
| 103 | count($distFiles) |
||
| 104 | )); |
||
| 105 | } |
||
| 106 | |||
| 107 | private function collectDistFiles() |
||
| 111 | |||
| 112 | private function hydrateFile($file, $environment) |
||
| 113 | { |
||
| 114 | $this->currentTargetFile = substr($file, 0, strlen($this->suffix) * -1); |
||
| 115 | |||
| 116 | $content = $this->sources->read($file); |
||
| 117 | $replacementCounter = $this->parseFileDirectives($file, $content, $environment); |
||
| 118 | |||
| 129 | |||
| 130 | private function parseFileDirectives($file, & $fileContent, $environment) |
||
| 141 | |||
| 142 | private function parseFormatterDirective($file, $fileContent) |
||
| 158 | |||
| 159 | private function parseListDirective($file, & $fileContent, $environment) |
||
| 194 | |||
| 195 | private function lookingForSyntaxErrorInListDirective($file, $fileContent) |
||
| 203 | |||
| 204 | private function generateContentForListDirective($variable, $environment, $delimiter, array $wrapper) |
||
| 226 | |||
| 227 | private function removeFileDirectives($fileContent) |
||
| 231 | |||
| 232 | private function injectValues($sourceFile, $content, $environment, $replacementCounter = 0) |
||
| 244 | |||
| 245 | private function readValueToInject($variableName, $environment) |
||
| 260 | |||
| 261 | private function checkValueIsAllowed($variableName, $environment, $value) |
||
| 276 | |||
| 277 | private function getFormatterForCurrentTargetFile() |
||
| 283 | |||
| 284 | private function injectScalarValues(& $content, $environment) |
||
| 304 | |||
| 305 | private function injectListValues(& $content, $environment) |
||
| 340 | |||
| 341 | private function detectEol($content) |
||
| 355 | |||
| 356 | private function backupFile($targetFile) |
||
| 367 | |||
| 368 | public function rollback() |
||
| 377 | |||
| 378 | private function rollbackFile($file) |
||
| 396 | |||
| 397 | public function getUnusedVariables() |
||
| 401 | |||
| 402 | private function markVariableAsUsed($variableName) |
||
| 409 | |||
| 410 | public function getUnvaluedVariables() |
||
| 414 | } |
||
| 415 |
Only declaring a single property per statement allows you to later on add doc comments more easily.
It is also recommended by PSR2, so it is a common style that many people expect.