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 | 18 | public function __construct($file) |
|
91 | { |
||
92 | 18 | parent::__construct($file); |
|
93 | 18 | $this->document = $this->loadXmlFile($file); |
|
94 | 16 | $this->xpath = new DOMXPath($this->document); |
|
95 | 16 | } |
|
96 | |||
97 | /** |
||
98 | * Return list of adapter configs. |
||
99 | * |
||
100 | * @return array |
||
101 | * @throws \phpbu\App\Exception |
||
102 | */ |
||
103 | 16 | protected function getAdapterConfigs() |
|
121 | |||
122 | /** |
||
123 | * Set the phpbu application settings. |
||
124 | * |
||
125 | * @param \phpbu\App\Configuration $configuration |
||
126 | */ |
||
127 | 14 | public function setAppSettings(Configuration $configuration) |
|
128 | { |
||
129 | 14 | $root = $this->document->documentElement; |
|
130 | |||
131 | 14 | if ($root->hasAttribute('bootstrap')) { |
|
132 | 14 | $configuration->setBootstrap($this->toAbsolutePath($root->getAttribute('bootstrap'))); |
|
133 | } |
||
134 | 14 | if ($root->hasAttribute('verbose')) { |
|
135 | 14 | $configuration->setVerbose(Str::toBoolean($root->getAttribute('verbose'), false)); |
|
136 | } |
||
137 | 14 | if ($root->hasAttribute('colors')) { |
|
138 | 14 | $configuration->setColors(Str::toBoolean($root->getAttribute('colors'), false)); |
|
139 | } |
||
140 | 14 | } |
|
141 | |||
142 | /** |
||
143 | * Set the log configuration. |
||
144 | * |
||
145 | * @param \phpbu\App\Configuration $configuration |
||
146 | * @throws \phpbu\App\Exception |
||
147 | */ |
||
148 | 14 | public function setLoggers(Configuration $configuration) |
|
168 | |||
169 | /** |
||
170 | * Set the backup configurations. |
||
171 | * |
||
172 | * @param \phpbu\App\Configuration $configuration |
||
173 | * @throws \phpbu\App\Exception |
||
174 | */ |
||
175 | 13 | public function setBackups(Configuration $configuration) |
|
176 | { |
||
177 | 13 | foreach ($this->xpath->query('backups/backup') as $backupNode) { |
|
178 | 13 | $configuration->addBackup($this->getBackupConfig($backupNode)); |
|
179 | } |
||
180 | 6 | } |
|
181 | |||
182 | /** |
||
183 | * 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 | 13 | View Code Duplication | private function getBackupConfig(DOMElement $backupNode) |
190 | { |
||
191 | 13 | $stopOnFailure = Str::toBoolean($backupNode->getAttribute('stopOnFailure'), false); |
|
192 | 13 | $backupName = $backupNode->getAttribute('name'); |
|
193 | 13 | $backup = new Configuration\Backup($backupName, $stopOnFailure); |
|
194 | |||
195 | 13 | $backup->setSource($this->getSource($backupNode)); |
|
196 | 11 | $backup->setTarget($this->getTarget($backupNode)); |
|
197 | |||
198 | 10 | $this->setChecks($backup, $backupNode); |
|
199 | 10 | $this->setCrypt($backup, $backupNode); |
|
200 | 9 | $this->setSyncs($backup, $backupNode); |
|
201 | 7 | $this->setCleanup($backup, $backupNode); |
|
202 | |||
203 | 6 | return $backup; |
|
204 | } |
||
205 | |||
206 | /** |
||
207 | * Get source configuration. |
||
208 | * |
||
209 | * @param \DOMElement $node |
||
210 | * @return \phpbu\App\Configuration\Backup\Source |
||
211 | * @throws \phpbu\App\Exception |
||
212 | */ |
||
213 | 13 | protected function getSource(DOMElement $node) |
|
214 | { |
||
215 | 13 | $sources = $node->getElementsByTagName('source'); |
|
216 | 13 | if ($sources->length !== 1) { |
|
217 | 1 | throw new Exception('backup requires exactly one source config'); |
|
218 | } |
||
219 | /** @var DOMElement $sourceNode */ |
||
220 | 12 | $sourceNode = $sources->item(0); |
|
221 | 12 | $type = $sourceNode->getAttribute('type'); |
|
222 | 12 | if (!$type) { |
|
223 | 1 | throw new Exception('source requires type attribute'); |
|
224 | } |
||
225 | |||
226 | 11 | return new Configuration\Backup\Source($type, $this->getOptions($sourceNode)); |
|
227 | } |
||
228 | |||
229 | /** |
||
230 | * Get Target configuration. |
||
231 | * |
||
232 | * @param \DOMElement $node |
||
233 | * @return \phpbu\App\Configuration\Backup\Target |
||
234 | * @throws \phpbu\App\Exception |
||
235 | */ |
||
236 | 11 | protected function getTarget(DOMElement $node) |
|
254 | |||
255 | /** |
||
256 | * Set backup checks. |
||
257 | * |
||
258 | * @param \phpbu\App\Configuration\Backup $backup |
||
259 | * @param \DOMElement $node |
||
260 | */ |
||
261 | 10 | protected function setChecks(Configuration\Backup $backup, DOMElement $node) |
|
274 | |||
275 | /** |
||
276 | * Set the crypt configuration. |
||
277 | * |
||
278 | * @param \phpbu\App\Configuration\Backup $backup |
||
279 | * @param \DOMElement $node |
||
280 | * @throws \phpbu\App\Exception |
||
281 | */ |
||
282 | 10 | View Code Duplication | protected function setCrypt(Configuration\Backup $backup, DOMElement $node) |
298 | |||
299 | /** |
||
300 | * Set backup sync configurations. |
||
301 | * |
||
302 | * @param \phpbu\App\Configuration\Backup $backup |
||
303 | * @param \DOMElement $node |
||
304 | * @throws \phpbu\App\Exception |
||
305 | */ |
||
306 | 9 | protected function setSyncs(Configuration\Backup $backup, DOMElement $node) |
|
319 | |||
320 | /** |
||
321 | * Set the cleanup configuration. |
||
322 | * |
||
323 | * @param \phpbu\App\Configuration\Backup $backup |
||
324 | * @param \DOMElement $node |
||
325 | * @throws \phpbu\App\Exception |
||
326 | */ |
||
327 | 7 | View Code Duplication | protected function setCleanup(Configuration\Backup $backup, DOMElement $node) |
343 | |||
344 | /** |
||
345 | * Extracts all option tags. |
||
346 | * |
||
347 | * @param DOMElement $node |
||
348 | * @return array |
||
349 | */ |
||
350 | 14 | protected function getOptions(DOMElement $node) |
|
361 | |||
362 | /** |
||
363 | * Load the XML-File. |
||
364 | * |
||
365 | * @param string $filename |
||
366 | * @throws \phpbu\App\Exception |
||
367 | * @return \DOMDocument |
||
368 | */ |
||
369 | 18 | private function loadXmlFile($filename) |
|
398 | } |
||
399 |