Complex classes like AbstractPhpStruct 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 AbstractPhpStruct, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 35 | abstract class AbstractPhpStruct extends AbstractModel implements NamespaceInterface, DocblockInterface { |
||
| 36 | |||
| 37 | use DocblockPart; |
||
| 38 | use LongDescriptionPart; |
||
| 39 | use QualifiedNamePart; |
||
| 40 | |||
| 41 | /** @var Map|string[] */ |
||
| 42 | private $useStatements; |
||
| 43 | |||
| 44 | /** @var Set|string[] */ |
||
| 45 | private $requiredFiles; |
||
| 46 | |||
| 47 | /** @var Map|PhpMethod[] */ |
||
| 48 | private $methods; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Creates a new struct |
||
| 52 | * |
||
| 53 | * @param string $name the fqcn |
||
| 54 | * @return static |
||
| 55 | */ |
||
| 56 | 13 | public static function create(?string $name = null) { |
|
| 59 | |||
| 60 | public static function fromName(string $name): self |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Creates a new struct |
||
| 69 | * |
||
| 70 | * @param string $name the fqcn |
||
| 71 | */ |
||
| 72 | 58 | public function __construct(?string $name = null) { |
|
| 79 | |||
| 80 | /** |
||
| 81 | * Sets requried files |
||
| 82 | * |
||
| 83 | * @param array $files |
||
| 84 | * @return $this |
||
| 85 | */ |
||
| 86 | 1 | public function setRequiredFiles(array $files) { |
|
| 91 | |||
| 92 | /** |
||
| 93 | * Adds a new required file |
||
| 94 | * |
||
| 95 | * @param string $file |
||
| 96 | * @return $this |
||
| 97 | */ |
||
| 98 | 2 | public function addRequiredFile(string $file) { |
|
| 103 | |||
| 104 | /** |
||
| 105 | * Returns required files |
||
| 106 | * |
||
| 107 | * @return Set collection of filenames |
||
| 108 | */ |
||
| 109 | 18 | public function getRequiredFiles(): Set { |
|
| 112 | |||
| 113 | /** |
||
| 114 | * Sets use statements |
||
| 115 | * |
||
| 116 | * @see #addUseStatement |
||
| 117 | * @see #declareUses() |
||
| 118 | * @param array $useStatements |
||
| 119 | * @return $this |
||
| 120 | */ |
||
| 121 | 1 | public function setUseStatements(array $useStatements) { |
|
| 129 | |||
| 130 | /** |
||
| 131 | * Adds a use statement with an optional alias |
||
| 132 | * |
||
| 133 | * @param string|PhpTypeInterface $qualifiedName |
||
| 134 | * @param null|string $alias |
||
| 135 | * @return $this |
||
| 136 | */ |
||
| 137 | 14 | public function addUseStatement($qualifiedName, string $alias = null) { |
|
| 163 | |||
| 164 | /** |
||
| 165 | * Clears all use statements |
||
| 166 | * |
||
| 167 | * @return $this |
||
| 168 | */ |
||
| 169 | 1 | public function clearUseStatements() { |
|
| 174 | |||
| 175 | /** |
||
| 176 | * Declares multiple use statements at once. |
||
| 177 | * |
||
| 178 | * @param ... use statements multiple qualified names |
||
| 179 | * @return $this |
||
| 180 | */ |
||
| 181 | 1 | public function declareUses(string ...$uses) { |
|
| 187 | |||
| 188 | /** |
||
| 189 | * Declares a "use $fullClassName" with " as $alias" if $alias is available, |
||
| 190 | * and returns its alias (or not qualified classname) to be used in your actual |
||
| 191 | * php code. |
||
| 192 | * |
||
| 193 | * If the class has already been declared you get only the set alias. |
||
| 194 | * |
||
| 195 | * @param string $qualifiedName |
||
| 196 | * @param null|string $alias |
||
| 197 | * @return string the used alias |
||
| 198 | */ |
||
| 199 | 1 | public function declareUse(string $qualifiedName, string $alias = null) { |
|
| 206 | |||
| 207 | /** |
||
| 208 | * Returns whether the given use statement is present |
||
| 209 | * |
||
| 210 | * @param string $qualifiedName |
||
| 211 | * @return bool |
||
| 212 | */ |
||
| 213 | 3 | public function hasUseStatement(string $qualifiedName): bool { |
|
| 216 | |||
| 217 | /** |
||
| 218 | * Returns the usable alias for a qualified name |
||
| 219 | * |
||
| 220 | * @param string $qualifiedName |
||
| 221 | * @return string the alias |
||
| 222 | */ |
||
| 223 | 1 | public function getUseAlias(string $qualifiedName): string { |
|
| 226 | |||
| 227 | /** |
||
| 228 | * Removes a use statement |
||
| 229 | * |
||
| 230 | * @param string $qualifiedName |
||
| 231 | * @return $this |
||
| 232 | */ |
||
| 233 | 3 | public function removeUseStatement(string $qualifiedName) { |
|
| 237 | |||
| 238 | /** |
||
| 239 | * Returns all use statements |
||
| 240 | * |
||
| 241 | * @return Map collection of use statements |
||
| 242 | */ |
||
| 243 | 21 | public function getUseStatements(): Map { |
|
| 246 | |||
| 247 | /** |
||
| 248 | * Sets a collection of methods |
||
| 249 | * |
||
| 250 | * @param PhpMethod[] $methods |
||
| 251 | * @return $this |
||
| 252 | */ |
||
| 253 | 1 | public function setMethods(array $methods) { |
|
| 265 | |||
| 266 | /** |
||
| 267 | * Adds a method |
||
| 268 | * |
||
| 269 | * @param PhpMethod $method |
||
| 270 | * @return $this |
||
| 271 | */ |
||
| 272 | 17 | public function addMethod(PhpMethod $method) { |
|
| 295 | |||
| 296 | /** |
||
| 297 | * Removes a method |
||
| 298 | * |
||
| 299 | * @param string|PhpMethod $nameOrMethod method name or Method instance |
||
| 300 | * @throws \InvalidArgumentException if the method cannot be found |
||
| 301 | * @return $this |
||
| 302 | */ |
||
| 303 | 2 | public function removeMethod($nameOrMethod) { |
|
| 317 | |||
| 318 | /** |
||
| 319 | * Checks whether a method exists or not |
||
| 320 | * |
||
| 321 | * @param string|PhpMethod $nameOrMethod method name or Method instance |
||
| 322 | * @return bool `true` if it exists and `false` if not |
||
| 323 | */ |
||
| 324 | 2 | public function hasMethod($nameOrMethod): bool { |
|
| 331 | |||
| 332 | /** |
||
| 333 | * Returns a method |
||
| 334 | * |
||
| 335 | * @param string $nameOrMethod the methods name |
||
| 336 | * @throws \InvalidArgumentException if the method cannot be found |
||
| 337 | * @return PhpMethod |
||
| 338 | */ |
||
| 339 | 10 | public function getMethod($nameOrMethod): PhpMethod { |
|
| 350 | |||
| 351 | /** |
||
| 352 | * Returns all methods |
||
| 353 | * |
||
| 354 | * @return Map|PhpMethod[] collection of methods |
||
| 355 | */ |
||
| 356 | 18 | public function getMethods(): Map { |
|
| 359 | |||
| 360 | /** |
||
| 361 | * Returns all method names |
||
| 362 | * |
||
| 363 | * @return Set |
||
| 364 | */ |
||
| 365 | 1 | public function getMethodNames(): Set { |
|
| 368 | |||
| 369 | /** |
||
| 370 | * Clears all methods |
||
| 371 | * |
||
| 372 | * @return $this |
||
| 373 | */ |
||
| 374 | 1 | public function clearMethods() { |
|
| 382 | |||
| 383 | /** |
||
| 384 | * Generates a docblock from provided information |
||
| 385 | * |
||
| 386 | * @return $this |
||
| 387 | */ |
||
| 388 | 11 | public function generateDocblock() { |
|
| 399 | } |
||
| 400 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.