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 |
||
5 | abstract class AbstractElement implements GenerableInterface |
||
6 | { |
||
7 | /** |
||
8 | * @var string |
||
9 | */ |
||
10 | protected $name; |
||
11 | /** |
||
12 | * @var AbstractElement[]|mixed[] |
||
13 | */ |
||
14 | protected $children; |
||
15 | /** |
||
16 | * @var int |
||
17 | */ |
||
18 | protected $indentation; |
||
19 | /** |
||
20 | * @param string $name |
||
21 | */ |
||
22 | 616 | public function __construct($name) |
|
28 | /** |
||
29 | * @param string $name |
||
30 | * @return AbstractElement |
||
31 | */ |
||
32 | 616 | public function setName($name) |
|
33 | { |
||
34 | 616 | if (!self::nameIsValid($name)) { |
|
35 | 40 | throw new \InvalidArgumentException(sprintf('Name "%s" is invalid when instantiating %s object', $name, $this->getCalledClass())); |
|
36 | } |
||
37 | 576 | $this->name = $name; |
|
38 | 576 | return $this; |
|
39 | } |
||
40 | /** |
||
41 | * @return string |
||
42 | */ |
||
43 | 404 | public function getName() |
|
47 | /** |
||
48 | * @param string $name |
||
49 | * @param bool $allowBackslash |
||
50 | * @return bool |
||
51 | */ |
||
52 | 624 | public static function nameIsValid($name, $allowBackslash = false) |
|
60 | /** |
||
61 | * @param mixed $string |
||
62 | * @param bool $checkName |
||
63 | * @param bool $allowBackslash |
||
64 | * @return bool |
||
65 | */ |
||
66 | 224 | public static function stringIsValid($string, $checkName = true, $allowBackslash = false) |
|
70 | /** |
||
71 | * @param mixed $object |
||
72 | * @return bool |
||
73 | */ |
||
74 | 172 | public static function objectIsValid($object, $checkClass = null) |
|
78 | /** |
||
79 | * @see \WsdlToPhp\PhpGenerator\Element\GenerableInterface::toString() |
||
80 | * @param int $indentation |
||
81 | * @return string |
||
82 | */ |
||
83 | 156 | public function toString($indentation = null) |
|
95 | /** |
||
96 | * @param int $indentation |
||
97 | * @return string|null |
||
98 | */ |
||
99 | 156 | private function getToStringDeclaration($indentation = null) |
|
107 | /** |
||
108 | * @param string $indentation |
||
109 | * @return string|null |
||
110 | */ |
||
111 | 156 | private function getToStringBeforeChildren($indentation = null) |
|
119 | /** |
||
120 | * @param string $indentation |
||
121 | * @return string|null |
||
122 | */ |
||
123 | 156 | private function getToStringAfterChildren($indentation = null) |
|
131 | /** |
||
132 | * @param array $array |
||
133 | * @return array |
||
134 | */ |
||
135 | 156 | private static function cleanArrayToString($array) |
|
145 | /** |
||
146 | * @throws \InvalidArgumentException |
||
147 | * @param string|AbstractElement $child |
||
148 | * @param int $indentation |
||
149 | * @return string |
||
150 | */ |
||
151 | 100 | protected function getChildContent($child, $indentation = null) |
|
161 | /** |
||
162 | * @return string |
||
163 | */ |
||
164 | 380 | public function getPhpName() |
|
168 | /** |
||
169 | * @throws \InvalidArgumentException |
||
170 | * @param mixed $child |
||
171 | * @return AbstractElement |
||
172 | */ |
||
173 | 164 | public function addChild($child) |
|
186 | /** |
||
187 | * @param mixed $child |
||
188 | * @return bool |
||
189 | */ |
||
190 | 188 | protected function childIsValid($child) |
|
201 | /** |
||
202 | * @return AbstractElement[]|mixed[] |
||
203 | */ |
||
204 | 220 | public function getChildren() |
|
208 | /** |
||
209 | * @return string |
||
210 | */ |
||
211 | abstract public function getPhpDeclaration(); |
||
212 | /** |
||
213 | * defines authorized children element types |
||
214 | * @return string[] |
||
215 | */ |
||
216 | abstract public function getChildrenTypes(); |
||
217 | /** |
||
218 | * @param int $indentation |
||
219 | * @return string |
||
220 | */ |
||
221 | 156 | private function getContextualLineBeforeChildren($indentation = null) |
|
230 | /** |
||
231 | * @param int $indentation |
||
232 | * @return string |
||
233 | */ |
||
234 | 156 | private function getContextualLineAfterChildren($indentation = null) |
|
243 | /** |
||
244 | * Allows to generate content before children content is generated |
||
245 | * @param int $indentation |
||
246 | * @return string |
||
247 | */ |
||
248 | 128 | public function getLineBeforeChildren($indentation = null) |
|
252 | /** |
||
253 | * Allows to generate content after children content is generated |
||
254 | * @param int $indentation |
||
255 | * @return string |
||
256 | */ |
||
257 | 128 | public function getLineAfterChildren($indentation = null) |
|
261 | /** |
||
262 | * Allows to indicate that children are contained by brackets, |
||
263 | * in the case the method returns true, getBracketBeforeChildren |
||
264 | * is called instead of getLineBeforeChildren and getBracketAfterChildren |
||
265 | * is called instead of getLineAfterChildren, but be aware that these methods |
||
266 | * call the two others |
||
267 | * @return boolean |
||
268 | */ |
||
269 | 124 | public function useBracketsForChildren() |
|
273 | /** |
||
274 | * Allows to generate content before children content is generated |
||
275 | * @param int $indentation |
||
276 | * @return string |
||
277 | */ |
||
278 | 72 | public function getBracketBeforeChildren($indentation = null) |
|
284 | /** |
||
285 | * Allows to generate content after children content is generated |
||
286 | * @param int $indentation |
||
287 | * @return string |
||
288 | */ |
||
289 | 72 | public function getBracketAfterChildren($indentation = null) |
|
294 | /** |
||
295 | * @param int $indentation |
||
296 | * @return AbstractElement |
||
297 | */ |
||
298 | 72 | public function setIndentation($indentation) |
|
303 | /** |
||
304 | * @return int |
||
305 | */ |
||
306 | 156 | public function getIndentation() |
|
310 | /** |
||
311 | * @see \WsdlToPhp\PhpGenerator\Element\GenerableInterface::getIndentationString() |
||
312 | * @param int $indentation |
||
313 | * @return string |
||
314 | */ |
||
315 | 156 | public function getIndentationString($indentation = null) |
|
319 | /** |
||
320 | * @param string $string |
||
321 | * @param int $indentation |
||
322 | * @return string |
||
323 | */ |
||
324 | 156 | public function getIndentedString($string, $indentation = null) |
|
332 | /** |
||
333 | * @return string |
||
334 | */ |
||
335 | 48 | final public function getCalledClass() |
|
339 | } |
||
340 |