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|null |
||
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 $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) |
||
167 | |||
168 | /** |
||
169 | * Set the source directory. |
||
170 | * |
||
171 | * @param string|null $sourceDir |
||
172 | * |
||
173 | * @throws \InvalidArgumentException |
||
174 | * |
||
175 | * @return $this |
||
176 | */ |
||
177 | public function setSourceDir(string $sourceDir = null): self |
||
189 | |||
190 | /** |
||
191 | * Get the source directory. |
||
192 | * |
||
193 | * @return string |
||
194 | */ |
||
195 | public function getSourceDir(): string |
||
199 | |||
200 | /** |
||
201 | * Set the destination directory. |
||
202 | * |
||
203 | * @param string|null $destinationDir |
||
204 | * |
||
205 | * @throws \InvalidArgumentException |
||
206 | * |
||
207 | * @return $this |
||
208 | */ |
||
209 | public function setDestinationDir(string $destinationDir = null): self |
||
224 | |||
225 | /** |
||
226 | * Get the destination directory. |
||
227 | * |
||
228 | * @return string |
||
229 | */ |
||
230 | public function getDestinationDir(): string |
||
234 | |||
235 | /** |
||
236 | * Paths helpers. |
||
237 | */ |
||
238 | |||
239 | /** |
||
240 | * Return the path of the content directory. |
||
241 | * |
||
242 | * @return string |
||
243 | */ |
||
244 | public function getContentPath(): string |
||
248 | |||
249 | /** |
||
250 | * Return the path of templates directory. |
||
251 | * |
||
252 | * @return string |
||
253 | */ |
||
254 | public function getLayoutsPath(): string |
||
258 | |||
259 | /** |
||
260 | * Return the path of themes directory. |
||
261 | * |
||
262 | * @return string |
||
263 | */ |
||
264 | public function getThemesPath(): string |
||
268 | |||
269 | /** |
||
270 | * Return the path of internal templates directory. |
||
271 | * |
||
272 | * @return string |
||
273 | */ |
||
274 | public function getInternalLayoutsPath(): string |
||
278 | |||
279 | /** |
||
280 | * Return the path of the output directory. |
||
281 | * |
||
282 | * @return string |
||
283 | */ |
||
284 | public function getOutputPath(): string |
||
288 | |||
289 | /** |
||
290 | * Return the path of static files directory. |
||
291 | * |
||
292 | * @return string |
||
293 | */ |
||
294 | public function getStaticPath(): string |
||
298 | |||
299 | /** |
||
300 | * Return a "clean" array of an output format. |
||
301 | * |
||
302 | * @param string $format |
||
303 | * |
||
304 | * @return array |
||
305 | */ |
||
306 | public function getOutputFormat(string $format): array |
||
319 | |||
320 | /** |
||
321 | * Theme helpers. |
||
322 | */ |
||
323 | |||
324 | /** |
||
325 | * Return theme(s) as an array. |
||
326 | * |
||
327 | * @return array|null |
||
328 | */ |
||
329 | public function getTheme(): ?array |
||
341 | |||
342 | /** |
||
343 | * Has a (valid) theme(s)? |
||
344 | * |
||
345 | * @throws Exception |
||
346 | * |
||
347 | * @return bool |
||
348 | */ |
||
349 | public function hasTheme(): bool |
||
367 | |||
368 | /** |
||
369 | * Return the path of a specific theme's directory. |
||
370 | * ("layouts" by default). |
||
371 | * |
||
372 | * @param string $theme |
||
373 | * @param string $dir |
||
374 | * |
||
375 | * @return string |
||
376 | */ |
||
377 | public function getThemeDirPath(string $theme, string $dir = 'layouts'): string |
||
381 | |||
382 | /** |
||
383 | * Language helpers. |
||
384 | */ |
||
385 | |||
386 | /** |
||
387 | * Return an array of available languages. |
||
388 | * |
||
389 | * @return array |
||
390 | */ |
||
391 | public function getLanguages(): array |
||
395 | |||
396 | /** |
||
397 | * Return the default language key (ie: "en", "fr-fr", etc.). |
||
398 | * |
||
399 | * @return string |
||
400 | */ |
||
401 | public function getLanguageDefaultKey(): string |
||
415 | |||
416 | /** |
||
417 | * Return properties of a (specified or default) language. |
||
418 | * |
||
419 | * @param string|null $key |
||
420 | * |
||
421 | * @return array |
||
422 | */ |
||
423 | public function getLanguageProperties(string $key = null): array |
||
434 | |||
435 | /** |
||
436 | * Return the property value of a (specified or default) language. |
||
437 | * |
||
438 | * @param string $property |
||
439 | * @param string|null $key |
||
440 | * |
||
441 | * @return string |
||
442 | */ |
||
443 | public function getLanguageProperty($property, $key = null): string |
||
464 | } |
||
465 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.