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 | 66 | public function __construct(\DOMNode $node) |
|
| 21 | |||
| 22 | /** |
||
| 23 | * @return bool |
||
| 24 | */ |
||
| 25 | 12 | public function isBlock() |
|
| 49 | |||
| 50 | /** |
||
| 51 | * @return bool |
||
| 52 | */ |
||
| 53 | public function isText() |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @return bool |
||
| 60 | */ |
||
| 61 | 6 | public function isWhitespace() |
|
| 65 | |||
| 66 | /** |
||
| 67 | * @return string |
||
| 68 | */ |
||
| 69 | 66 | public function getTagName() |
|
| 73 | |||
| 74 | /** |
||
| 75 | * @return string |
||
| 76 | */ |
||
| 77 | 54 | public function getValue() |
|
| 81 | |||
| 82 | /** |
||
| 83 | * @return ElementInterface|null |
||
| 84 | */ |
||
| 85 | 6 | public function getParent() |
|
| 89 | |||
| 90 | /** |
||
| 91 | * @return bool |
||
| 92 | */ |
||
| 93 | 66 | public function hasChildren() |
|
| 97 | |||
| 98 | /** |
||
| 99 | * @return ElementInterface[] |
||
| 100 | */ |
||
| 101 | 66 | public function getChildren() |
|
| 111 | |||
| 112 | /** |
||
| 113 | * @return ElementInterface|null |
||
| 114 | */ |
||
| 115 | 12 | public function getNext() |
|
| 126 | |||
| 127 | /** |
||
| 128 | * @param \DomNode $node |
||
| 129 | * |
||
| 130 | * @return \DomNode|null |
||
| 131 | */ |
||
| 132 | 12 | private function getNextNode($node, $checkChildren = true) |
|
| 142 | |||
| 143 | /** |
||
| 144 | * @param string[]|string $tagNames |
||
| 145 | * |
||
| 146 | * @return bool |
||
| 147 | */ |
||
| 148 | 66 | public function isDescendantOf($tagNames) |
|
| 166 | |||
| 167 | /** |
||
| 168 | * @param string $markdown |
||
| 169 | */ |
||
| 170 | 66 | public function setFinalMarkdown($markdown) |
|
| 175 | |||
| 176 | /** |
||
| 177 | * @return string |
||
| 178 | */ |
||
| 179 | 60 | public function getChildrenAsString() |
|
| 183 | |||
| 184 | /** |
||
| 185 | * @return int |
||
| 186 | */ |
||
| 187 | 6 | public function getSiblingPosition() |
|
| 206 | |||
| 207 | /** |
||
| 208 | * @return int |
||
| 209 | */ |
||
| 210 | 6 | public function getListItemLevel() |
|
| 224 | |||
| 225 | /** |
||
| 226 | * @param string $name |
||
| 227 | * |
||
| 228 | * @return string |
||
| 229 | */ |
||
| 230 | 6 | public function getAttribute($name) |
|
| 238 | |||
| 239 | /** |
||
| 240 | * @param ElementInterface $element |
||
| 241 | * |
||
| 242 | * @return bool |
||
| 243 | */ |
||
| 244 | 6 | public function equals(ElementInterface $element) |
|
| 252 | } |
||
| 253 |