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 | 81 | public function __construct(\DOMNode $node) |
|
21 | |||
22 | /** |
||
23 | * @return bool |
||
24 | */ |
||
25 | 9 | 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 | 81 | public function getTagName() |
|
73 | |||
74 | /** |
||
75 | * @return string |
||
76 | */ |
||
77 | 66 | public function getValue() |
|
81 | |||
82 | /** |
||
83 | * @return ElementInterface|null |
||
84 | */ |
||
85 | 6 | public function getParent() |
|
89 | |||
90 | /** |
||
91 | * @return bool |
||
92 | */ |
||
93 | 81 | public function hasChildren() |
|
97 | |||
98 | /** |
||
99 | * @return ElementInterface[] |
||
100 | */ |
||
101 | 81 | public function getChildren() |
|
111 | |||
112 | /** |
||
113 | * @return ElementInterface|null |
||
114 | */ |
||
115 | 9 | public function getNext() |
|
126 | |||
127 | /** |
||
128 | * @param \DomNode $node |
||
129 | * @param bool $checkChildren |
||
130 | * |
||
131 | * @return \DomNode|null |
||
132 | */ |
||
133 | 9 | private function getNextNode($node, $checkChildren = true) |
|
134 | { |
||
135 | 9 | if ($checkChildren && $node->firstChild) { |
|
136 | return $node->firstChild; |
||
137 | } |
||
138 | |||
139 | 9 | if ($node->nextSibling) { |
|
140 | 9 | return $node->nextSibling; |
|
141 | } |
||
142 | |||
143 | 3 | if ($node->parentNode) { |
|
144 | 3 | return $this->getNextNode($node->parentNode, false); |
|
145 | } |
||
146 | } |
||
147 | |||
148 | /** |
||
149 | * @param string[]|string $tagNames |
||
150 | * |
||
151 | * @return bool |
||
152 | */ |
||
153 | 81 | public function isDescendantOf($tagNames) |
|
171 | |||
172 | /** |
||
173 | * @param string $markdown |
||
174 | */ |
||
175 | 81 | public function setFinalMarkdown($markdown) |
|
180 | |||
181 | /** |
||
182 | * @return string |
||
183 | */ |
||
184 | 72 | public function getChildrenAsString() |
|
188 | |||
189 | /** |
||
190 | * @return int |
||
191 | */ |
||
192 | 6 | public function getSiblingPosition() |
|
211 | |||
212 | /** |
||
213 | * @return int |
||
214 | */ |
||
215 | 6 | public function getListItemLevel() |
|
229 | |||
230 | /** |
||
231 | * @param string $name |
||
232 | * |
||
233 | * @return string |
||
234 | */ |
||
235 | 24 | public function getAttribute($name) |
|
243 | |||
244 | /** |
||
245 | * @param ElementInterface $element |
||
246 | * |
||
247 | * @return bool |
||
248 | */ |
||
249 | 6 | public function equals(ElementInterface $element) |
|
257 | } |
||
258 |