Complex classes like NodeList 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 NodeList, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | class NodeList implements IteratorAggregate |
||
24 | { |
||
25 | use EscapeHtmlTrait; |
||
26 | |||
27 | /** |
||
28 | * @var IteratorIterator |
||
29 | */ |
||
30 | protected $nodes; |
||
31 | |||
32 | /** |
||
33 | * @var Locator |
||
34 | */ |
||
35 | protected $locator; |
||
36 | |||
37 | /** |
||
38 | * @param Locator $locator |
||
39 | * @param array|DOMNodeList $nodes DOMNodes in array or DOMNodeList |
||
40 | */ |
||
41 | public function __construct(Locator $locator, $nodes = null) |
||
46 | |||
47 | /** |
||
48 | * @param array|DOMNodeList $nodes DOMNodes in array or DOMNodeList |
||
49 | * @return $this |
||
50 | */ |
||
51 | public function create($nodes) |
||
55 | |||
56 | /** |
||
57 | * @return IteratorIterator |
||
58 | */ |
||
59 | public function getNodes() |
||
66 | |||
67 | /** |
||
68 | * @param array|DOMNodeList|IteratorIterator $nodes |
||
69 | * @return $this |
||
70 | * @throws Exception\InvalidArgumentException |
||
71 | */ |
||
72 | public function setNodes($nodes) |
||
95 | |||
96 | /** |
||
97 | * @return \Traversable |
||
98 | */ |
||
99 | public function getIterator() |
||
103 | |||
104 | /** |
||
105 | * Set nodes text value |
||
106 | * |
||
107 | * @param string $value |
||
108 | * @param Callable $preSet Modify and return value, passed parameters $node, $value |
||
109 | * @return $this |
||
110 | */ |
||
111 | public function setValue($value, $preSet = null) |
||
121 | |||
122 | /** |
||
123 | * Set nodes html value |
||
124 | * |
||
125 | * @param string $xhtml |
||
126 | * @param Callable $preSet Modify and return xhtml, passed parameters $node, $xhtml |
||
127 | * @return $this |
||
128 | */ |
||
129 | public function setHtml($xhtml, $preSet = null) |
||
146 | |||
147 | /** |
||
148 | * Append XHTML to nodes |
||
149 | * |
||
150 | * @param string $xhtml Valid XHTML |
||
151 | * @return $this Newly created nodes |
||
152 | */ |
||
153 | public function appendHtml($xhtml) |
||
173 | |||
174 | /** |
||
175 | * Set nodes attributes |
||
176 | * |
||
177 | * @param array $attribs Attributes to set |
||
178 | * @param Callable $preSet Modify and return value, assed parameters $node, $value |
||
179 | * @return $this |
||
180 | */ |
||
181 | public function setAttribs(array $attribs, $preSet = null) |
||
200 | |||
201 | /** |
||
202 | * Replace node with XHTML code |
||
203 | * |
||
204 | * @param string $xhtml XHTML to replace node |
||
205 | * @param Callable $preSet Modify and return xhtml, assed parameters $node, $xhtml |
||
206 | * @return $this |
||
207 | */ |
||
208 | public function replace($xhtml, $preSet = null) |
||
238 | |||
239 | /** |
||
240 | * Remove target nodes |
||
241 | * |
||
242 | * @param string $locator CSS selector or XPath (xpath=) |
||
243 | * @return $this |
||
244 | */ |
||
245 | public function remove($locator = '.') |
||
266 | |||
267 | /** |
||
268 | * Perform callback on each node that match the locator |
||
269 | * |
||
270 | * @param string $locator |
||
271 | * @param Callable $callback The NodeList parameter is passed |
||
272 | * @return $this |
||
273 | * @throws Exception\RuntimeException |
||
274 | */ |
||
275 | public function each($locator, $callback) |
||
295 | |||
296 | /** |
||
297 | * Return nodes in array |
||
298 | * |
||
299 | * Error-free nodes |
||
300 | * manipulation in a loop. |
||
301 | * |
||
302 | * @return array |
||
303 | */ |
||
304 | public function toArray() |
||
312 | } |
||
313 |