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 Config 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 Config, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | class Config implements \ArrayAccess |
||
18 | { |
||
19 | /** |
||
20 | * Configuration is a Data object. |
||
21 | * |
||
22 | * @var Data |
||
23 | */ |
||
24 | protected $data; |
||
25 | /** |
||
26 | * Source directory. |
||
27 | * |
||
28 | * @var string |
||
29 | */ |
||
30 | protected $sourceDir; |
||
31 | /** |
||
32 | * Destination directory. |
||
33 | * |
||
34 | * @var string |
||
35 | */ |
||
36 | protected $destinationDir; |
||
37 | |||
38 | /** |
||
39 | * Config constructor. |
||
40 | * |
||
41 | * @param Config|array|null $config |
||
42 | */ |
||
43 | public function __construct($config = null) |
||
78 | |||
79 | /** |
||
80 | * Import an array to the current configuration. |
||
81 | * |
||
82 | * @param array $config |
||
83 | */ |
||
84 | public function import($config) |
||
94 | |||
95 | /** |
||
96 | * Set a Data object as configuration. |
||
97 | * |
||
98 | * @param Data $data |
||
99 | * |
||
100 | * @return $this |
||
101 | */ |
||
102 | protected function setFromData(Data $data) |
||
110 | |||
111 | /** |
||
112 | * Get configuration as a Data object. |
||
113 | * |
||
114 | * @return Data |
||
115 | */ |
||
116 | public function getAll() |
||
120 | |||
121 | /** |
||
122 | * Get configuration as an array. |
||
123 | * |
||
124 | * @return array |
||
125 | */ |
||
126 | public function getAllAsArray() |
||
130 | |||
131 | /** |
||
132 | * Is configuration's key' exists? |
||
133 | * |
||
134 | * @param string $key |
||
135 | * |
||
136 | * @return bool |
||
137 | */ |
||
138 | public function has($key) |
||
142 | |||
143 | /** |
||
144 | * Get the value of a configuration's key'. |
||
145 | * |
||
146 | * @param string $key |
||
147 | * @param string $default |
||
148 | * |
||
149 | * @return array|mixed|null |
||
150 | */ |
||
151 | public function get($key, $default = '') |
||
155 | |||
156 | /** |
||
157 | * Implement ArrayAccess. |
||
158 | * |
||
159 | * @param mixed $offset |
||
160 | * |
||
161 | * @return bool |
||
162 | */ |
||
163 | public function offsetExists($offset) |
||
167 | |||
168 | /** |
||
169 | * Implement ArrayAccess. |
||
170 | * |
||
171 | * @param mixed $offset |
||
172 | * |
||
173 | * @return null |
||
174 | */ |
||
175 | public function offsetGet($offset) |
||
179 | |||
180 | /** |
||
181 | * Implement ArrayAccess. |
||
182 | * |
||
183 | * @param mixed $offset |
||
184 | * @param mixed $value |
||
185 | */ |
||
186 | public function offsetSet($offset, $value) |
||
190 | |||
191 | /** |
||
192 | * Implement ArrayAccess. |
||
193 | * |
||
194 | * @param mixed $offset |
||
195 | */ |
||
196 | public function offsetUnset($offset) |
||
200 | |||
201 | /** |
||
202 | * Set the source directory. |
||
203 | * |
||
204 | * @param null $sourceDir |
||
205 | * |
||
206 | * @throws Exception |
||
207 | * |
||
208 | * @return $this |
||
209 | */ |
||
210 | View Code Duplication | public function setSourceDir($sourceDir = null) |
|
222 | |||
223 | /** |
||
224 | * Get the source directory. |
||
225 | * |
||
226 | * @return string |
||
227 | */ |
||
228 | public function getSourceDir() |
||
232 | |||
233 | /** |
||
234 | * Set the destination directory. |
||
235 | * |
||
236 | * @param null $destinationDir |
||
237 | * |
||
238 | * @throws Exception |
||
239 | * |
||
240 | * @return $this |
||
241 | */ |
||
242 | View Code Duplication | public function setDestinationDir($destinationDir = null) |
|
257 | |||
258 | /** |
||
259 | * Get the destination directory. |
||
260 | * |
||
261 | * @return string |
||
262 | */ |
||
263 | public function getDestinationDir() |
||
267 | |||
268 | /** |
||
269 | * Paths helpers. |
||
270 | */ |
||
271 | |||
272 | /** |
||
273 | * Return the path of the content directory. |
||
274 | * |
||
275 | * @return string |
||
276 | */ |
||
277 | public function getContentPath() |
||
281 | |||
282 | /** |
||
283 | * Return the path of templates directory. |
||
284 | * |
||
285 | * @return string |
||
286 | */ |
||
287 | public function getLayoutsPath() |
||
291 | |||
292 | /** |
||
293 | * Return the path of themes directory. |
||
294 | * |
||
295 | * @return string |
||
296 | */ |
||
297 | public function getThemesPath() |
||
301 | |||
302 | /** |
||
303 | * Return the path of internal templates directory. |
||
304 | * |
||
305 | * @return string |
||
306 | */ |
||
307 | public function getInternalLayoutsPath() |
||
311 | |||
312 | /** |
||
313 | * Return the path of the output directory. |
||
314 | * |
||
315 | * @return string |
||
316 | */ |
||
317 | public function getOutputPath() |
||
321 | |||
322 | /** |
||
323 | * Return the path of static files directory. |
||
324 | * |
||
325 | * @return string |
||
326 | */ |
||
327 | public function getStaticPath() |
||
331 | |||
332 | /** |
||
333 | * Return a "clean" array of an output format. |
||
334 | * |
||
335 | * @param string $format |
||
336 | * |
||
337 | * @return array |
||
338 | */ |
||
339 | public function getOutputFormat(string $format): array |
||
352 | |||
353 | /** |
||
354 | * Theme helpers. |
||
355 | */ |
||
356 | |||
357 | /** |
||
358 | * Return theme(s) as an array. |
||
359 | * |
||
360 | * @return array|null |
||
361 | */ |
||
362 | public function getTheme() |
||
372 | |||
373 | /** |
||
374 | * Has a (valid) theme(s)? |
||
375 | * |
||
376 | * @throws Exception |
||
377 | * |
||
378 | * @return bool |
||
379 | */ |
||
380 | public function hasTheme() |
||
398 | |||
399 | /** |
||
400 | * Return the path of a specific theme's directory. |
||
401 | * ("layouts" by default). |
||
402 | * |
||
403 | * @param string $theme |
||
404 | * @param string $dir |
||
405 | * |
||
406 | * @return string |
||
407 | */ |
||
408 | public function getThemeDirPath($theme, $dir = 'layouts') |
||
412 | |||
413 | /** |
||
414 | * Language helpers. |
||
415 | */ |
||
416 | |||
417 | /** |
||
418 | * Return an array of available languages. |
||
419 | * |
||
420 | * @return array |
||
421 | */ |
||
422 | public function getLanguages(): array |
||
426 | |||
427 | /** |
||
428 | * Return the default language key (ie: "en", "fr-fr", etc.). |
||
429 | * |
||
430 | * @return string |
||
431 | */ |
||
432 | public function getLanguageDefaultKey(): string |
||
446 | |||
447 | /** |
||
448 | * Return properties of a (specified or default) language. |
||
449 | * |
||
450 | * @return array |
||
451 | */ |
||
452 | public function getLanguageProperties($key = null): array |
||
463 | |||
464 | /** |
||
465 | * Return the property value of a (specified or default) language. |
||
466 | * |
||
467 | * @return string |
||
468 | */ |
||
469 | public function getLanguageProperty($property, $key = null): string |
||
490 | } |
||
491 |