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 | protected $supportedStepTypes = array('file'); |
||
| 11 | protected $supportedActions = array('load', 'save', 'copy', 'move', 'delete', 'append', 'prepend'); |
||
| 12 | |||
| 13 | /** @var PrefixBasedResolverInterface $referenceResolver */ |
||
| 14 | protected $referenceResolver; |
||
| 15 | |||
| 16 | 73 | public function __construct(PrefixBasedResolverInterface $referenceResolver) |
|
| 20 | |||
| 21 | /** |
||
| 22 | * @param MigrationStep $step |
||
| 23 | * @return mixed |
||
| 24 | * @throws \Exception |
||
| 25 | */ |
||
| 26 | 2 | View Code Duplication | public function execute(MigrationStep $step) |
|
|
|||
| 27 | { |
||
| 28 | 2 | parent::execute($step); |
|
| 29 | |||
| 30 | 2 | if (!isset($step->dsl['mode'])) { |
|
| 31 | throw new \Exception("Invalid step definition: missing 'mode'"); |
||
| 32 | } |
||
| 33 | |||
| 34 | 2 | $action = $step->dsl['mode']; |
|
| 35 | |||
| 36 | 2 | if (!in_array($action, $this->supportedActions)) { |
|
| 37 | throw new \Exception("Invalid step definition: value '$action' is not allowed for 'mode'"); |
||
| 38 | } |
||
| 39 | |||
| 40 | 2 | return $this->$action($step->dsl, $step->context); |
|
| 41 | } |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @param array $dsl |
||
| 45 | * @param array $context |
||
| 46 | * @return string |
||
| 47 | * @throws \Exception |
||
| 48 | */ |
||
| 49 | 2 | protected function load($dsl, $context) |
|
| 63 | |||
| 64 | /** |
||
| 65 | * @param array $dsl |
||
| 66 | * @param array $context |
||
| 67 | * @return int |
||
| 68 | * @throws \Exception |
||
| 69 | */ |
||
| 70 | 1 | protected function save($dsl, $context) |
|
| 95 | |||
| 96 | /** |
||
| 97 | * @param array $dsl |
||
| 98 | * @param array $context |
||
| 99 | * @return int |
||
| 100 | * @throws \Exception |
||
| 101 | */ |
||
| 102 | 1 | View Code Duplication | protected function append($dsl, $context) |
| 122 | |||
| 123 | /** |
||
| 124 | * @param array $dsl |
||
| 125 | * @param array $context |
||
| 126 | * @return int |
||
| 127 | * @throws \Exception |
||
| 128 | */ |
||
| 129 | 1 | View Code Duplication | protected function prepend($dsl, $context) |
| 151 | |||
| 152 | 1 | /** |
|
| 153 | * @param array $dsl |
||
| 154 | * @param array $context |
||
| 155 | * @return true |
||
| 156 | * @throws \Exception |
||
| 157 | */ |
||
| 158 | View Code Duplication | protected function copy($dsl, $context) |
|
| 183 | |||
| 184 | 1 | /** |
|
| 185 | * @param array $dsl |
||
| 186 | * @param array $context |
||
| 187 | * @return true |
||
| 188 | * @throws \Exception |
||
| 189 | */ |
||
| 190 | View Code Duplication | protected function move($dsl, $context) |
|
| 215 | 2 | ||
| 216 | 1 | /** |
|
| 217 | * @param array $dsl |
||
| 218 | * @param array $context |
||
| 219 | 2 | * @return true |
|
| 220 | 2 | * @throws \Exception |
|
| 221 | */ |
||
| 222 | 2 | protected function delete($dsl, $context) |
|
| 241 | 1 | ||
| 242 | 1 | protected function setReferences($fileName, $dsl) |
|
| 291 | |||
| 292 | /** |
||
| 293 | * Replaces any references inside a string |
||
| 294 | * |
||
| 295 | * @param string |
||
| 296 | * @return string |
||
| 297 | */ |
||
| 298 | View Code Duplication | protected function resolveReferencesInText($text) |
|
| 316 | } |
||
| 317 |
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.