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 |
||
64 | class Json extends File implements Loader |
||
65 | { |
||
66 | /** |
||
67 | * Config file. |
||
68 | * |
||
69 | * @var array |
||
70 | */ |
||
71 | private $json; |
||
72 | |||
73 | /** |
||
74 | * Constructor. |
||
75 | * |
||
76 | * @param string $file |
||
77 | * @throws \phpbu\App\Exception |
||
78 | */ |
||
79 | public function __construct($file) |
||
84 | |||
85 | /** |
||
86 | * Return list of adapter configs. |
||
87 | * |
||
88 | * @return array |
||
89 | * @throws \phpbu\App\Exception |
||
90 | */ |
||
91 | protected function getAdapterConfigs() |
||
107 | |||
108 | /** |
||
109 | * Set the phpbu application settings. |
||
110 | * |
||
111 | * @param \phpbu\App\Configuration $configuration |
||
112 | */ |
||
113 | public function setAppSettings(Configuration $configuration) |
||
125 | |||
126 | /** |
||
127 | * Set the log configuration. |
||
128 | * |
||
129 | * @param \phpbu\App\Configuration $configuration |
||
130 | * @throws \phpbu\App\Exception |
||
131 | */ |
||
132 | public function setLoggers(Configuration $configuration) |
||
152 | |||
153 | /** |
||
154 | * Set the backup configurations. |
||
155 | * |
||
156 | * @param \phpbu\App\Configuration $configuration |
||
157 | * @throws \phpbu\App\Exception |
||
158 | */ |
||
159 | public function setBackups(Configuration $configuration) |
||
168 | |||
169 | /** |
||
170 | * Get the config for a single backup node. |
||
171 | * |
||
172 | * @param array $json |
||
173 | * @throws \phpbu\App\Exception |
||
174 | * @return \phpbu\App\Configuration\Backup |
||
175 | */ |
||
176 | View Code Duplication | private function getBackupConfig(array $json) |
|
192 | |||
193 | /** |
||
194 | * Get source configuration. |
||
195 | * |
||
196 | * @param array $json |
||
197 | * @return \phpbu\App\Configuration\Backup\Source |
||
198 | * @throws \phpbu\App\Exception |
||
199 | */ |
||
200 | protected function getSource(array $json) |
||
211 | |||
212 | /** |
||
213 | * Get Target configuration. |
||
214 | * |
||
215 | * @param array $json |
||
216 | * @return \phpbu\App\Configuration\Backup\Target |
||
217 | * @throws \phpbu\App\Exception |
||
218 | */ |
||
219 | protected function getTarget(array $json) |
||
234 | |||
235 | /** |
||
236 | * Set backup checks. |
||
237 | * |
||
238 | * @param \phpbu\App\Configuration\Backup $backup |
||
239 | * @param array $json |
||
240 | */ |
||
241 | protected function setChecks(Configuration\Backup $backup, array $json) |
||
255 | |||
256 | /** |
||
257 | * Set the crypt configuration. |
||
258 | * |
||
259 | * @param \phpbu\App\Configuration\Backup $backup |
||
260 | * @param array $json |
||
261 | * @throws \phpbu\App\Exception |
||
262 | */ |
||
263 | View Code Duplication | protected function setCrypt(Configuration\Backup $backup, array $json) |
|
275 | |||
276 | /** |
||
277 | * Set backup sync configurations. |
||
278 | * |
||
279 | * @param \phpbu\App\Configuration\Backup $backup |
||
280 | * @param array $json |
||
281 | * @throws \phpbu\App\Exception |
||
282 | */ |
||
283 | protected function setSyncs(Configuration\Backup $backup, array $json) |
||
297 | |||
298 | /** |
||
299 | * Set the cleanup configuration. |
||
300 | * |
||
301 | * @param \phpbu\App\Configuration\Backup $backup |
||
302 | * @param array $json |
||
303 | * @throws \phpbu\App\Exception |
||
304 | */ |
||
305 | View Code Duplication | protected function setCleanup(Configuration\Backup $backup, array $json) |
|
317 | |||
318 | /** |
||
319 | * Extracts all option tags. |
||
320 | * |
||
321 | * @param array $json |
||
322 | * @return array |
||
323 | */ |
||
324 | protected function getOptions(array $json) |
||
334 | |||
335 | /** |
||
336 | * Load the JSON-File. |
||
337 | * |
||
338 | * @param string $filename |
||
339 | * @throws \phpbu\App\Exception |
||
340 | * @return array |
||
341 | */ |
||
342 | private function loadJsonFile($filename) |
||
354 | } |
||
355 |