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 FileExecutor 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 FileExecutor, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 8 | class FileExecutor extends AbstractExecutor |
||
| 9 | { |
||
| 10 | use IgnorableStepExecutorTrait; |
||
| 11 | |||
| 12 | protected $supportedStepTypes = array('file'); |
||
| 13 | protected $supportedActions = array('load', 'save', 'copy', 'move', 'delete', 'append', 'prepend', 'exists'); |
||
| 14 | |||
| 15 | /** @var PrefixBasedResolverInterface $referenceResolver */ |
||
| 16 | protected $referenceResolver; |
||
| 17 | |||
| 18 | public function __construct(PrefixBasedResolverInterface $referenceResolver) |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @param MigrationStep $step |
||
| 25 | * @return mixed |
||
| 26 | * @throws \Exception |
||
| 27 | */ |
||
| 28 | View Code Duplication | public function execute(MigrationStep $step) |
|
| 46 | |||
| 47 | /** |
||
| 48 | * @param array $dsl |
||
| 49 | * @param array $context |
||
| 50 | * @return string |
||
| 51 | * @throws \Exception |
||
| 52 | */ |
||
| 53 | protected function load($dsl, $context) |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @param array $dsl |
||
| 70 | * @param array $context |
||
| 71 | * @return string |
||
| 72 | * @throws \Exception |
||
| 73 | */ |
||
| 74 | protected function exists($dsl, $context) |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @param array $dsl |
||
| 102 | * @param array $context |
||
| 103 | * @return int |
||
| 104 | * @throws \Exception |
||
| 105 | */ |
||
| 106 | protected function save($dsl, $context) |
||
| 107 | { |
||
| 108 | View Code Duplication | if (!isset($dsl['file']) || (!isset($dsl['body']) && !isset($dsl['template']))) { |
|
| 109 | throw new \Exception("Can not save file: name or body or template missing"); |
||
| 110 | } |
||
| 111 | |||
| 112 | View Code Duplication | if (isset($dsl['body']) && is_string($dsl['body'])) { |
|
| 113 | $contents = $this->resolveReferencesInText($dsl['body']); |
||
| 114 | } elseif (isset($dsl['template']) && is_string($dsl['template'])) { |
||
| 115 | $path = $this->referenceResolver->resolveReference($dsl['template']); |
||
| 116 | // we use the same logic as for the image/file fields in content: look up file 1st relative to the migration |
||
| 117 | $template = dirname($context['path']) . '/templates/' . $path; |
||
| 118 | if (!is_file($template)) { |
||
| 119 | $template = $path; |
||
| 120 | } |
||
| 121 | $contents = $this->resolveReferencesInText(file_get_contents($template)); |
||
| 122 | } else { |
||
| 123 | throw new \Exception("Can not save file: either body or template tag must be a string"); |
||
| 124 | } |
||
| 125 | |||
| 126 | $fileName = $this->referenceResolver->resolveReference($dsl['file']); |
||
| 127 | |||
| 128 | $overwrite = isset($dsl['overwrite']) ? $overwrite = $dsl['overwrite'] : false; |
||
| 129 | if (!$overwrite && file_exists($fileName)) { |
||
| 130 | throw new \Exception("Can not save file '$fileName: file already exists"); |
||
| 131 | } |
||
| 132 | |||
| 133 | $return = file_put_contents($fileName, $contents); |
||
| 134 | |||
| 135 | $this->setReferences($fileName, $dsl); |
||
| 136 | |||
| 137 | return $return; |
||
| 138 | } |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @param array $dsl |
||
| 142 | * @param array $context |
||
| 143 | * @return int |
||
| 144 | * @throws \Exception |
||
| 145 | */ |
||
| 146 | protected function append($dsl, $context) |
||
| 147 | { |
||
| 148 | View Code Duplication | if (!isset($dsl['file']) || (!isset($dsl['body']) && !isset($dsl['template']))) { |
|
| 149 | throw new \Exception("Can not append to file: name or body or template missing"); |
||
| 150 | } |
||
| 151 | |||
| 152 | View Code Duplication | if (isset($dsl['body']) && is_string($dsl['body'])) { |
|
| 153 | $contents = $this->resolveReferencesInText($dsl['body']); |
||
| 154 | } elseif (isset($dsl['template']) && is_string($dsl['template'])) { |
||
| 155 | $path = $this->referenceResolver->resolveReference($dsl['template']); |
||
| 156 | // we use the same logic as for the image/file fields in content: look up file 1st relative to the migration |
||
| 157 | $template = dirname($context['path']) . '/templates/' . $path; |
||
| 158 | if (!is_file($template)) { |
||
| 159 | $template = $path; |
||
| 160 | } |
||
| 161 | $contents = $this->resolveReferencesInText(file_get_contents($template)); |
||
| 162 | } else { |
||
| 163 | throw new \Exception("Can not append to file: either body or template tag must be a string"); |
||
| 164 | } |
||
| 165 | |||
| 166 | $fileName = $this->referenceResolver->resolveReference($dsl['file']); |
||
| 167 | |||
| 168 | $return = file_put_contents($fileName, $contents, FILE_APPEND); |
||
| 169 | |||
| 170 | $this->setReferences($fileName, $dsl); |
||
| 171 | |||
| 172 | return $return; |
||
| 173 | } |
||
| 174 | |||
| 175 | /** |
||
| 176 | * @param array $dsl |
||
| 177 | * @param array $context |
||
| 178 | * @return int |
||
| 179 | * @throws \Exception |
||
| 180 | */ |
||
| 181 | protected function prepend($dsl, $context) |
||
| 182 | { |
||
| 183 | View Code Duplication | if (!isset($dsl['file']) || (!isset($dsl['body']) && !isset($dsl['template']))) { |
|
| 184 | throw new \Exception("Can not prepend to file: name or body or template missing"); |
||
| 185 | } |
||
| 186 | |||
| 187 | View Code Duplication | if (isset($dsl['body']) && is_string($dsl['body'])) { |
|
| 188 | $contents = $this->resolveReferencesInText($dsl['body']); |
||
| 189 | } elseif (isset($dsl['template']) && is_string($dsl['template'])) { |
||
| 190 | $path = $this->referenceResolver->resolveReference($dsl['template']); |
||
| 191 | // we use the same logic as for the image/file fields in content: look up file 1st relative to the migration |
||
| 192 | $template = dirname($context['path']) . '/templates/' . $path; |
||
| 193 | if (!is_file($template)) { |
||
| 194 | $template = $path; |
||
| 195 | } |
||
| 196 | $contents = $this->resolveReferencesInText(file_get_contents($template)); |
||
| 197 | } else { |
||
| 198 | throw new \Exception("Can not append to file: either body or template tag must be a string"); |
||
| 199 | } |
||
| 200 | |||
| 201 | $fileName = $this->referenceResolver->resolveReference($dsl['file']); |
||
| 202 | |||
| 203 | if (file_exists($fileName)) { |
||
| 204 | $contents .= file_get_contents($fileName); |
||
| 205 | } |
||
| 206 | |||
| 207 | $return = file_put_contents($fileName, $contents); |
||
| 208 | |||
| 209 | $this->setReferences($fileName, $dsl); |
||
| 210 | |||
| 211 | return $return; |
||
| 212 | } |
||
| 213 | |||
| 214 | /** |
||
| 215 | * @param array $dsl |
||
| 216 | * @param array $context |
||
| 217 | * @return true |
||
| 218 | * @throws \Exception |
||
| 219 | */ |
||
| 220 | View Code Duplication | protected function copy($dsl, $context) |
|
| 245 | |||
| 246 | /** |
||
| 247 | * @param array $dsl |
||
| 248 | * @param array $context |
||
| 249 | * @return true |
||
| 250 | * @throws \Exception |
||
| 251 | */ |
||
| 252 | View Code Duplication | protected function move($dsl, $context) |
|
| 277 | |||
| 278 | /** |
||
| 279 | * @param array $dsl |
||
| 280 | * @param array $context |
||
| 281 | * @return true |
||
| 282 | * @throws \Exception |
||
| 283 | */ |
||
| 284 | protected function delete($dsl, $context) |
||
| 303 | |||
| 304 | protected function setReferences($fileName, $dsl) |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Replaces any references inside a string |
||
| 356 | * |
||
| 357 | * @param string $text |
||
| 358 | * @return string |
||
| 359 | */ |
||
| 360 | View Code Duplication | protected function resolveReferencesInText($text) |
|
| 378 | } |
||
| 379 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.