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 | 57 | 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 | 17 | 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 | 13 | public function addUseStatement($qualifiedName, string $alias = null) { |
|
161 | |||
162 | /** |
||
163 | * Clears all use statements |
||
164 | * |
||
165 | * @return $this |
||
166 | */ |
||
167 | 1 | public function clearUseStatements() { |
|
172 | |||
173 | /** |
||
174 | * Declares multiple use statements at once. |
||
175 | * |
||
176 | * @param ... use statements multiple qualified names |
||
177 | * @return $this |
||
178 | */ |
||
179 | 1 | public function declareUses(string ...$uses) { |
|
185 | |||
186 | /** |
||
187 | * Declares a "use $fullClassName" with " as $alias" if $alias is available, |
||
188 | * and returns its alias (or not qualified classname) to be used in your actual |
||
189 | * php code. |
||
190 | * |
||
191 | * If the class has already been declared you get only the set alias. |
||
192 | * |
||
193 | * @param string $qualifiedName |
||
194 | * @param null|string $alias |
||
195 | * @return string the used alias |
||
196 | */ |
||
197 | 1 | public function declareUse(string $qualifiedName, string $alias = null) { |
|
204 | |||
205 | /** |
||
206 | * Returns whether the given use statement is present |
||
207 | * |
||
208 | * @param string $qualifiedName |
||
209 | * @return bool |
||
210 | */ |
||
211 | 3 | public function hasUseStatement(string $qualifiedName): bool { |
|
214 | |||
215 | /** |
||
216 | * Returns the usable alias for a qualified name |
||
217 | * |
||
218 | * @param string $qualifiedName |
||
219 | * @return string the alias |
||
220 | */ |
||
221 | 1 | public function getUseAlias(string $qualifiedName): string { |
|
224 | |||
225 | /** |
||
226 | * Removes a use statement |
||
227 | * |
||
228 | * @param string $qualifiedName |
||
229 | * @return $this |
||
230 | */ |
||
231 | 3 | public function removeUseStatement(string $qualifiedName) { |
|
235 | |||
236 | /** |
||
237 | * Returns all use statements |
||
238 | * |
||
239 | * @return Map collection of use statements |
||
240 | */ |
||
241 | 20 | public function getUseStatements(): Map { |
|
244 | |||
245 | /** |
||
246 | * Sets a collection of methods |
||
247 | * |
||
248 | * @param PhpMethod[] $methods |
||
249 | * @return $this |
||
250 | */ |
||
251 | 1 | public function setMethods(array $methods) { |
|
263 | |||
264 | /** |
||
265 | * Adds a method |
||
266 | * |
||
267 | * @param PhpMethod $method |
||
268 | * @return $this |
||
269 | */ |
||
270 | 16 | public function addMethod(PhpMethod $method) { |
|
293 | |||
294 | /** |
||
295 | * Removes a method |
||
296 | * |
||
297 | * @param string|PhpMethod $nameOrMethod method name or Method instance |
||
298 | * @throws \InvalidArgumentException if the method cannot be found |
||
299 | * @return $this |
||
300 | */ |
||
301 | 2 | public function removeMethod($nameOrMethod) { |
|
315 | |||
316 | /** |
||
317 | * Checks whether a method exists or not |
||
318 | * |
||
319 | * @param string|PhpMethod $nameOrMethod method name or Method instance |
||
320 | * @return bool `true` if it exists and `false` if not |
||
321 | */ |
||
322 | 2 | public function hasMethod($nameOrMethod): bool { |
|
329 | |||
330 | /** |
||
331 | * Returns a method |
||
332 | * |
||
333 | * @param string $nameOrMethod the methods name |
||
334 | * @throws \InvalidArgumentException if the method cannot be found |
||
335 | * @return PhpMethod |
||
336 | */ |
||
337 | 10 | public function getMethod($nameOrMethod): PhpMethod { |
|
348 | |||
349 | /** |
||
350 | * Returns all methods |
||
351 | * |
||
352 | * @return Map|PhpMethod[] collection of methods |
||
353 | */ |
||
354 | 17 | public function getMethods(): Map { |
|
357 | |||
358 | /** |
||
359 | * Returns all method names |
||
360 | * |
||
361 | * @return Set |
||
362 | */ |
||
363 | 1 | public function getMethodNames(): Set { |
|
366 | |||
367 | /** |
||
368 | * Clears all methods |
||
369 | * |
||
370 | * @return $this |
||
371 | */ |
||
372 | 1 | public function clearMethods() { |
|
380 | |||
381 | /** |
||
382 | * Generates a docblock from provided information |
||
383 | * |
||
384 | * @return $this |
||
385 | */ |
||
386 | 11 | public function generateDocblock() { |
|
397 | } |
||
398 |
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.