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) |
||
168 | |||
169 | /** |
||
170 | * Set the source directory. |
||
171 | * |
||
172 | * @param string|null $sourceDir |
||
173 | * |
||
174 | * @throws \InvalidArgumentException |
||
175 | * |
||
176 | * @return $this |
||
177 | */ |
||
178 | public function setSourceDir(string $sourceDir = null): self |
||
190 | |||
191 | /** |
||
192 | * Get the source directory. |
||
193 | * |
||
194 | * @return string |
||
195 | */ |
||
196 | public function getSourceDir(): string |
||
200 | |||
201 | /** |
||
202 | * Set the destination directory. |
||
203 | * |
||
204 | * @param string|null $destinationDir |
||
205 | * |
||
206 | * @throws \InvalidArgumentException |
||
207 | * |
||
208 | * @return $this |
||
209 | */ |
||
210 | public function setDestinationDir(string $destinationDir = null): self |
||
225 | |||
226 | /** |
||
227 | * Get the destination directory. |
||
228 | * |
||
229 | * @return string |
||
230 | */ |
||
231 | public function getDestinationDir(): string |
||
235 | |||
236 | /** |
||
237 | * Paths helpers. |
||
238 | */ |
||
239 | |||
240 | /** |
||
241 | * Return the path of the content directory. |
||
242 | * |
||
243 | * @return string |
||
244 | */ |
||
245 | public function getContentPath(): string |
||
249 | |||
250 | /** |
||
251 | * Return the path of the data directory. |
||
252 | * |
||
253 | * @return string |
||
254 | */ |
||
255 | public function getDataPath(): string |
||
259 | |||
260 | /** |
||
261 | * Return the path of templates directory. |
||
262 | * |
||
263 | * @return string |
||
264 | */ |
||
265 | public function getLayoutsPath(): string |
||
269 | |||
270 | /** |
||
271 | * Return the path of themes directory. |
||
272 | * |
||
273 | * @return string |
||
274 | */ |
||
275 | public function getThemesPath(): string |
||
279 | |||
280 | /** |
||
281 | * Return the path of internal templates directory. |
||
282 | * |
||
283 | * @return string |
||
284 | */ |
||
285 | public function getInternalLayoutsPath(): string |
||
289 | |||
290 | /** |
||
291 | * Return the path of the output directory. |
||
292 | * |
||
293 | * @return string |
||
294 | */ |
||
295 | public function getOutputPath(): string |
||
299 | |||
300 | /** |
||
301 | * Return the path of static files directory. |
||
302 | * |
||
303 | * @return string |
||
304 | */ |
||
305 | public function getStaticPath(): string |
||
309 | |||
310 | /** |
||
311 | * Return a "clean" array of an output format. |
||
312 | * |
||
313 | * @param string $format |
||
314 | * |
||
315 | * @return array |
||
316 | */ |
||
317 | /*public function getOutputFormat(string $format): array |
||
318 | { |
||
319 | $default = [ |
||
320 | 'mediatype' => null, // 'text/html' |
||
321 | 'subpath' => null, // '' |
||
322 | 'suffix' => null, // '/index' |
||
323 | 'extension' => null, // 'html' |
||
324 | ]; |
||
325 | |||
326 | $result = $this->get(sprintf('output.formats.%s', $format)); |
||
327 | |||
328 | return array_merge($default, $result); |
||
329 | }*/ |
||
330 | |||
331 | /** |
||
332 | * Return the property value of an output format. |
||
333 | * |
||
334 | * @param string $property |
||
335 | * @param string $name |
||
336 | * |
||
337 | * @return string|array|null |
||
338 | */ |
||
339 | public function getOutputFormatProperty(string $name, string $property) |
||
359 | |||
360 | /** |
||
361 | * Theme helpers. |
||
362 | */ |
||
363 | |||
364 | /** |
||
365 | * Return theme(s) as an array. |
||
366 | * |
||
367 | * @return array|null |
||
368 | */ |
||
369 | public function getTheme(): ?array |
||
381 | |||
382 | /** |
||
383 | * Has a (valid) theme(s)? |
||
384 | * |
||
385 | * @throws Exception |
||
386 | * |
||
387 | * @return bool |
||
388 | */ |
||
389 | public function hasTheme(): bool |
||
407 | |||
408 | /** |
||
409 | * Return the path of a specific theme's directory. |
||
410 | * ("layouts" by default). |
||
411 | * |
||
412 | * @param string $theme |
||
413 | * @param string $dir |
||
414 | * |
||
415 | * @return string |
||
416 | */ |
||
417 | public function getThemeDirPath(string $theme, string $dir = 'layouts'): string |
||
421 | |||
422 | /** |
||
423 | * Language helpers. |
||
424 | */ |
||
425 | |||
426 | /** |
||
427 | * Return an array of available languages. |
||
428 | * |
||
429 | * @return array |
||
430 | */ |
||
431 | public function getLanguages(): array |
||
435 | |||
436 | /** |
||
437 | * Return the default language code (ie: "en", "fr-fr", etc.). |
||
438 | * |
||
439 | * @return string |
||
440 | */ |
||
441 | public function getLanguageDefault(): string |
||
449 | |||
450 | /** |
||
451 | * Return a language code index. |
||
452 | * |
||
453 | * @param string $code |
||
454 | * |
||
455 | * @return int |
||
456 | */ |
||
457 | public function getLanguageIndex(string $code): int |
||
467 | |||
468 | /** |
||
469 | * Return the property value of a (specified or default) language. |
||
470 | * |
||
471 | * @param string $property |
||
472 | * @param string|null $code |
||
473 | * |
||
474 | * @return string|null |
||
475 | */ |
||
476 | public function getLanguageProperty(string $property, string $code = null): ?string |
||
492 | } |
||
493 |