Complex classes like TraversalTrait 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 TraversalTrait, and based on these observations, apply Extract Interface, too.
| 1 | <?php declare(strict_types=1); |
||
| 17 | trait TraversalTrait |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * @param iterable $nodes |
||
| 21 | * |
||
| 22 | * @return NodeList |
||
| 23 | */ |
||
| 24 | 138 | public function newNodeList(iterable $nodes = null): NodeList { |
|
| 36 | |||
| 37 | /** |
||
| 38 | * @param string $selector |
||
| 39 | * @param string $prefix |
||
| 40 | * |
||
| 41 | * @return NodeList |
||
| 42 | */ |
||
| 43 | 130 | public function find(string $selector, string $prefix = 'descendant::'): NodeList { |
|
| 48 | |||
| 49 | /** |
||
| 50 | * @param string $xpath |
||
| 51 | * |
||
| 52 | * @return NodeList |
||
| 53 | */ |
||
| 54 | 130 | public function findXPath(string $xpath): NodeList { |
|
| 71 | |||
| 72 | /** |
||
| 73 | * @param string|NodeList|\DOMNode|callable $input |
||
| 74 | * @param bool $matchType |
||
| 75 | * |
||
| 76 | * @return NodeList |
||
| 77 | */ |
||
| 78 | 18 | protected function getNodesMatchingInput($input, bool $matchType = true): NodeList { |
|
| 112 | |||
| 113 | /** |
||
| 114 | * @param string|NodeList|\DOMNode|callable $input |
||
| 115 | * |
||
| 116 | * @return bool |
||
| 117 | */ |
||
| 118 | 15 | public function is($input): bool { |
|
| 121 | |||
| 122 | /** |
||
| 123 | * @param string|NodeList|\DOMNode|callable $input |
||
| 124 | * |
||
| 125 | * @return NodeList |
||
| 126 | */ |
||
| 127 | 1 | public function not($input): NodeList { |
|
| 130 | |||
| 131 | /** |
||
| 132 | * @param string|NodeList|\DOMNode|callable $input |
||
| 133 | * |
||
| 134 | * @return NodeList |
||
| 135 | */ |
||
| 136 | 1 | public function filter($input): NodeList { |
|
| 139 | |||
| 140 | /** |
||
| 141 | * @param string|NodeList|\DOMNode|callable $input |
||
| 142 | * |
||
| 143 | * @return NodeList |
||
| 144 | */ |
||
| 145 | 1 | public function has($input): NodeList { |
|
| 177 | |||
| 178 | /** |
||
| 179 | * @param string|NodeList|\DOMNode|callable $selector |
||
| 180 | * |
||
| 181 | * @return \DOMNode|null |
||
| 182 | */ |
||
| 183 | public function preceding($selector = null): ?\DOMNode { |
||
| 186 | |||
| 187 | /** |
||
| 188 | * @param string|NodeList|\DOMNode|callable $selector |
||
| 189 | * |
||
| 190 | * @return NodeList |
||
| 191 | */ |
||
| 192 | 21 | public function precedingAll($selector = null): NodeList { |
|
| 195 | |||
| 196 | /** |
||
| 197 | * @param string|NodeList|\DOMNode|callable $input |
||
| 198 | * @param string|NodeList|\DOMNode|callable $selector |
||
| 199 | * |
||
| 200 | * @return NodeList |
||
| 201 | */ |
||
| 202 | 32 | public function precedingUntil($input = null, $selector = null): NodeList { |
|
| 205 | |||
| 206 | /** |
||
| 207 | * @param string|NodeList|\DOMNode|callable $selector |
||
| 208 | * |
||
| 209 | * @return \DOMNode|null |
||
| 210 | */ |
||
| 211 | 12 | public function following($selector = null): ?\DOMNode { |
|
| 214 | |||
| 215 | /** |
||
| 216 | * @param string|NodeList|\DOMNode|callable $selector |
||
| 217 | * |
||
| 218 | * @return NodeList |
||
| 219 | */ |
||
| 220 | 21 | public function followingAll($selector = null): NodeList { |
|
| 223 | |||
| 224 | /** |
||
| 225 | * @param string|NodeList|\DOMNode|callable $input |
||
| 226 | * @param string|NodeList|\DOMNode|callable $selector |
||
| 227 | * |
||
| 228 | * @return NodeList |
||
| 229 | */ |
||
| 230 | 25 | public function followingUntil($input = null, $selector = null): NodeList { |
|
| 233 | |||
| 234 | /** |
||
| 235 | * @param string|NodeList|\DOMNode|callable $selector |
||
| 236 | * |
||
| 237 | * @return NodeList |
||
| 238 | */ |
||
| 239 | public function siblings($selector = null): NodeList { |
||
| 250 | |||
| 251 | /** |
||
| 252 | * NodeList is only array like. Removing items using foreach() has undesired results. |
||
| 253 | * |
||
| 254 | * @return NodeList |
||
| 255 | */ |
||
| 256 | public function children(): NodeList { |
||
| 265 | |||
| 266 | /** |
||
| 267 | * @param string|NodeList|\DOMNode|callable $selector |
||
| 268 | * |
||
| 269 | * @return Element|NodeList|null |
||
| 270 | */ |
||
| 271 | 51 | public function parent($selector = null) { |
|
| 276 | |||
| 277 | /** |
||
| 278 | * @param int $index |
||
| 279 | * |
||
| 280 | * @return \DOMNode|null |
||
| 281 | */ |
||
| 282 | 2 | public function eq(int $index): ?\DOMNode { |
|
| 289 | |||
| 290 | /** |
||
| 291 | * @param string $selector |
||
| 292 | * |
||
| 293 | * @return NodeList |
||
| 294 | */ |
||
| 295 | public function parents(string $selector = null): NodeList { |
||
| 298 | |||
| 299 | /** |
||
| 300 | * @param string|NodeList|\DOMNode|callable $input |
||
| 301 | * @param string|NodeList|\DOMNode|callable $selector |
||
| 302 | * |
||
| 303 | * @return NodeList |
||
| 304 | */ |
||
| 305 | public function parentsUntil($input = null, $selector = null): NodeList { |
||
| 308 | |||
| 309 | /** |
||
| 310 | * @return \DOMNode |
||
| 311 | */ |
||
| 312 | public function intersect(): \DOMNode { |
||
| 331 | |||
| 332 | /** |
||
| 333 | * @param string|NodeList|\DOMNode|callable $input |
||
| 334 | * |
||
| 335 | * @return Element|NodeList|null |
||
| 336 | */ |
||
| 337 | 3 | public function closest($input) { |
|
| 342 | |||
| 343 | /** |
||
| 344 | * NodeList is only array like. Removing items using foreach() has undesired results. |
||
| 345 | * |
||
| 346 | * @return NodeList |
||
| 347 | */ |
||
| 348 | public function contents(): NodeList { |
||
| 359 | |||
| 360 | /** |
||
| 361 | * @param string|NodeList|\DOMNode $input |
||
| 362 | * |
||
| 363 | * @return NodeList |
||
| 364 | */ |
||
| 365 | public function add($input): NodeList { |
||
| 374 | |||
| 375 | /** @var int */ |
||
| 376 | private static $MATCH_TYPE_FIRST = 1; |
||
| 377 | |||
| 378 | /** @var int */ |
||
| 379 | private static $MATCH_TYPE_LAST = 2; |
||
| 380 | |||
| 381 | /** |
||
| 382 | * @param \DOMNode $baseNode |
||
| 383 | * @param string $property |
||
| 384 | * @param string|NodeList|\DOMNode|callable $input |
||
| 385 | * @param string|NodeList|\DOMNode|callable $selector |
||
| 386 | * @param int $matchType |
||
| 387 | * |
||
| 388 | * @return NodeList |
||
| 389 | */ |
||
| 390 | 65 | protected function _buildNodeListUntil(\DOMNode $baseNode, string $property, $input = null, $selector = null, int $matchType = null): NodeList { |
|
| 419 | |||
| 420 | /** |
||
| 421 | * @param iterable $nodeLists |
||
| 422 | * |
||
| 423 | * @return NodeList |
||
| 424 | */ |
||
| 425 | 65 | protected function _uniqueNodes(iterable $nodeLists): NodeList { |
|
| 442 | |||
| 443 | /** |
||
| 444 | * @param string $property |
||
| 445 | * @param string|NodeList|\DOMNode|callable $input |
||
| 446 | * @param string|NodeList|\DOMNode|callable $selector |
||
| 447 | * @param int $matchType |
||
| 448 | * |
||
| 449 | * @return NodeList |
||
| 450 | */ |
||
| 451 | 65 | protected function _walkPathUntil(string $property, $input = null, $selector = null, int $matchType = null): NodeList { |
|
| 460 | } |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.