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 | * @var string |
||
| 34 | */ |
||
| 35 | private $version; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var string|null |
||
| 39 | */ |
||
| 40 | private $moduleName; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var string|null |
||
| 44 | */ |
||
| 45 | private $path; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var PathMapping[] |
||
| 49 | */ |
||
| 50 | private $pathMappings = array(); |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var BindingDescriptor[] |
||
| 54 | */ |
||
| 55 | private $bindingDescriptors = array(); |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var BindingTypeDescriptor[] |
||
| 59 | */ |
||
| 60 | private $typeDescriptors = array(); |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var bool[] |
||
| 64 | */ |
||
| 65 | private $dependencies = array(); |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var array |
||
| 69 | */ |
||
| 70 | private $extra = array(); |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Creates a new module file. |
||
| 74 | * |
||
| 75 | * @param string|null $moduleName The module name. Optional. |
||
| 76 | * @param string|null $path The path where the file is stored or |
||
| 77 | * `null` if this configuration is not |
||
| 78 | * stored on the file system. |
||
| 79 | * |
||
| 80 | * @throws InvalidArgumentException If the name/path is not a string or empty. |
||
| 81 | */ |
||
| 82 | 626 | public function __construct($moduleName = null, $path = null) |
|
| 90 | |||
| 91 | /** |
||
| 92 | * Returns the version of the module file. |
||
| 93 | * |
||
| 94 | * @return string The module file version. |
||
| 95 | */ |
||
| 96 | 13 | public function getVersion() |
|
| 100 | |||
| 101 | /** |
||
| 102 | * Sets the version of the module file. |
||
| 103 | * |
||
| 104 | * @param string $version The module file version. |
||
| 105 | */ |
||
| 106 | 47 | public function setVersion($version) |
|
| 113 | |||
| 114 | /** |
||
| 115 | * Returns the module name. |
||
| 116 | * |
||
| 117 | * @return string|null The module name or `null` if none is set. |
||
| 118 | */ |
||
| 119 | 453 | public function getModuleName() |
|
| 123 | |||
| 124 | /** |
||
| 125 | * Sets the module name. |
||
| 126 | * |
||
| 127 | * @param string|null $moduleName The module name or `null` to unset. |
||
| 128 | * |
||
| 129 | * @throws InvalidArgumentException If the name is not a string or empty. |
||
| 130 | */ |
||
| 131 | 42 | public function setModuleName($moduleName) |
|
| 137 | |||
| 138 | /** |
||
| 139 | * Returns the path to the module file. |
||
| 140 | * |
||
| 141 | * @return string|null The path or `null` if this file is not stored on the |
||
| 142 | * file system. |
||
| 143 | */ |
||
| 144 | 25 | public function getPath() |
|
| 148 | |||
| 149 | /** |
||
| 150 | * Sets the names of the modules this module depends on. |
||
| 151 | * |
||
| 152 | * @param string[] $moduleNames The names of the modules. |
||
| 153 | */ |
||
| 154 | 52 | public function setDependencies(array $moduleNames) |
|
| 162 | |||
| 163 | /** |
||
| 164 | * Adds a dependency to a module. |
||
| 165 | * |
||
| 166 | * @param string $moduleName The name of the module. |
||
| 167 | */ |
||
| 168 | 10 | public function addDependency($moduleName) |
|
| 172 | |||
| 173 | /** |
||
| 174 | * Adds a dependency to a module. |
||
| 175 | * |
||
| 176 | * @param string $moduleName The name of the module. |
||
| 177 | */ |
||
| 178 | 2 | public function removeDependency($moduleName) |
|
| 182 | |||
| 183 | /** |
||
| 184 | * Removes all dependencies. |
||
| 185 | */ |
||
| 186 | public function clearDependencies() |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Returns the names of the modules this module overrides. |
||
| 193 | * |
||
| 194 | * @return string[] The names of the overridden modules. |
||
|
|
|||
| 195 | */ |
||
| 196 | 120 | public function getDependencies() |
|
| 200 | |||
| 201 | /** |
||
| 202 | * Returns whether the module depends on a given module. |
||
| 203 | * |
||
| 204 | * @param string $moduleName The name of the module. |
||
| 205 | * |
||
| 206 | * @return bool Returns `true` if the module depends on the given module. |
||
| 207 | */ |
||
| 208 | 7 | public function hasDependency($moduleName) |
|
| 212 | |||
| 213 | /** |
||
| 214 | * Returns whether the module has dependencies on other modules. |
||
| 215 | * |
||
| 216 | * @return bool Returns `true` if the module depends on other modules and |
||
| 217 | * `false` otherwise. |
||
| 218 | */ |
||
| 219 | public function hasDependencies() |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Returns the path mappings. |
||
| 226 | * |
||
| 227 | * @return PathMapping[] The path mappings. |
||
| 228 | */ |
||
| 229 | 89 | public function getPathMappings() |
|
| 233 | |||
| 234 | /** |
||
| 235 | * Returns the path mapping for a repository path. |
||
| 236 | * |
||
| 237 | * @param string $repositoryPath The repository path. |
||
| 238 | * |
||
| 239 | * @return PathMapping The corresponding path mapping. |
||
| 240 | * |
||
| 241 | * @throws NoSuchPathMappingException If the repository path is not mapped. |
||
| 242 | */ |
||
| 243 | 15 | public function getPathMapping($repositoryPath) |
|
| 251 | |||
| 252 | /** |
||
| 253 | * Returns whether the file contains a path mapping for a repository path. |
||
| 254 | * |
||
| 255 | * @param string $repositoryPath The repository path. |
||
| 256 | * |
||
| 257 | * @return bool Returns `true` if the file contains a mapping for the path. |
||
| 258 | */ |
||
| 259 | 30 | public function hasPathMapping($repositoryPath) |
|
| 263 | |||
| 264 | /** |
||
| 265 | * Returns whether the file contains any path mappings. |
||
| 266 | * |
||
| 267 | * @return bool Returns `true` if the file contains path mappings and |
||
| 268 | * `false` otherwise. |
||
| 269 | */ |
||
| 270 | 1 | public function hasPathMappings() |
|
| 274 | |||
| 275 | /** |
||
| 276 | * Adds a path mapping. |
||
| 277 | * |
||
| 278 | * @param PathMapping $mapping The path mapping. |
||
| 279 | */ |
||
| 280 | 67 | public function addPathMapping(PathMapping $mapping) |
|
| 286 | |||
| 287 | /** |
||
| 288 | * Removes the path mapping for a repository path. |
||
| 289 | * |
||
| 290 | * @param string $repositoryPath The repository path. |
||
| 291 | */ |
||
| 292 | 16 | public function removePathMapping($repositoryPath) |
|
| 296 | |||
| 297 | /** |
||
| 298 | * Removes all path mappings. |
||
| 299 | */ |
||
| 300 | public function clearPathMappings() |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Returns the binding descriptors. |
||
| 307 | * |
||
| 308 | * @return BindingDescriptor[] The binding descriptors. |
||
| 309 | */ |
||
| 310 | 123 | public function getBindingDescriptors() |
|
| 314 | |||
| 315 | /** |
||
| 316 | * Returns the binding descriptor with the given UUID. |
||
| 317 | * |
||
| 318 | * @param Uuid $uuid The UUID of the binding descriptor. |
||
| 319 | * |
||
| 320 | * @return BindingDescriptor The binding descriptor. |
||
| 321 | * |
||
| 322 | * @throws NoSuchBindingException If the UUID was not found. |
||
| 323 | */ |
||
| 324 | 10 | public function getBindingDescriptor(Uuid $uuid) |
|
| 334 | |||
| 335 | /** |
||
| 336 | * Adds a binding descriptor. |
||
| 337 | * |
||
| 338 | * @param BindingDescriptor $descriptor The binding descriptor to add. |
||
| 339 | */ |
||
| 340 | 66 | public function addBindingDescriptor(BindingDescriptor $descriptor) |
|
| 344 | |||
| 345 | /** |
||
| 346 | * Removes a binding descriptor. |
||
| 347 | * |
||
| 348 | * @param Uuid $uuid The UUID of the binding descriptor to remove. |
||
| 349 | */ |
||
| 350 | 9 | public function removeBindingDescriptor(Uuid $uuid) |
|
| 354 | |||
| 355 | /** |
||
| 356 | * Removes all binding descriptors. |
||
| 357 | */ |
||
| 358 | public function clearBindingDescriptors() |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Returns whether the binding descriptor exists in this file. |
||
| 365 | * |
||
| 366 | * @param Uuid $uuid The UUID of the binding descriptor. |
||
| 367 | * |
||
| 368 | * @return bool Whether the file contains the binding descriptor. |
||
| 369 | */ |
||
| 370 | 13 | public function hasBindingDescriptor(Uuid $uuid) |
|
| 374 | |||
| 375 | /** |
||
| 376 | * Returns whether the file contains any binding descriptors. |
||
| 377 | * |
||
| 378 | * @return bool Returns `true` if the file contains binding descriptors and |
||
| 379 | * `false` otherwise. |
||
| 380 | */ |
||
| 381 | 1 | public function hasBindingDescriptors() |
|
| 385 | |||
| 386 | /** |
||
| 387 | * Adds a type descriptor. |
||
| 388 | * |
||
| 389 | * @param BindingTypeDescriptor $descriptor The type descriptor. |
||
| 390 | */ |
||
| 391 | 85 | public function addTypeDescriptor(BindingTypeDescriptor $descriptor) |
|
| 395 | |||
| 396 | /** |
||
| 397 | * Removes a type descriptor. |
||
| 398 | * |
||
| 399 | * @param string $typeName The type name. |
||
| 400 | */ |
||
| 401 | 14 | public function removeTypeDescriptor($typeName) |
|
| 405 | |||
| 406 | /** |
||
| 407 | * Removes all type descriptors. |
||
| 408 | */ |
||
| 409 | public function clearTypeDescriptors() |
||
| 413 | |||
| 414 | /** |
||
| 415 | * Returns the type descriptor with the given name. |
||
| 416 | * |
||
| 417 | * @param string $typeName The type name. |
||
| 418 | * |
||
| 419 | * @return BindingTypeDescriptor The type descriptor. |
||
| 420 | */ |
||
| 421 | 13 | public function getTypeDescriptor($typeName) |
|
| 425 | |||
| 426 | /** |
||
| 427 | * Returns the type descriptors. |
||
| 428 | * |
||
| 429 | * @return BindingTypeDescriptor[] The type descriptors. |
||
| 430 | */ |
||
| 431 | 116 | public function getTypeDescriptors() |
|
| 436 | |||
| 437 | /** |
||
| 438 | * Returns whether a type is defined in this file. |
||
| 439 | * |
||
| 440 | * @param string $typeName The type name. |
||
| 441 | * |
||
| 442 | * @return bool Whether the type is defined in the file. |
||
| 443 | */ |
||
| 444 | 17 | public function hasTypeDescriptor($typeName) |
|
| 448 | |||
| 449 | /** |
||
| 450 | * Returns whether the file contains any type descriptors. |
||
| 451 | * |
||
| 452 | * @return bool Returns `true` if the file contains type descriptors and |
||
| 453 | * `false` otherwise. |
||
| 454 | */ |
||
| 455 | 2 | public function hasTypeDescriptors() |
|
| 459 | |||
| 460 | /** |
||
| 461 | * Sets an extra key in the module file. |
||
| 462 | * |
||
| 463 | * Extra keys can be freely set by the user. They are stored in a separate |
||
| 464 | * area of the module file and not validated in any way. |
||
| 465 | * |
||
| 466 | * @param string $key The name of the key. |
||
| 467 | * @param mixed $value The value to store. |
||
| 468 | */ |
||
| 469 | 85 | public function setExtraKey($key, $value) |
|
| 473 | |||
| 474 | /** |
||
| 475 | * Sets multiple extra keys at once. |
||
| 476 | * |
||
| 477 | * Existing extra keys are overridden. |
||
| 478 | * |
||
| 479 | * @param array $values The values indexed by their key names. |
||
| 480 | * |
||
| 481 | * @see setExtraKey() |
||
| 482 | */ |
||
| 483 | 9 | public function setExtraKeys(array $values) |
|
| 491 | |||
| 492 | /** |
||
| 493 | * Sets multiple extra keys at once. |
||
| 494 | * |
||
| 495 | * Existing extra keys are preserved. |
||
| 496 | * |
||
| 497 | * @param array $values The values indexed by their key names. |
||
| 498 | * |
||
| 499 | * @see setExtraKey() |
||
| 500 | */ |
||
| 501 | 1 | public function addExtraKeys(array $values) |
|
| 507 | |||
| 508 | /** |
||
| 509 | * Removes an extra key. |
||
| 510 | * |
||
| 511 | * @param string $key The name of the key. |
||
| 512 | * |
||
| 513 | * @see setExtraKey() |
||
| 514 | */ |
||
| 515 | 7 | public function removeExtraKey($key) |
|
| 519 | |||
| 520 | /** |
||
| 521 | * Removes all extra keys. |
||
| 522 | * |
||
| 523 | * @see setExtraKey() |
||
| 524 | */ |
||
| 525 | 4 | public function clearExtraKeys() |
|
| 529 | |||
| 530 | /** |
||
| 531 | * Returns the value of an extra key. |
||
| 532 | * |
||
| 533 | * @param string $key The name of the key. |
||
| 534 | * @param mixed $default The value to return if the key was not set. |
||
| 535 | * |
||
| 536 | * @return mixed The value stored for the key. |
||
| 537 | * |
||
| 538 | * @see setExtraKey() |
||
| 539 | */ |
||
| 540 | 96 | public function getExtraKey($key, $default = null) |
|
| 544 | |||
| 545 | /** |
||
| 546 | * Returns all stored extra keys. |
||
| 547 | * |
||
| 548 | * @return array The stored values indexed by their key names. |
||
| 549 | * |
||
| 550 | * @see setExtraKey() |
||
| 551 | */ |
||
| 552 | 38 | public function getExtraKeys() |
|
| 556 | |||
| 557 | /** |
||
| 558 | * Returns whether the given extra key exists. |
||
| 559 | * |
||
| 560 | * @param string $key The name of the key. |
||
| 561 | * |
||
| 562 | * @return bool Returns `true` if the given extra key exists and `false` |
||
| 563 | * otherwise. |
||
| 564 | * |
||
| 565 | * @see setExtraKey() |
||
| 566 | */ |
||
| 567 | 14 | public function hasExtraKey($key) |
|
| 571 | |||
| 572 | /** |
||
| 573 | * Returns whether the file contains any extra keys. |
||
| 574 | * |
||
| 575 | * @return bool Returns `true` if the file contains extra keys and `false` |
||
| 576 | * otherwise. |
||
| 577 | * |
||
| 578 | * @see setExtraKey() |
||
| 579 | */ |
||
| 580 | 2 | public function hasExtraKeys() |
|
| 584 | } |
||
| 585 |
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.