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 |
||
18 | { |
||
19 | /** |
||
20 | * Configuration is a Data object. |
||
21 | * |
||
22 | * @var Data |
||
23 | */ |
||
24 | protected $data; |
||
25 | /** |
||
26 | * Local configuration. |
||
27 | * |
||
28 | * @var Config|array |
||
29 | */ |
||
30 | protected $localConfig; |
||
31 | /** |
||
32 | * Source directory. |
||
33 | * |
||
34 | * @var string |
||
35 | */ |
||
36 | protected $sourceDir; |
||
37 | /** |
||
38 | * Destination directory. |
||
39 | * |
||
40 | * @var string |
||
41 | */ |
||
42 | protected $destinationDir; |
||
43 | |||
44 | /** |
||
45 | * Config constructor. |
||
46 | * |
||
47 | * @param Config|array|null $config |
||
48 | */ |
||
49 | public function __construct($config = null) |
||
57 | |||
58 | /** |
||
59 | * Import config data into the current configuration. |
||
60 | * |
||
61 | * @param Config|array|null $config |
||
62 | * |
||
63 | * @return void |
||
64 | */ |
||
65 | public function import($config): void |
||
99 | |||
100 | /** |
||
101 | * Set a Data object as configuration. |
||
102 | * |
||
103 | * @param Data $data |
||
104 | * |
||
105 | * @return $this |
||
106 | */ |
||
107 | protected function setData(Data $data): self |
||
115 | |||
116 | /** |
||
117 | * Get configuration as a Data object. |
||
118 | * |
||
119 | * @return Data |
||
120 | */ |
||
121 | public function getData(): Data |
||
125 | |||
126 | /** |
||
127 | * Get configuration as an array. |
||
128 | * |
||
129 | * @return array |
||
130 | */ |
||
131 | public function getAsArray(): array |
||
135 | |||
136 | /** |
||
137 | * Is configuration's key' exists? |
||
138 | * |
||
139 | * @param string $key |
||
140 | * |
||
141 | * @return bool |
||
142 | */ |
||
143 | public function has(string $key): bool |
||
147 | |||
148 | /** |
||
149 | * Get the value of a configuration's key'. |
||
150 | * |
||
151 | * @param string $key |
||
152 | * @param string|null $language |
||
153 | * |
||
154 | * @return array|mixed|null |
||
155 | */ |
||
156 | public function get(string $key, string $language = null) |
||
169 | |||
170 | /** |
||
171 | * Set the source directory. |
||
172 | * |
||
173 | * @param string|null $sourceDir |
||
174 | * |
||
175 | * @throws \InvalidArgumentException |
||
176 | * |
||
177 | * @return $this |
||
178 | */ |
||
179 | public function setSourceDir(string $sourceDir = null): self |
||
191 | |||
192 | /** |
||
193 | * Get the source directory. |
||
194 | * |
||
195 | * @return string |
||
196 | */ |
||
197 | public function getSourceDir(): string |
||
201 | |||
202 | /** |
||
203 | * Set the destination directory. |
||
204 | * |
||
205 | * @param string|null $destinationDir |
||
206 | * |
||
207 | * @throws \InvalidArgumentException |
||
208 | * |
||
209 | * @return $this |
||
210 | */ |
||
211 | public function setDestinationDir(string $destinationDir = null): self |
||
226 | |||
227 | /** |
||
228 | * Get the destination directory. |
||
229 | * |
||
230 | * @return string |
||
231 | */ |
||
232 | public function getDestinationDir(): string |
||
236 | |||
237 | /** |
||
238 | * Paths helpers. |
||
239 | */ |
||
240 | |||
241 | /** |
||
242 | * Return the path of the content directory. |
||
243 | * |
||
244 | * @return string |
||
245 | */ |
||
246 | public function getContentPath(): string |
||
250 | |||
251 | /** |
||
252 | * Return the path of the data directory. |
||
253 | * |
||
254 | * @return string |
||
255 | */ |
||
256 | public function getDataPath(): string |
||
260 | |||
261 | /** |
||
262 | * Return the path of templates directory. |
||
263 | * |
||
264 | * @return string |
||
265 | */ |
||
266 | public function getLayoutsPath(): string |
||
270 | |||
271 | /** |
||
272 | * Return the path of themes directory. |
||
273 | * |
||
274 | * @return string |
||
275 | */ |
||
276 | public function getThemesPath(): string |
||
280 | |||
281 | /** |
||
282 | * Return the path of internal templates directory. |
||
283 | * |
||
284 | * @return string |
||
285 | */ |
||
286 | public function getInternalLayoutsPath(): string |
||
290 | |||
291 | /** |
||
292 | * Return the path of the output directory. |
||
293 | * |
||
294 | * @return string |
||
295 | */ |
||
296 | public function getOutputPath(): string |
||
300 | |||
301 | /** |
||
302 | * Return the path of static files directory. |
||
303 | * |
||
304 | * @return string |
||
305 | */ |
||
306 | public function getStaticPath(): string |
||
310 | |||
311 | /** |
||
312 | * Return a "clean" array of an output format. |
||
313 | * |
||
314 | * @param string $format |
||
315 | * |
||
316 | * @return array |
||
317 | */ |
||
318 | public function getOutputFormat(string $format): array |
||
331 | |||
332 | /** |
||
333 | * Theme helpers. |
||
334 | */ |
||
335 | |||
336 | /** |
||
337 | * Return theme(s) as an array. |
||
338 | * |
||
339 | * @return array|null |
||
340 | */ |
||
341 | public function getTheme(): ?array |
||
353 | |||
354 | /** |
||
355 | * Has a (valid) theme(s)? |
||
356 | * |
||
357 | * @throws Exception |
||
358 | * |
||
359 | * @return bool |
||
360 | */ |
||
361 | public function hasTheme(): bool |
||
379 | |||
380 | /** |
||
381 | * Return the path of a specific theme's directory. |
||
382 | * ("layouts" by default). |
||
383 | * |
||
384 | * @param string $theme |
||
385 | * @param string $dir |
||
386 | * |
||
387 | * @return string |
||
388 | */ |
||
389 | public function getThemeDirPath(string $theme, string $dir = 'layouts'): string |
||
393 | |||
394 | /** |
||
395 | * Language helpers. |
||
396 | */ |
||
397 | |||
398 | /** |
||
399 | * Return an array of available languages. |
||
400 | * |
||
401 | * @return array |
||
402 | */ |
||
403 | public function getLanguages(): array |
||
407 | |||
408 | /** |
||
409 | * Return the default language code (ie: "en", "fr-fr", etc.). |
||
410 | * |
||
411 | * @return string |
||
412 | */ |
||
413 | public function getLanguageDefault(): string |
||
427 | |||
428 | /** |
||
429 | * Return the property value of a (specified or default) language. |
||
430 | * |
||
431 | * @param string $property |
||
432 | * @param string|null $key |
||
433 | * |
||
434 | * @return string |
||
435 | */ |
||
436 | public function getLanguageProperty($property, $key = null): string |
||
457 | } |
||
458 |
This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.
Unreachable code is most often the result of
return
,die
orexit
statements that have been added for debug purposes.In the above example, the last
return false
will never be executed, because a return statement has already been met in every possible execution path.