Complex classes like Document 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 Document, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | class Document extends DOMDocument{ |
||
24 | use Magic; |
||
25 | |||
26 | const NODE_CLASSES = [ |
||
27 | 'Attr', |
||
28 | 'CharacterData', |
||
29 | 'Comment', |
||
30 | 'DocumentFragment', |
||
31 | 'DocumentType', |
||
32 | 'Element', |
||
33 | 'Text', |
||
34 | ]; |
||
35 | |||
36 | /** |
||
37 | * Document constructor. |
||
38 | * |
||
39 | * @param string $content |
||
40 | * @param bool $xml |
||
41 | * @param string|null $version |
||
42 | * @param string|null $encoding |
||
43 | */ |
||
44 | 42 | public function __construct($content = null, $xml = false, $version = '1.0', $encoding = 'UTF-8'){ |
|
56 | |||
57 | |||
58 | /********* |
||
59 | * magic * |
||
60 | *********/ |
||
61 | |||
62 | 1 | public function magic_get_title(){ |
|
65 | |||
66 | 1 | public function magic_set_title(string $title){ |
|
83 | |||
84 | |||
85 | |||
86 | /******** |
||
87 | * ugly * |
||
88 | ********/ |
||
89 | |||
90 | 2 | public function _loadDocument($content, $xml = false){ |
|
99 | |||
100 | 1 | public function _loadDocumentString(string $documentSource, bool $xml = false){ |
|
109 | |||
110 | /** |
||
111 | * @param mixed $content |
||
112 | * |
||
113 | * @return \chillerlan\PrototypeDOM\NodeList |
||
114 | * @throws \Exception |
||
115 | */ |
||
116 | 8 | public function _toNodeList($content):NodeList{ |
|
129 | |||
130 | /** |
||
131 | * @param string $content |
||
132 | * |
||
133 | * @return \chillerlan\PrototypeDOM\NodeList |
||
134 | */ |
||
135 | 8 | public function _HTMLFragmentToNodeList(string $content):NodeList{ |
|
141 | |||
142 | /** |
||
143 | * @param array $array |
||
144 | * |
||
145 | * @return \chillerlan\PrototypeDOM\NodeList |
||
146 | */ |
||
147 | 3 | public function _arrayToNodeList(array $array):NodeList{ |
|
156 | |||
157 | |||
158 | /*********** |
||
159 | * generic * |
||
160 | ***********/ |
||
161 | |||
162 | /** |
||
163 | * @param string $selector |
||
164 | * @param string $axis |
||
165 | * |
||
166 | * @return string |
||
167 | */ |
||
168 | 41 | public function selector2xpath(string $selector, string $axis = '//'):string{ |
|
171 | |||
172 | /** |
||
173 | * @param string $xpath |
||
174 | * @param \DOMNode|null $contextNode |
||
175 | * |
||
176 | * @return \chillerlan\PrototypeDOM\NodeList |
||
177 | */ |
||
178 | 41 | public function query(string $xpath, DOMNode $contextNode = null):NodeList{ |
|
181 | |||
182 | /** |
||
183 | * @param string $selector |
||
184 | * @param \DOMNode|null $contextNode |
||
185 | * @param string $axis |
||
186 | * |
||
187 | * @return \chillerlan\PrototypeDOM\NodeList |
||
188 | */ |
||
189 | 41 | public function querySelectorAll(string $selector, DOMNode $contextNode = null, string $axis = 'descendant-or-self::'):NodeList{ |
|
192 | |||
193 | /** |
||
194 | * @param string|array $selectors |
||
195 | * @param \DOMNode|null $contextNode |
||
196 | * @param string $axis |
||
197 | * |
||
198 | * @return \chillerlan\PrototypeDOM\Document |
||
199 | */ |
||
200 | 1 | public function removeElementsBySelector($selectors, DOMNode $contextNode = null, string $axis = 'descendant-or-self::'):Document{ |
|
213 | |||
214 | /** |
||
215 | * @param \chillerlan\PrototypeDOM\NodeList $nodeList |
||
216 | * |
||
217 | * @return \chillerlan\PrototypeDOM\Document |
||
218 | */ |
||
219 | 2 | public function insertNodeList(NodeList $nodeList):Document{ |
|
228 | |||
229 | /************* |
||
230 | * prototype * |
||
231 | *************/ |
||
232 | |||
233 | /** |
||
234 | * @link http://api.prototypejs.org/dom/Element/inspect/ |
||
235 | * |
||
236 | * @param \DOMNode|null $context |
||
237 | * @param bool $xml |
||
238 | * |
||
239 | * @return string |
||
240 | */ |
||
241 | 8 | public function inspect(DOMNode $context = null, $xml = false):string{ |
|
246 | |||
247 | /** |
||
248 | * @link http://api.prototypejs.org/dom/Element/select/ |
||
249 | * |
||
250 | * @param string|array $selectors |
||
251 | * @param \DOMNode|null $contextNode |
||
252 | * @param string $axis |
||
253 | * @param int $nodeType |
||
254 | * |
||
255 | * @return \chillerlan\PrototypeDOM\NodeList |
||
256 | */ |
||
257 | 41 | public function select($selectors = null, DOMNode $contextNode = null, string $axis = 'descendant-or-self::', int $nodeType = XML_ELEMENT_NODE):NodeList{ |
|
283 | |||
284 | /** |
||
285 | * @link http://api.prototypejs.org/dom/Element/recursivelyCollect/ |
||
286 | * |
||
287 | * @param \chillerlan\PrototypeDOM\Node\PrototypeNode $element |
||
288 | * @param string $property |
||
289 | * @param int $maxLength |
||
290 | * @param int $nodeType |
||
291 | * |
||
292 | * @return \chillerlan\PrototypeDOM\NodeList |
||
293 | */ |
||
294 | 3 | public function recursivelyCollect(PrototypeNode $element, string $property, int $maxLength = -1, int $nodeType = XML_ELEMENT_NODE):NodeList{ |
|
315 | |||
316 | /** |
||
317 | * @param \chillerlan\PrototypeDOM\Node\PrototypeNode $element |
||
318 | * @param string $property |
||
319 | * @param string|null $selector |
||
320 | * @param int $index |
||
321 | * @param int $nodeType |
||
322 | * |
||
323 | * @return \chillerlan\PrototypeDOM\Node\PrototypeElement|null |
||
324 | */ |
||
325 | 4 | public function _recursivelyFind(PrototypeNode $element, string $property, string $selector = null, int $index = 0, int $nodeType = XML_ELEMENT_NODE){ |
|
343 | |||
344 | /** |
||
345 | * @link http://api.prototypejs.org/dom/Element/match/ |
||
346 | * |
||
347 | * @param \chillerlan\PrototypeDOM\Node\PrototypeNode $element |
||
348 | * @param string $selector |
||
349 | * |
||
350 | * @return bool |
||
351 | */ |
||
352 | 4 | public function match(PrototypeNode $element, string $selector):bool{ |
|
364 | |||
365 | /** |
||
366 | * @link http://api.prototypejs.org/dom/Element/new/ |
||
367 | * |
||
368 | * @param string $tag |
||
369 | * @param array|null $attributes |
||
370 | * |
||
371 | * @return \chillerlan\PrototypeDOM\Node\Element |
||
372 | */ |
||
373 | 6 | public function newElement(string $tag, array $attributes = null):Element{ |
|
383 | |||
384 | } |
||
385 |
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.