Complex classes like DescriptorAbstract 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 DescriptorAbstract, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 23 | abstract class DescriptorAbstract implements Descriptor, Filterable |
||
| 24 | { |
||
| 25 | /** |
||
| 26 | * @var string Fully Qualified Structural Element Name; the FQCN including method, property of constant name |
||
| 27 | */ |
||
| 28 | protected $fqsen = ''; |
||
| 29 | |||
| 30 | /** @var string $name The local name for this element */ |
||
| 31 | protected $name = ''; |
||
| 32 | |||
| 33 | /** @var NamespaceDescriptor|string $namespace The namespace for this element */ |
||
| 34 | protected $namespace = ''; |
||
| 35 | |||
| 36 | /** @var PackageDescriptor $package The package with which this element is associated */ |
||
| 37 | protected $package; |
||
| 38 | |||
| 39 | /** @var string $summary A summary describing the function of this element in short. */ |
||
| 40 | protected $summary = ''; |
||
| 41 | |||
| 42 | /** @var string $description A more extensive description of this element. */ |
||
| 43 | protected $description = ''; |
||
| 44 | |||
| 45 | /** @var FileDescriptor|null $file The file to which this element belongs; if applicable */ |
||
| 46 | protected $fileDescriptor; |
||
| 47 | |||
| 48 | /** @var int $line The line number on which this element occurs. */ |
||
| 49 | protected $line = 0; |
||
| 50 | |||
| 51 | /** @var Collection $tags The tags associated with this element. */ |
||
| 52 | protected $tags; |
||
| 53 | |||
| 54 | /** @var Collection $errors A list of errors found while building this element. */ |
||
| 55 | protected $errors; |
||
| 56 | |||
| 57 | /** @var DescriptorAbstract|null the element from which to inherit information in this element */ |
||
| 58 | protected $inheritedElement = null; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Initializes this descriptor. |
||
| 62 | */ |
||
| 63 | 2 | public function __construct() |
|
| 68 | |||
| 69 | /** |
||
| 70 | * Sets the Fully Qualified Structural Element Name (FQSEN) for this element. |
||
| 71 | * |
||
| 72 | * @param string $name |
||
| 73 | */ |
||
| 74 | 2 | public function setFullyQualifiedStructuralElementName($name) |
|
| 78 | |||
| 79 | /** |
||
| 80 | * Returns the Fully Qualified Structural Element Name (FQSEN) for this element. |
||
| 81 | * |
||
| 82 | * @return string |
||
| 83 | */ |
||
| 84 | 1 | public function getFullyQualifiedStructuralElementName() |
|
| 88 | |||
| 89 | /** |
||
| 90 | * Sets the local name for this element. |
||
| 91 | * |
||
| 92 | * @param string $name |
||
| 93 | */ |
||
| 94 | 2 | public function setName($name) |
|
| 98 | |||
| 99 | /** |
||
| 100 | * Returns the local name for this element. |
||
| 101 | * |
||
| 102 | * @return string |
||
| 103 | */ |
||
| 104 | 1 | public function getName() |
|
| 108 | |||
| 109 | /** |
||
| 110 | * Sets the namespace (name) for this element. |
||
| 111 | * |
||
| 112 | * @param NamespaceDescriptor|string $namespace |
||
| 113 | */ |
||
| 114 | 1 | public function setNamespace($namespace) |
|
| 118 | |||
| 119 | /** |
||
| 120 | * Returns the namespace for this element (defaults to global "\") |
||
| 121 | * |
||
| 122 | * @return NamespaceDescriptor|string |
||
| 123 | */ |
||
| 124 | 1 | public function getNamespace() |
|
| 128 | |||
| 129 | /** |
||
| 130 | * Sets the summary describing this element in short. |
||
| 131 | * |
||
| 132 | * @param string $summary |
||
| 133 | */ |
||
| 134 | 1 | public function setSummary($summary) |
|
| 138 | |||
| 139 | /** |
||
| 140 | * Returns the summary which describes this element. |
||
| 141 | * |
||
| 142 | * This method will automatically attempt to inherit the parent's summary if this one has none. |
||
| 143 | * |
||
| 144 | * @return string |
||
| 145 | */ |
||
| 146 | 7 | public function getSummary() |
|
| 159 | |||
| 160 | /** |
||
| 161 | * Sets a description for this element. |
||
| 162 | * |
||
| 163 | * @param string $description |
||
| 164 | */ |
||
| 165 | 1 | public function setDescription($description) |
|
| 169 | |||
| 170 | /** |
||
| 171 | * Returns the description for this element. |
||
| 172 | * |
||
| 173 | * This method will automatically attempt to inherit the parent's description if this one has none. |
||
| 174 | * |
||
| 175 | * @return string |
||
| 176 | */ |
||
| 177 | 14 | public function getDescription() |
|
| 194 | |||
| 195 | /** |
||
| 196 | * Sets the file and linenumber where this element is at. |
||
| 197 | * |
||
| 198 | * @param int $line |
||
| 199 | */ |
||
| 200 | 1 | public function setLocation(FileDescriptor $file, $line = 0) |
|
| 205 | |||
| 206 | /** |
||
| 207 | * Returns the path to the file containing this element relative to the project's root. |
||
| 208 | * |
||
| 209 | * @return string |
||
| 210 | */ |
||
| 211 | 1 | public function getPath() |
|
| 215 | |||
| 216 | /** |
||
| 217 | * Returns the file in which this element resides or null in case the element is not bound to a file.. |
||
| 218 | * |
||
| 219 | * @return FileDescriptor|null |
||
| 220 | */ |
||
| 221 | 1 | public function getFile() |
|
| 225 | |||
| 226 | /** |
||
| 227 | * Sets the file to which this element is associated. |
||
| 228 | * |
||
| 229 | * @return bool |
||
| 230 | */ |
||
| 231 | public function setFile(FileDescriptor $file) |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Returns the line number where the definition for this element can be found. |
||
| 238 | * |
||
| 239 | * @return int |
||
| 240 | */ |
||
| 241 | 2 | public function getLine() |
|
| 245 | |||
| 246 | /** |
||
| 247 | * Sets the line number for this element's location in the source file. |
||
| 248 | * |
||
| 249 | * @param integer $lineNumber |
||
| 250 | */ |
||
| 251 | 1 | public function setLine($lineNumber) |
|
| 255 | |||
| 256 | /** |
||
| 257 | * Sets the tags associated with this element. |
||
| 258 | */ |
||
| 259 | 2 | public function setTags(Collection $tags) |
|
| 263 | |||
| 264 | /** |
||
| 265 | * Returns the tags associated with this element. |
||
| 266 | * |
||
| 267 | * @return Collection |
||
| 268 | */ |
||
| 269 | 1 | public function getTags() |
|
| 273 | |||
| 274 | /** |
||
| 275 | * Sets the name of the package to which this element belongs. |
||
| 276 | * |
||
| 277 | * @param PackageDescriptor $package |
||
| 278 | */ |
||
| 279 | 1 | public function setPackage($package) |
|
| 283 | |||
| 284 | /** |
||
| 285 | * Returns the package name for this element. |
||
| 286 | * |
||
| 287 | * @return PackageDescriptor|null |
||
| 288 | */ |
||
| 289 | 2 | public function getPackage() |
|
| 303 | |||
| 304 | /** |
||
| 305 | * @return Collection |
||
| 306 | */ |
||
| 307 | 5 | public function getAuthor() |
|
| 322 | |||
| 323 | /** |
||
| 324 | * Returns the versions for this element. |
||
| 325 | * |
||
| 326 | * @return Collection |
||
| 327 | */ |
||
| 328 | 5 | public function getVersion() |
|
| 343 | |||
| 344 | /** |
||
| 345 | * Returns the copyrights for this element. |
||
| 346 | * |
||
| 347 | * @return Collection |
||
| 348 | */ |
||
| 349 | 5 | public function getCopyright() |
|
| 364 | |||
| 365 | /** |
||
| 366 | * Checks whether this element is deprecated. |
||
| 367 | * |
||
| 368 | * @return boolean |
||
| 369 | */ |
||
| 370 | 1 | public function isDeprecated() |
|
| 374 | |||
| 375 | /** |
||
| 376 | * Sets a list of all errors associated with this element. |
||
| 377 | */ |
||
| 378 | 2 | public function setErrors(Collection $errors): void |
|
| 382 | |||
| 383 | /** |
||
| 384 | * Returns all errors that occur in this element. |
||
| 385 | * |
||
| 386 | * @return Collection |
||
| 387 | */ |
||
| 388 | 1 | public function getErrors() |
|
| 392 | |||
| 393 | /** |
||
| 394 | * Dynamically constructs a set of getters to retrieve tag (collections) with. |
||
| 395 | * |
||
| 396 | * Important: __call() is not a fast method of access; it is preferred to directly use the getTags() collection. |
||
| 397 | * This interface is provided to allow for uniform and easy access to certain tags. |
||
| 398 | * |
||
| 399 | * @param string $name |
||
| 400 | * @param mixed[] $arguments |
||
| 401 | * |
||
| 402 | * @return Collection|null |
||
| 403 | */ |
||
| 404 | 1 | public function __call($name, $arguments) |
|
| 415 | |||
| 416 | /** |
||
| 417 | * Represents this object by its unique identifier, the Fully Qualified Structural Element Name. |
||
| 418 | * |
||
| 419 | * @return string |
||
| 420 | */ |
||
| 421 | 1 | public function __toString(): string |
|
| 425 | |||
| 426 | /** |
||
| 427 | * @return DescriptorAbstract|null |
||
| 428 | */ |
||
| 429 | public function getInheritedElement() |
||
| 433 | } |
||
| 434 |