Complex classes like AbstractElement 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 AbstractElement, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
7 | abstract class AbstractElement implements GenerateableInterface |
||
8 | { |
||
9 | /** |
||
10 | * @var string |
||
11 | */ |
||
12 | protected $name; |
||
13 | /** |
||
14 | * @var AbstractElement[]|mixed[] |
||
15 | */ |
||
16 | protected $children; |
||
17 | /** |
||
18 | * @var int |
||
19 | */ |
||
20 | protected $indentation; |
||
21 | /** |
||
22 | * @param string $name |
||
23 | */ |
||
24 | 288 | public function __construct(string $name) |
|
30 | /** |
||
31 | * @throws \InvalidArgumentException |
||
32 | * @param string $name |
||
33 | * @return AbstractElement |
||
34 | */ |
||
35 | 288 | public function setName(string $name) |
|
43 | /** |
||
44 | * @return string |
||
45 | */ |
||
46 | 204 | public function getName() |
|
50 | /** |
||
51 | * @param string $name |
||
52 | * @param bool $allowBackslash |
||
53 | * @return bool |
||
54 | */ |
||
55 | 292 | public static function nameIsValid(string $name, bool $allowBackslash = false) |
|
63 | /** |
||
64 | * @param mixed $string |
||
65 | * @param bool $checkName |
||
66 | * @param bool $allowBackslash |
||
67 | * @return bool |
||
68 | */ |
||
69 | 112 | public static function stringIsValid($string, bool $checkName = true, bool $allowBackslash = false): bool |
|
73 | /** |
||
74 | * @param mixed $object |
||
75 | * @param mixed $checkClass |
||
76 | * @return bool |
||
77 | */ |
||
78 | 86 | public static function objectIsValid($object, ?string $checkClass = null): bool |
|
82 | /** |
||
83 | * @see \WsdlToPhp\PhpGenerator\Element\GenerateableInterface::toString() |
||
84 | * @param int $indentation |
||
85 | * @return string |
||
86 | */ |
||
87 | 78 | public function toString(int $indentation = null): string |
|
99 | /** |
||
100 | * @param int $indentation |
||
101 | * @return string|null |
||
102 | */ |
||
103 | 78 | private function getToStringDeclaration(int $indentation = null): ?string |
|
111 | /** |
||
112 | * @param int $indentation |
||
113 | * @return string|null |
||
114 | */ |
||
115 | 78 | private function getToStringBeforeChildren(int $indentation = null): ?string |
|
123 | /** |
||
124 | * @param int $indentation |
||
125 | * @return string|null |
||
126 | */ |
||
127 | 78 | private function getToStringAfterChildren(int $indentation = null): ?string |
|
135 | /** |
||
136 | * @param array $array |
||
137 | * @return array |
||
138 | */ |
||
139 | 78 | private static function cleanArrayToString(array $array): array |
|
149 | /** |
||
150 | * @throws \InvalidArgumentException |
||
151 | * @param string|AbstractElement $child |
||
152 | * @param int $indentation |
||
153 | * @return string |
||
154 | */ |
||
155 | 50 | protected function getChildContent($child, int $indentation = null): string |
|
165 | /** |
||
166 | * @return string |
||
167 | */ |
||
168 | 190 | public function getPhpName(): string |
|
172 | /** |
||
173 | * @throws \InvalidArgumentException |
||
174 | * @param mixed $child |
||
175 | * @return AbstractElement |
||
176 | */ |
||
177 | 82 | public function addChild($child): AbstractElement |
|
190 | /** |
||
191 | * @param mixed $child |
||
192 | * @return bool |
||
193 | */ |
||
194 | 94 | protected function childIsValid($child): bool |
|
205 | /** |
||
206 | * @return AbstractElement[]|mixed[] |
||
207 | */ |
||
208 | 110 | public function getChildren(): array |
|
212 | /** |
||
213 | * @return string |
||
214 | */ |
||
215 | abstract public function getPhpDeclaration(): string; |
||
216 | /** |
||
217 | * defines authorized children element types |
||
218 | * @return string[] |
||
219 | */ |
||
220 | abstract public function getChildrenTypes(): array; |
||
221 | /** |
||
222 | * @param int $indentation |
||
223 | * @return string |
||
224 | */ |
||
225 | 78 | private function getContextualLineBeforeChildren(int $indentation = null): string |
|
234 | /** |
||
235 | * @param int $indentation |
||
236 | * @return string |
||
237 | */ |
||
238 | 78 | private function getContextualLineAfterChildren(int $indentation = null): string |
|
247 | /** |
||
248 | * Allows to generate content before children content is generated |
||
249 | * @param int $indentation |
||
250 | * @return string |
||
251 | */ |
||
252 | 64 | public function getLineBeforeChildren(int $indentation = null): string |
|
256 | /** |
||
257 | * Allows to generate content after children content is generated |
||
258 | * @param int $indentation |
||
259 | * @return string |
||
260 | */ |
||
261 | 64 | public function getLineAfterChildren(int $indentation = null): string |
|
265 | /** |
||
266 | * Allows to indicate that children are contained by brackets, |
||
267 | * in the case the method returns true, getBracketBeforeChildren |
||
268 | * is called instead of getLineBeforeChildren and getBracketAfterChildren |
||
269 | * is called instead of getLineAfterChildren, but be aware that these methods |
||
270 | * call the two others |
||
271 | * @return bool |
||
272 | */ |
||
273 | 62 | public function useBracketsForChildren(): bool |
|
277 | /** |
||
278 | * Allows to generate content before children content is generated |
||
279 | * @param int $indentation |
||
280 | * @return string |
||
281 | */ |
||
282 | 36 | public function getBracketBeforeChildren(int $indentation = null): string |
|
288 | /** |
||
289 | * Allows to generate content after children content is generated |
||
290 | * @param int $indentation |
||
291 | * @return string |
||
292 | */ |
||
293 | 36 | public function getBracketAfterChildren(int $indentation = null): string |
|
298 | /** |
||
299 | * @param int $indentation |
||
300 | * @return AbstractElement |
||
301 | */ |
||
302 | 36 | public function setIndentation(int $indentation): AbstractElement |
|
307 | /** |
||
308 | * @return int |
||
309 | */ |
||
310 | 78 | public function getIndentation(): int |
|
314 | /** |
||
315 | * @see \WsdlToPhp\PhpGenerator\Element\GenerateableInterface::getIndentationString() |
||
316 | * @param int $indentation |
||
317 | * @return string |
||
318 | */ |
||
319 | 78 | public function getIndentationString(int $indentation = null): string |
|
323 | /** |
||
324 | * @param string $string |
||
325 | * @param int $indentation |
||
326 | * @return string |
||
327 | */ |
||
328 | 78 | public function getIndentedString(string $string, int $indentation = null): string |
|
336 | /** |
||
337 | * @return string |
||
338 | */ |
||
339 | 4 | final public function getCalledClass(): string |
|
343 | } |
||
344 |