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 | 96 | public function __construct(\DOMNode $node) |
|
27 | |||
28 | /** |
||
29 | * @return bool |
||
30 | */ |
||
31 | 18 | public function isBlock() |
|
32 | { |
||
33 | 18 | switch ($this->getTagName()) { |
|
34 | 18 | case 'blockquote': |
|
35 | 18 | case 'body': |
|
36 | 18 | case 'div': |
|
37 | 18 | case 'h1': |
|
38 | 18 | case 'h2': |
|
39 | 18 | case 'h3': |
|
40 | 18 | case 'h4': |
|
41 | 18 | case 'h5': |
|
42 | 18 | case 'h6': |
|
43 | 18 | case 'hr': |
|
44 | 18 | case 'html': |
|
45 | 18 | case 'li': |
|
46 | 17 | case 'p': |
|
47 | 17 | case 'ol': |
|
48 | 17 | case 'ul': |
|
49 | 3 | return true; |
|
50 | 10 | default: |
|
51 | 15 | return false; |
|
52 | 10 | } |
|
53 | } |
||
54 | |||
55 | /** |
||
56 | * @return bool |
||
57 | */ |
||
58 | 18 | public function isText() |
|
62 | |||
63 | /** |
||
64 | * @return bool |
||
65 | */ |
||
66 | 9 | public function isWhitespace() |
|
70 | |||
71 | /** |
||
72 | * @return string |
||
73 | */ |
||
74 | 96 | public function getTagName() |
|
78 | |||
79 | /** |
||
80 | * @return string |
||
81 | */ |
||
82 | 84 | public function getValue() |
|
86 | |||
87 | /** |
||
88 | * @return ElementInterface|null |
||
89 | */ |
||
90 | 81 | public function getParent() |
|
94 | |||
95 | /** |
||
96 | * @return ElementInterface|null |
||
97 | */ |
||
98 | 18 | public function getNextSibling() |
|
102 | |||
103 | /** |
||
104 | * @return ElementInterface|null |
||
105 | */ |
||
106 | 18 | public function getPreviousSibling() |
|
110 | |||
111 | /** |
||
112 | * @return bool |
||
113 | */ |
||
114 | 96 | public function hasChildren() |
|
118 | |||
119 | /** |
||
120 | * @return ElementInterface[] |
||
121 | */ |
||
122 | 96 | public function getChildren() |
|
123 | { |
||
124 | 96 | $ret = array(); |
|
125 | /** @var \DOMNode $node */ |
||
126 | 96 | foreach ($this->node->childNodes as $node) { |
|
127 | 96 | $ret[] = new static($node); |
|
128 | 64 | } |
|
129 | |||
130 | 96 | return $ret; |
|
131 | } |
||
132 | |||
133 | /** |
||
134 | * @return ElementInterface|null |
||
135 | */ |
||
136 | 24 | public function getNext() |
|
137 | { |
||
138 | 24 | if ($this->nextCached === null) { |
|
139 | 24 | $nextNode = $this->getNextNode($this->node); |
|
140 | 24 | if ($nextNode !== null) { |
|
141 | 24 | $this->nextCached = new static($nextNode); |
|
142 | 16 | } |
|
143 | 16 | } |
|
144 | |||
145 | 24 | return $this->nextCached; |
|
146 | } |
||
147 | |||
148 | /** |
||
149 | * @param \DomNode $node |
||
150 | * @param bool $checkChildren |
||
151 | * |
||
152 | * @return \DomNode|null |
||
153 | */ |
||
154 | 24 | private function getNextNode($node, $checkChildren = true) |
|
168 | |||
169 | /** |
||
170 | * @param string[]|string $tagNames |
||
171 | * |
||
172 | * @return bool |
||
173 | */ |
||
174 | 96 | public function isDescendantOf($tagNames) |
|
175 | { |
||
176 | 96 | if (!is_array($tagNames)) { |
|
177 | 6 | $tagNames = array($tagNames); |
|
178 | 4 | } |
|
179 | |||
180 | 96 | for ($p = $this->node->parentNode; $p !== false; $p = $p->parentNode) { |
|
181 | 96 | if (is_null($p)) { |
|
182 | 96 | return false; |
|
183 | } |
||
184 | |||
185 | 96 | if (in_array($p->nodeName, $tagNames)) { |
|
186 | 24 | return true; |
|
187 | } |
||
188 | 64 | } |
|
189 | |||
190 | return false; |
||
191 | } |
||
192 | |||
193 | /** |
||
194 | * @param string $markdown |
||
195 | */ |
||
196 | 96 | public function setFinalMarkdown($markdown) |
|
201 | |||
202 | /** |
||
203 | * @return string |
||
204 | */ |
||
205 | 87 | public function getChildrenAsString() |
|
209 | |||
210 | /** |
||
211 | * @return int |
||
212 | */ |
||
213 | 9 | public function getSiblingPosition() |
|
214 | { |
||
215 | 9 | $position = 0; |
|
216 | |||
217 | // Loop through all nodes and find the given $node |
||
218 | 9 | foreach ($this->getParent()->getChildren() as $current_node) { |
|
219 | 9 | if (!$current_node->isWhitespace()) { |
|
220 | 9 | $position++; |
|
221 | 6 | } |
|
222 | |||
223 | // TODO: Need a less-buggy way of comparing these |
||
224 | // Perhaps we can somehow ensure that we always have the exact same object and use === instead? |
||
225 | 9 | if ($this->equals($current_node)) { |
|
226 | 9 | break; |
|
227 | } |
||
228 | 6 | } |
|
229 | |||
230 | 9 | return $position; |
|
231 | } |
||
232 | |||
233 | /** |
||
234 | * @return int |
||
235 | */ |
||
236 | 12 | public function getListItemLevel() |
|
237 | { |
||
238 | 12 | $level = 0; |
|
239 | 12 | $parent = $this->getParent(); |
|
240 | |||
241 | 12 | while ($parent !== null && $parent->node->parentNode) { |
|
242 | 12 | if ($parent->getTagName() === 'li') { |
|
243 | 6 | $level++; |
|
244 | 4 | } |
|
245 | 12 | $parent = $parent->getParent(); |
|
246 | 8 | } |
|
247 | |||
248 | 12 | return $level; |
|
249 | } |
||
250 | |||
251 | /** |
||
252 | * @param string $name |
||
253 | * |
||
254 | * @return string |
||
255 | */ |
||
256 | 30 | public function getAttribute($name) |
|
264 | |||
265 | /** |
||
266 | * @param ElementInterface $element |
||
267 | * |
||
268 | * @return bool |
||
269 | */ |
||
270 | 9 | 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..