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 Json 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 Json, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 68 | class Json extends File implements Loader |
||
| 69 | { |
||
| 70 | /** |
||
| 71 | * Config file. |
||
| 72 | * |
||
| 73 | * @var array |
||
| 74 | */ |
||
| 75 | private $json; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Constructor. |
||
| 79 | * |
||
| 80 | * @param string $file |
||
| 81 | * @throws \phpbu\App\Exception |
||
| 82 | */ |
||
| 83 | public function __construct($file) |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Return list of adapter configs. |
||
| 91 | * |
||
| 92 | * @return array |
||
| 93 | * @throws \phpbu\App\Exception |
||
| 94 | */ |
||
| 95 | protected function getAdapterConfigs() |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Set the phpbu application settings. |
||
| 114 | * |
||
| 115 | * @param \phpbu\App\Configuration $configuration |
||
| 116 | */ |
||
| 117 | public function setAppSettings(Configuration $configuration) |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Set the php settings. |
||
| 132 | * Checking for include_path and ini settings. |
||
| 133 | * |
||
| 134 | * @param \phpbu\App\Configuration $configuration |
||
| 135 | */ |
||
| 136 | public function setPhpSettings(Configuration $configuration) |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Set the log configuration. |
||
| 154 | * |
||
| 155 | * @param \phpbu\App\Configuration $configuration |
||
| 156 | * @throws \phpbu\App\Exception |
||
| 157 | */ |
||
| 158 | public function setLoggers(Configuration $configuration) |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Set the backup configurations. |
||
| 181 | * |
||
| 182 | * @param \phpbu\App\Configuration $configuration |
||
| 183 | * @throws \phpbu\App\Exception |
||
| 184 | */ |
||
| 185 | public function setBackups(Configuration $configuration) |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Get the config for a single backup node. |
||
| 197 | * |
||
| 198 | * @param array $json |
||
| 199 | * @throws \phpbu\App\Exception |
||
| 200 | * @return \phpbu\App\Configuration\Backup |
||
| 201 | */ |
||
| 202 | View Code Duplication | private function getBackupConfig(array $json) |
|
| 218 | |||
| 219 | /** |
||
| 220 | * Get source configuration. |
||
| 221 | * |
||
| 222 | * @param array $json |
||
| 223 | * @return \phpbu\App\Configuration\Backup\Source |
||
| 224 | * @throws \phpbu\App\Exception |
||
| 225 | */ |
||
| 226 | protected function getSource(array $json) |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Get Target configuration. |
||
| 240 | * |
||
| 241 | * @param array $json |
||
| 242 | * @return \phpbu\App\Configuration\Backup\Target |
||
| 243 | * @throws \phpbu\App\Exception |
||
| 244 | */ |
||
| 245 | protected function getTarget(array $json) |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Set backup checks. |
||
| 263 | * |
||
| 264 | * @param \phpbu\App\Configuration\Backup $backup |
||
| 265 | * @param array $json |
||
| 266 | */ |
||
| 267 | protected function setChecks(Configuration\Backup $backup, array $json) |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Set the crypt configuration. |
||
| 284 | * |
||
| 285 | * @param \phpbu\App\Configuration\Backup $backup |
||
| 286 | * @param array $json |
||
| 287 | * @throws \phpbu\App\Exception |
||
| 288 | */ |
||
| 289 | View Code Duplication | protected function setCrypt(Configuration\Backup $backup, array $json) |
|
| 301 | |||
| 302 | /** |
||
| 303 | * Set backup sync configurations. |
||
| 304 | * |
||
| 305 | * @param \phpbu\App\Configuration\Backup $backup |
||
| 306 | * @param array $json |
||
| 307 | * @throws \phpbu\App\Exception |
||
| 308 | */ |
||
| 309 | protected function setSyncs(Configuration\Backup $backup, array $json) |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Set the cleanup configuration. |
||
| 326 | * |
||
| 327 | * @param \phpbu\App\Configuration\Backup $backup |
||
| 328 | * @param array $json |
||
| 329 | * @throws \phpbu\App\Exception |
||
| 330 | */ |
||
| 331 | View Code Duplication | protected function setCleanup(Configuration\Backup $backup, array $json) |
|
| 343 | |||
| 344 | /** |
||
| 345 | * Extracts all option tags. |
||
| 346 | * |
||
| 347 | * @param array $json |
||
| 348 | * @return array |
||
| 349 | */ |
||
| 350 | protected function getOptions(array $json) |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Load the JSON-File. |
||
| 363 | * |
||
| 364 | * @param string $filename |
||
| 365 | * @throws \phpbu\App\Exception |
||
| 366 | * @return array |
||
| 367 | */ |
||
| 368 | private function loadJsonFile($filename) |
||
| 380 | } |
||
| 381 |