Complex classes like Element 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 Element, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
5 | class Element implements ElementInterface |
||
6 | { |
||
7 | /** |
||
8 | * @var \DOMNode |
||
9 | */ |
||
10 | protected $node; |
||
11 | |||
12 | /** |
||
13 | * @var ElementInterface|null |
||
14 | */ |
||
15 | private $nextCached; |
||
16 | |||
17 | /** |
||
18 | * @var DOMNode|null |
||
19 | */ |
||
20 | private $previousSiblingCached; |
||
21 | |||
22 | 90 | public function __construct(\DOMNode $node) |
|
27 | |||
28 | /** |
||
29 | * @return bool |
||
30 | */ |
||
31 | 15 | public function isBlock() |
|
54 | |||
55 | /** |
||
56 | * @return bool |
||
57 | */ |
||
58 | 15 | public function isText() |
|
62 | |||
63 | /** |
||
64 | * @return bool |
||
65 | */ |
||
66 | 6 | public function isWhitespace() |
|
70 | |||
71 | /** |
||
72 | * @return string |
||
73 | */ |
||
74 | 90 | public function getTagName() |
|
78 | |||
79 | /** |
||
80 | * @return string |
||
81 | */ |
||
82 | 78 | public function getValue() |
|
86 | |||
87 | /** |
||
88 | * @return ElementInterface|null |
||
89 | */ |
||
90 | 75 | public function getParent() |
|
94 | |||
95 | /** |
||
96 | * @return ElementInterface|null |
||
97 | */ |
||
98 | 15 | public function getNextSibling() |
|
102 | |||
103 | /** |
||
104 | * @return ElementInterface|null |
||
105 | */ |
||
106 | 15 | public function getPreviousSibling() |
|
110 | |||
111 | /** |
||
112 | * @return bool |
||
113 | */ |
||
114 | 90 | public function hasChildren() |
|
118 | |||
119 | /** |
||
120 | * @return ElementInterface[] |
||
121 | */ |
||
122 | 90 | public function getChildren() |
|
132 | |||
133 | /** |
||
134 | * @return ElementInterface|null |
||
135 | */ |
||
136 | 21 | public function getNext() |
|
147 | |||
148 | /** |
||
149 | * @param \DomNode $node |
||
150 | * @param bool $checkChildren |
||
151 | * |
||
152 | * @return \DomNode|null |
||
153 | */ |
||
154 | 21 | private function getNextNode($node, $checkChildren = true) |
|
168 | |||
169 | /** |
||
170 | * @param string[]|string $tagNames |
||
171 | * |
||
172 | * @return bool |
||
173 | */ |
||
174 | 90 | public function isDescendantOf($tagNames) |
|
192 | |||
193 | /** |
||
194 | * @param string $markdown |
||
195 | */ |
||
196 | 90 | public function setFinalMarkdown($markdown) |
|
201 | |||
202 | /** |
||
203 | * @return string |
||
204 | */ |
||
205 | 81 | public function getChildrenAsString() |
|
209 | |||
210 | /** |
||
211 | * @return int |
||
212 | */ |
||
213 | 6 | public function getSiblingPosition() |
|
232 | |||
233 | /** |
||
234 | * @return int |
||
235 | */ |
||
236 | 9 | public function getListItemLevel() |
|
250 | |||
251 | /** |
||
252 | * @param string $name |
||
253 | * |
||
254 | * @return string |
||
255 | */ |
||
256 | 27 | public function getAttribute($name) |
|
264 | |||
265 | /** |
||
266 | * @param ElementInterface $element |
||
267 | * |
||
268 | * @return bool |
||
269 | */ |
||
270 | 6 | public function equals(ElementInterface $element) |
|
278 | } |
||
279 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..