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 Xml 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 Xml, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
68 | class Xml extends File implements Loader |
||
69 | { |
||
70 | /** |
||
71 | * Config file DOMDocument |
||
72 | * |
||
73 | * @var \DOMDocument |
||
74 | */ |
||
75 | private $document; |
||
76 | |||
77 | /** |
||
78 | * Xpath to navigate the config DOM. |
||
79 | * |
||
80 | * @var \DOMXPath |
||
81 | */ |
||
82 | private $xpath; |
||
83 | |||
84 | /** |
||
85 | * Constructor. |
||
86 | * |
||
87 | * @param string $file |
||
88 | * @throws \phpbu\App\Exception |
||
89 | */ |
||
90 | public function __construct($file) |
||
96 | |||
97 | 15 | /** |
|
98 | 15 | * Return list of adapter configs. |
|
99 | 13 | * |
|
100 | 13 | * @return array |
|
101 | * @throws \phpbu\App\Exception |
||
102 | */ |
||
103 | protected function getAdapterConfigs() |
||
121 | |||
122 | /** |
||
123 | * Set the phpbu application settings. |
||
124 | * |
||
125 | * @param \phpbu\App\Configuration $configuration |
||
126 | */ |
||
127 | public function setAppSettings(Configuration $configuration) |
||
141 | 11 | ||
142 | 13 | /** |
|
143 | 13 | * Set the log configuration. |
|
144 | * |
||
145 | * @param \phpbu\App\Configuration $configuration |
||
146 | * @throws \phpbu\App\Exception |
||
147 | */ |
||
148 | public function setLoggers(Configuration $configuration) |
||
168 | 10 | ||
169 | 12 | /** |
|
170 | 12 | * Set the backup configurations. |
|
171 | * |
||
172 | * @param \phpbu\App\Configuration $configuration |
||
173 | * @throws \phpbu\App\Exception |
||
174 | */ |
||
175 | public function setBackups(Configuration $configuration) |
||
181 | 12 | ||
182 | 6 | /** |
|
183 | 6 | * Get the config for a single backup node. |
|
184 | * |
||
185 | * @param \DOMElement $backupNode |
||
186 | * @throws \phpbu\App\Exception |
||
187 | * @return \phpbu\App\Configuration\Backup |
||
188 | */ |
||
189 | View Code Duplication | private function getBackupConfig(DOMElement $backupNode) |
|
205 | |||
206 | 6 | /** |
|
207 | * Get source configuration. |
||
208 | * |
||
209 | * @param \DOMElement $node |
||
210 | * @return \phpbu\App\Configuration\Backup\Source |
||
211 | * @throws \phpbu\App\Exception |
||
212 | */ |
||
213 | protected function getSource(DOMElement $node) |
||
228 | |||
229 | 10 | /** |
|
230 | * Get Target configuration. |
||
231 | * |
||
232 | * @param \DOMElement $node |
||
233 | * @return \phpbu\App\Configuration\Backup\Target |
||
234 | * @throws \phpbu\App\Exception |
||
235 | */ |
||
236 | protected function getTarget(DOMElement $node) |
||
254 | |||
255 | 9 | /** |
|
256 | * Set backup checks. |
||
257 | * |
||
258 | * @param \phpbu\App\Configuration\Backup $backup |
||
259 | * @param \DOMElement $node |
||
260 | */ |
||
261 | protected function setChecks(Configuration\Backup $backup, DOMElement $node) |
||
274 | 8 | ||
275 | 9 | /** |
|
276 | 9 | * Set the crypt configuration. |
|
277 | * |
||
278 | * @param \phpbu\App\Configuration\Backup $backup |
||
279 | * @param \DOMElement $node |
||
280 | * @throws \phpbu\App\Exception |
||
281 | */ |
||
282 | View Code Duplication | protected function setCrypt(Configuration\Backup $backup, DOMElement $node) |
|
298 | 6 | ||
299 | 6 | /** |
|
300 | 8 | * Set backup sync configurations. |
|
301 | * |
||
302 | * @param \phpbu\App\Configuration\Backup $backup |
||
303 | * @param \DOMElement $node |
||
304 | * @throws \phpbu\App\Exception |
||
305 | */ |
||
306 | protected function setSyncs(Configuration\Backup $backup, DOMElement $node) |
||
319 | 7 | ||
320 | 7 | /** |
|
321 | 7 | * Set the cleanup configuration. |
|
322 | * |
||
323 | * @param \phpbu\App\Configuration\Backup $backup |
||
324 | * @param \DOMElement $node |
||
325 | * @throws \phpbu\App\Exception |
||
326 | */ |
||
327 | View Code Duplication | protected function setCleanup(Configuration\Backup $backup, DOMElement $node) |
|
343 | 6 | ||
344 | 6 | /** |
|
345 | 6 | * Extracts all option tags. |
|
346 | * |
||
347 | * @param DOMElement $node |
||
348 | * @return array |
||
349 | */ |
||
350 | protected function getOptions(DOMElement $node) |
||
361 | 11 | ||
362 | 11 | /** |
|
363 | * Load the XML-File. |
||
364 | * |
||
365 | * @param string $filename |
||
366 | * @throws \phpbu\App\Exception |
||
367 | * @return \DOMDocument |
||
368 | */ |
||
369 | private function loadXmlFile($filename) |
||
398 | } |
||
399 |