Complex classes like ManipulationTrait 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 ManipulationTrait, and based on these observations, apply Extract Interface, too.
| 1 | <?php declare(strict_types=1); |
||
| 17 | trait ManipulationTrait |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * Magic method - Trap function names using reserved keyword (empty, clone, etc..) |
||
| 21 | * |
||
| 22 | * @param string $name |
||
| 23 | * @param array $arguments |
||
| 24 | * |
||
| 25 | * @return mixed |
||
| 26 | */ |
||
| 27 | 8 | public function __call(string $name, array $arguments) { |
|
| 34 | |||
| 35 | /** |
||
| 36 | * @param string|NodeList|\DOMNode $input |
||
| 37 | * |
||
| 38 | * @return iterable |
||
| 39 | */ |
||
| 40 | 61 | protected function inputPrepareAsTraversable($input): iterable { |
|
| 53 | |||
| 54 | /** |
||
| 55 | * @param string|NodeList|\DOMNode $input |
||
| 56 | * |
||
| 57 | * @return NodeList |
||
| 58 | */ |
||
| 59 | 61 | protected function inputAsNodeList($input): NodeList { |
|
| 74 | |||
| 75 | /** |
||
| 76 | * @param string|NodeList|\DOMNode $input |
||
| 77 | * |
||
| 78 | * @return \DOMNode|null |
||
| 79 | */ |
||
| 80 | 22 | protected function inputAsFirstNode($input): ?\DOMNode { |
|
| 85 | |||
| 86 | /** |
||
| 87 | * @param string $html |
||
| 88 | * |
||
| 89 | * @return NodeList |
||
| 90 | */ |
||
| 91 | 45 | protected function nodesFromHtml($html): NodeList { |
|
| 98 | |||
| 99 | /** |
||
| 100 | * @param string|NodeList|\DOMNode|callable $input |
||
| 101 | * @param callable $callback |
||
| 102 | * |
||
| 103 | * @return self |
||
| 104 | */ |
||
| 105 | protected function manipulateNodesWithInput($input, callable $callback): self { |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @param string|null $selector |
||
| 123 | * |
||
| 124 | * @return NodeList |
||
| 125 | */ |
||
| 126 | 40 | public function detach(string $selector = null): NodeList { |
|
| 145 | |||
| 146 | /** |
||
| 147 | * @param string|null $selector |
||
| 148 | * |
||
| 149 | * @return self |
||
| 150 | */ |
||
| 151 | 35 | public function remove(string $selector = null): self { |
|
| 156 | |||
| 157 | /** |
||
| 158 | * @param string|NodeList|\DOMNode|callable $input |
||
| 159 | * |
||
| 160 | * @return self |
||
| 161 | */ |
||
| 162 | public function replaceWith($input): self { |
||
| 171 | |||
| 172 | /** |
||
| 173 | * @param string|NodeList|\DOMNode|callable $input |
||
| 174 | * |
||
| 175 | * @return string|self |
||
| 176 | */ |
||
| 177 | 14 | public function text($input = null) { |
|
| 184 | |||
| 185 | /** |
||
| 186 | * @return string |
||
| 187 | */ |
||
| 188 | public function getText(): string { |
||
| 193 | |||
| 194 | /** |
||
| 195 | * @param string|NodeList|\DOMNode|callable $input |
||
| 196 | * |
||
| 197 | * @return self |
||
| 198 | */ |
||
| 199 | 5 | public function setText($input): self { |
|
| 214 | |||
| 215 | /** |
||
| 216 | * @param string|NodeList|\DOMNode|callable $input |
||
| 217 | * |
||
| 218 | * @return self |
||
| 219 | */ |
||
| 220 | public function before($input): self { |
||
| 229 | |||
| 230 | /** |
||
| 231 | * @param string|NodeList|\DOMNode|callable $input |
||
| 232 | * |
||
| 233 | * @return self |
||
| 234 | */ |
||
| 235 | public function after($input): self { |
||
| 248 | |||
| 249 | /** |
||
| 250 | * @param string|NodeList|\DOMNode|callable $input |
||
| 251 | * |
||
| 252 | * @return self |
||
| 253 | */ |
||
| 254 | public function prepend($input): self { |
||
| 263 | |||
| 264 | /** |
||
| 265 | * @param string|NodeList|\DOMNode|callable $input |
||
| 266 | * |
||
| 267 | * @return self |
||
| 268 | */ |
||
| 269 | public function append($input): self { |
||
| 278 | |||
| 279 | /** |
||
| 280 | * @return self |
||
| 281 | */ |
||
| 282 | public function _empty(): self { |
||
| 289 | |||
| 290 | /** |
||
| 291 | * @return NodeList|\DOMNode |
||
| 292 | */ |
||
| 293 | 3 | public function _clone() { |
|
| 302 | |||
| 303 | /** |
||
| 304 | * @param string $name |
||
| 305 | * |
||
| 306 | * @return self |
||
| 307 | */ |
||
| 308 | public function removeAttr(string $name): self { |
||
| 317 | |||
| 318 | /** |
||
| 319 | * @param string $name |
||
| 320 | * |
||
| 321 | * @return bool |
||
| 322 | */ |
||
| 323 | public function hasAttr(string $name): bool { |
||
| 332 | |||
| 333 | /** |
||
| 334 | * @internal |
||
| 335 | * |
||
| 336 | * @param string $name |
||
| 337 | * |
||
| 338 | * @return string|null |
||
| 339 | */ |
||
| 340 | 18 | public function getAttr(string $name): ?string { |
|
| 355 | |||
| 356 | /** |
||
| 357 | * @internal |
||
| 358 | * |
||
| 359 | * @param string $name |
||
| 360 | * @param string $value |
||
| 361 | * |
||
| 362 | * @return self |
||
| 363 | */ |
||
| 364 | public function setAttr(string $name, string $value): self { |
||
| 373 | |||
| 374 | /** |
||
| 375 | * @param string $name |
||
| 376 | * @param string|null $value |
||
| 377 | * |
||
| 378 | * @return self|mixed |
||
| 379 | */ |
||
| 380 | 17 | public function attr(string $name, string $value = null) { |
|
| 387 | |||
| 388 | /** |
||
| 389 | * @internal |
||
| 390 | * |
||
| 391 | * @param string $name |
||
| 392 | * @param string|callable $value |
||
| 393 | * @param bool $addValue |
||
| 394 | */ |
||
| 395 | protected function _pushAttrValue(string $name, $value, bool $addValue = false): void { |
||
| 428 | |||
| 429 | /** |
||
| 430 | * @param string|callable $class |
||
| 431 | * |
||
| 432 | * @return self |
||
| 433 | */ |
||
| 434 | 7 | public function addClass($class): self { |
|
| 439 | |||
| 440 | /** |
||
| 441 | * @param string|callable $class |
||
| 442 | * |
||
| 443 | * @return self |
||
| 444 | */ |
||
| 445 | 8 | public function removeClass($class): self { |
|
| 450 | |||
| 451 | /** |
||
| 452 | * @param string $class |
||
| 453 | * |
||
| 454 | * @return bool |
||
| 455 | */ |
||
| 456 | public function hasClass(string $class): bool { |
||
| 469 | |||
| 470 | /** |
||
| 471 | * @param Element $node |
||
| 472 | * |
||
| 473 | * @return \SplStack |
||
| 474 | */ |
||
| 475 | 21 | protected function _getFirstChildWrapStack(Element $node): \SplStack { |
|
| 489 | |||
| 490 | /** |
||
| 491 | * @param Element $node |
||
| 492 | * |
||
| 493 | * @return \SplStack |
||
| 494 | */ |
||
| 495 | 21 | protected function _prepareWrapStack(Element $node): \SplStack { |
|
| 507 | |||
| 508 | /** |
||
| 509 | * @param string|NodeList|\DOMNode|callable $input |
||
| 510 | * @param callable $callback |
||
| 511 | */ |
||
| 512 | protected function wrapWithInputByCallback($input, callable $callback): void { |
||
| 530 | |||
| 531 | /** |
||
| 532 | * @param string|NodeList|\DOMNode|callable $input |
||
| 533 | * |
||
| 534 | * @return self |
||
| 535 | */ |
||
| 536 | public function wrapInner($input): self { |
||
| 552 | |||
| 553 | /** |
||
| 554 | * @param string|NodeList|\DOMNode|callable $input |
||
| 555 | * |
||
| 556 | * @return self |
||
| 557 | */ |
||
| 558 | public function wrap($input): self { |
||
| 572 | |||
| 573 | /** |
||
| 574 | * @param string|NodeList|\DOMNode|callable $input |
||
| 575 | * |
||
| 576 | * @return self |
||
| 577 | */ |
||
| 578 | 7 | public function wrapAll($input): self { |
|
| 605 | |||
| 606 | /** |
||
| 607 | * @return self |
||
| 608 | */ |
||
| 609 | public function unwrap(): self { |
||
| 625 | |||
| 626 | /** |
||
| 627 | * @return string |
||
| 628 | */ |
||
| 629 | 2 | public function getOuterHtml(): string { |
|
| 634 | |||
| 635 | /** |
||
| 636 | * @return string |
||
| 637 | */ |
||
| 638 | public function getHtml(): string { |
||
| 643 | |||
| 644 | /** |
||
| 645 | * @param string|NodeList|\DOMNode|callable $input |
||
| 646 | * |
||
| 647 | * @return self |
||
| 648 | */ |
||
| 649 | public function setHtml($input): self { |
||
| 660 | |||
| 661 | /** |
||
| 662 | * @param string|NodeList|\DOMNode|callable $input |
||
| 663 | * |
||
| 664 | * @return string|self |
||
| 665 | */ |
||
| 666 | 140 | public function html($input = null) { |
|
| 673 | } |
If you implement
__calland you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__callis implemented by a parent class and only the child class knows which methods exist: