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 | 87 | public function __construct(\DOMNode $node) |
|
21 | |||
22 | /** |
||
23 | * @return bool |
||
24 | */ |
||
25 | 12 | public function isBlock() |
|
48 | |||
49 | /** |
||
50 | * @return bool |
||
51 | */ |
||
52 | public function isText() |
||
56 | |||
57 | /** |
||
58 | * @return bool |
||
59 | */ |
||
60 | 6 | public function isWhitespace() |
|
64 | |||
65 | /** |
||
66 | * @return string |
||
67 | */ |
||
68 | 87 | public function getTagName() |
|
72 | |||
73 | /** |
||
74 | * @return string |
||
75 | */ |
||
76 | 75 | public function getValue() |
|
80 | |||
81 | /** |
||
82 | * @return ElementInterface|null |
||
83 | */ |
||
84 | 72 | public function getParent() |
|
88 | |||
89 | /** |
||
90 | * @return bool |
||
91 | */ |
||
92 | 87 | public function hasChildren() |
|
96 | |||
97 | /** |
||
98 | * @return ElementInterface[] |
||
99 | */ |
||
100 | 87 | public function getChildren() |
|
110 | |||
111 | /** |
||
112 | * @return ElementInterface|null |
||
113 | */ |
||
114 | 18 | public function getNext() |
|
125 | |||
126 | /** |
||
127 | * @param \DomNode $node |
||
128 | * @param bool $checkChildren |
||
129 | * |
||
130 | * @return \DomNode|null |
||
131 | */ |
||
132 | 18 | private function getNextNode($node, $checkChildren = true) |
|
146 | |||
147 | /** |
||
148 | * @param string[]|string $tagNames |
||
149 | * |
||
150 | * @return bool |
||
151 | */ |
||
152 | 87 | public function isDescendantOf($tagNames) |
|
170 | |||
171 | /** |
||
172 | * @param string $markdown |
||
173 | */ |
||
174 | 87 | public function setFinalMarkdown($markdown) |
|
179 | |||
180 | /** |
||
181 | * @return string |
||
182 | */ |
||
183 | 78 | public function getChildrenAsString() |
|
187 | |||
188 | /** |
||
189 | * @return int |
||
190 | */ |
||
191 | 6 | public function getSiblingPosition() |
|
210 | |||
211 | /** |
||
212 | * @return int |
||
213 | */ |
||
214 | 9 | public function getListItemLevel() |
|
228 | |||
229 | /** |
||
230 | * @param string $name |
||
231 | * |
||
232 | * @return string |
||
233 | */ |
||
234 | 27 | public function getAttribute($name) |
|
242 | |||
243 | /** |
||
244 | * @param ElementInterface $element |
||
245 | * |
||
246 | * @return bool |
||
247 | */ |
||
248 | 6 | public function equals(ElementInterface $element) |
|
256 | } |
||
257 |