Complex classes like ModuleFile 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 ModuleFile, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
30 | class ModuleFile |
||
31 | { |
||
32 | /** |
||
33 | * The current puli.json version. |
||
34 | */ |
||
35 | const DEFAULT_VERSION = '1.0'; |
||
36 | |||
37 | /** |
||
38 | * @var string |
||
39 | */ |
||
40 | private $version = self::DEFAULT_VERSION; |
||
41 | |||
42 | /** |
||
43 | * @var string|null |
||
44 | */ |
||
45 | private $moduleName; |
||
46 | |||
47 | /** |
||
48 | * @var string|null |
||
49 | */ |
||
50 | private $path; |
||
51 | |||
52 | /** |
||
53 | * @var PathMapping[] |
||
54 | */ |
||
55 | private $pathMappings = array(); |
||
56 | |||
57 | /** |
||
58 | * @var BindingDescriptor[] |
||
59 | */ |
||
60 | private $bindingDescriptors = array(); |
||
61 | |||
62 | /** |
||
63 | * @var BindingTypeDescriptor[] |
||
64 | */ |
||
65 | private $typeDescriptors = array(); |
||
66 | |||
67 | /** |
||
68 | * @var bool[] |
||
69 | */ |
||
70 | private $overriddenModules = array(); |
||
71 | |||
72 | /** |
||
73 | * @var array |
||
74 | */ |
||
75 | private $extra = array(); |
||
76 | |||
77 | /** |
||
78 | * Creates a new module file. |
||
79 | * |
||
80 | * @param string|null $moduleName The module name. Optional. |
||
81 | * @param string|null $path The path where the file is stored or |
||
82 | * `null` if this configuration is not |
||
83 | * stored on the file system. |
||
84 | * |
||
85 | * @throws InvalidArgumentException If the name/path is not a string or empty. |
||
86 | */ |
||
87 | 626 | public function __construct($moduleName = null, $path = null) |
|
95 | |||
96 | /** |
||
97 | * Returns the version of the module file. |
||
98 | * |
||
99 | * @return string The module file version. |
||
100 | */ |
||
101 | 9 | public function getVersion() |
|
105 | |||
106 | /** |
||
107 | * Sets the version of the module file. |
||
108 | * |
||
109 | * @param string $version The module file version. |
||
110 | */ |
||
111 | 7 | public function setVersion($version) |
|
118 | |||
119 | /** |
||
120 | * Returns the module name. |
||
121 | * |
||
122 | * @return string|null The module name or `null` if none is set. |
||
123 | */ |
||
124 | 453 | public function getModuleName() |
|
128 | |||
129 | /** |
||
130 | * Sets the module name. |
||
131 | * |
||
132 | * @param string|null $moduleName The module name or `null` to unset. |
||
133 | * |
||
134 | * @throws InvalidArgumentException If the name is not a string or empty. |
||
135 | */ |
||
136 | 42 | public function setModuleName($moduleName) |
|
142 | |||
143 | /** |
||
144 | * Returns the path to the module file. |
||
145 | * |
||
146 | * @return string|null The path or `null` if this file is not stored on the |
||
147 | * file system. |
||
148 | */ |
||
149 | 25 | public function getPath() |
|
153 | |||
154 | /** |
||
155 | * Sets the names of the modules this module overrides. |
||
156 | * |
||
157 | * @param string[] $moduleNames The names of the overridden modules. |
||
158 | */ |
||
159 | 52 | public function setOverriddenModules(array $moduleNames) |
|
167 | |||
168 | /** |
||
169 | * Adds an overridden module. |
||
170 | * |
||
171 | * @param string $moduleName The name of the overridden module. |
||
172 | */ |
||
173 | 10 | public function addOverriddenModule($moduleName) |
|
177 | |||
178 | /** |
||
179 | * Adds an overridden module. |
||
180 | * |
||
181 | * @param string $moduleName The name of the overridden module. |
||
182 | */ |
||
183 | 2 | public function removeOverriddenModule($moduleName) |
|
187 | |||
188 | /** |
||
189 | * Removes all overridden modules. |
||
190 | */ |
||
191 | public function clearOverriddenModules() |
||
195 | |||
196 | /** |
||
197 | * Returns the names of the modules this module overrides. |
||
198 | * |
||
199 | * @return string[] The names of the overridden modules. |
||
|
|||
200 | */ |
||
201 | 120 | public function getOverriddenModules() |
|
205 | |||
206 | /** |
||
207 | * Returns whether the module overrides a given module. |
||
208 | * |
||
209 | * @param string $moduleName The name of the overridden module. |
||
210 | * |
||
211 | * @return bool Returns `true` if the module is overridden in the module |
||
212 | * file. |
||
213 | */ |
||
214 | 7 | public function hasOverriddenModule($moduleName) |
|
218 | |||
219 | /** |
||
220 | * Returns whether the module overrides any other module. |
||
221 | * |
||
222 | * @return bool Returns `true` if the module overrides other packgaes and |
||
223 | * `false` otherwise. |
||
224 | */ |
||
225 | public function hasOverriddenModules() |
||
229 | |||
230 | /** |
||
231 | * Returns the path mappings. |
||
232 | * |
||
233 | * @return PathMapping[] The path mappings. |
||
234 | */ |
||
235 | 89 | public function getPathMappings() |
|
239 | |||
240 | /** |
||
241 | * Returns the path mapping for a repository path. |
||
242 | * |
||
243 | * @param string $repositoryPath The repository path. |
||
244 | * |
||
245 | * @return PathMapping The corresponding path mapping. |
||
246 | * |
||
247 | * @throws NoSuchPathMappingException If the repository path is not mapped. |
||
248 | */ |
||
249 | 15 | public function getPathMapping($repositoryPath) |
|
257 | |||
258 | /** |
||
259 | * Returns whether the file contains a path mapping for a repository path. |
||
260 | * |
||
261 | * @param string $repositoryPath The repository path. |
||
262 | * |
||
263 | * @return bool Returns `true` if the file contains a mapping for the path. |
||
264 | */ |
||
265 | 30 | public function hasPathMapping($repositoryPath) |
|
269 | |||
270 | /** |
||
271 | * Returns whether the file contains any path mappings. |
||
272 | * |
||
273 | * @return bool Returns `true` if the file contains path mappings and |
||
274 | * `false` otherwise. |
||
275 | */ |
||
276 | 1 | public function hasPathMappings() |
|
280 | |||
281 | /** |
||
282 | * Adds a path mapping. |
||
283 | * |
||
284 | * @param PathMapping $mapping The path mapping. |
||
285 | */ |
||
286 | 67 | public function addPathMapping(PathMapping $mapping) |
|
292 | |||
293 | /** |
||
294 | * Removes the path mapping for a repository path. |
||
295 | * |
||
296 | * @param string $repositoryPath The repository path. |
||
297 | */ |
||
298 | 16 | public function removePathMapping($repositoryPath) |
|
302 | |||
303 | /** |
||
304 | * Removes all path mappings. |
||
305 | */ |
||
306 | public function clearPathMappings() |
||
310 | |||
311 | /** |
||
312 | * Returns the binding descriptors. |
||
313 | * |
||
314 | * @return BindingDescriptor[] The binding descriptors. |
||
315 | */ |
||
316 | 123 | public function getBindingDescriptors() |
|
320 | |||
321 | /** |
||
322 | * Returns the binding descriptor with the given UUID. |
||
323 | * |
||
324 | * @param Uuid $uuid The UUID of the binding descriptor. |
||
325 | * |
||
326 | * @return BindingDescriptor The binding descriptor. |
||
327 | * |
||
328 | * @throws NoSuchBindingException If the UUID was not found. |
||
329 | */ |
||
330 | 10 | public function getBindingDescriptor(Uuid $uuid) |
|
340 | |||
341 | /** |
||
342 | * Adds a binding descriptor. |
||
343 | * |
||
344 | * @param BindingDescriptor $descriptor The binding descriptor to add. |
||
345 | */ |
||
346 | 66 | public function addBindingDescriptor(BindingDescriptor $descriptor) |
|
350 | |||
351 | /** |
||
352 | * Removes a binding descriptor. |
||
353 | * |
||
354 | * @param Uuid $uuid The UUID of the binding descriptor to remove. |
||
355 | */ |
||
356 | 9 | public function removeBindingDescriptor(Uuid $uuid) |
|
360 | |||
361 | /** |
||
362 | * Removes all binding descriptors. |
||
363 | */ |
||
364 | public function clearBindingDescriptors() |
||
368 | |||
369 | /** |
||
370 | * Returns whether the binding descriptor exists in this file. |
||
371 | * |
||
372 | * @param Uuid $uuid The UUID of the binding descriptor. |
||
373 | * |
||
374 | * @return bool Whether the file contains the binding descriptor. |
||
375 | */ |
||
376 | 13 | public function hasBindingDescriptor(Uuid $uuid) |
|
380 | |||
381 | /** |
||
382 | * Returns whether the file contains any binding descriptors. |
||
383 | * |
||
384 | * @return bool Returns `true` if the file contains binding descriptors and |
||
385 | * `false` otherwise. |
||
386 | */ |
||
387 | 1 | public function hasBindingDescriptors() |
|
391 | |||
392 | /** |
||
393 | * Adds a type descriptor. |
||
394 | * |
||
395 | * @param BindingTypeDescriptor $descriptor The type descriptor. |
||
396 | */ |
||
397 | 85 | public function addTypeDescriptor(BindingTypeDescriptor $descriptor) |
|
401 | |||
402 | /** |
||
403 | * Removes a type descriptor. |
||
404 | * |
||
405 | * @param string $typeName The type name. |
||
406 | */ |
||
407 | 14 | public function removeTypeDescriptor($typeName) |
|
411 | |||
412 | /** |
||
413 | * Removes all type descriptors. |
||
414 | */ |
||
415 | public function clearTypeDescriptors() |
||
419 | |||
420 | /** |
||
421 | * Returns the type descriptor with the given name. |
||
422 | * |
||
423 | * @param string $typeName The type name. |
||
424 | * |
||
425 | * @return BindingTypeDescriptor The type descriptor. |
||
426 | */ |
||
427 | 13 | public function getTypeDescriptor($typeName) |
|
431 | |||
432 | /** |
||
433 | * Returns the type descriptors. |
||
434 | * |
||
435 | * @return BindingTypeDescriptor[] The type descriptors. |
||
436 | */ |
||
437 | 116 | public function getTypeDescriptors() |
|
442 | |||
443 | /** |
||
444 | * Returns whether a type is defined in this file. |
||
445 | * |
||
446 | * @param string $typeName The type name. |
||
447 | * |
||
448 | * @return bool Whether the type is defined in the file. |
||
449 | */ |
||
450 | 17 | public function hasTypeDescriptor($typeName) |
|
454 | |||
455 | /** |
||
456 | * Returns whether the file contains any type descriptors. |
||
457 | * |
||
458 | * @return bool Returns `true` if the file contains type descriptors and |
||
459 | * `false` otherwise. |
||
460 | */ |
||
461 | 2 | public function hasTypeDescriptors() |
|
465 | |||
466 | /** |
||
467 | * Sets an extra key in the module file. |
||
468 | * |
||
469 | * Extra keys can be freely set by the user. They are stored in a separate |
||
470 | * area of the module file and not validated in any way. |
||
471 | * |
||
472 | * @param string $key The name of the key. |
||
473 | * @param mixed $value The value to store. |
||
474 | */ |
||
475 | 85 | public function setExtraKey($key, $value) |
|
479 | |||
480 | /** |
||
481 | * Sets multiple extra keys at once. |
||
482 | * |
||
483 | * Existing extra keys are overridden. |
||
484 | * |
||
485 | * @param array $values The values indexed by their key names. |
||
486 | * |
||
487 | * @see setExtraKey() |
||
488 | */ |
||
489 | 9 | public function setExtraKeys(array $values) |
|
497 | |||
498 | /** |
||
499 | * Sets multiple extra keys at once. |
||
500 | * |
||
501 | * Existing extra keys are preserved. |
||
502 | * |
||
503 | * @param array $values The values indexed by their key names. |
||
504 | * |
||
505 | * @see setExtraKey() |
||
506 | */ |
||
507 | 1 | public function addExtraKeys(array $values) |
|
513 | |||
514 | /** |
||
515 | * Removes an extra key. |
||
516 | * |
||
517 | * @param string $key The name of the key. |
||
518 | * |
||
519 | * @see setExtraKey() |
||
520 | */ |
||
521 | 7 | public function removeExtraKey($key) |
|
525 | |||
526 | /** |
||
527 | * Removes all extra keys. |
||
528 | * |
||
529 | * @see setExtraKey() |
||
530 | */ |
||
531 | 4 | public function clearExtraKeys() |
|
535 | |||
536 | /** |
||
537 | * Returns the value of an extra key. |
||
538 | * |
||
539 | * @param string $key The name of the key. |
||
540 | * @param mixed $default The value to return if the key was not set. |
||
541 | * |
||
542 | * @return mixed The value stored for the key. |
||
543 | * |
||
544 | * @see setExtraKey() |
||
545 | */ |
||
546 | 96 | public function getExtraKey($key, $default = null) |
|
550 | |||
551 | /** |
||
552 | * Returns all stored extra keys. |
||
553 | * |
||
554 | * @return array The stored values indexed by their key names. |
||
555 | * |
||
556 | * @see setExtraKey() |
||
557 | */ |
||
558 | 38 | public function getExtraKeys() |
|
562 | |||
563 | /** |
||
564 | * Returns whether the given extra key exists. |
||
565 | * |
||
566 | * @param string $key The name of the key. |
||
567 | * |
||
568 | * @return bool Returns `true` if the given extra key exists and `false` |
||
569 | * otherwise. |
||
570 | * |
||
571 | * @see setExtraKey() |
||
572 | */ |
||
573 | 14 | public function hasExtraKey($key) |
|
577 | |||
578 | /** |
||
579 | * Returns whether the file contains any extra keys. |
||
580 | * |
||
581 | * @return bool Returns `true` if the file contains extra keys and `false` |
||
582 | * otherwise. |
||
583 | * |
||
584 | * @see setExtraKey() |
||
585 | */ |
||
586 | 2 | public function hasExtraKeys() |
|
590 | } |
||
591 |
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.