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 | 72 | public function __construct(\DOMNode $node) |
|
18 | { |
||
19 | 72 | $this->node = $node; |
|
20 | 72 | } |
|
21 | |||
22 | /** |
||
23 | * @return bool |
||
24 | */ |
||
25 | public function isBlock() |
||
26 | { |
||
27 | switch ($this->getTagName()) { |
||
28 | case 'blockquote': |
||
29 | case 'body': |
||
30 | case 'code': |
||
31 | case 'div': |
||
32 | case 'h1': |
||
33 | case 'h2': |
||
34 | case 'h3': |
||
35 | case 'h4': |
||
36 | case 'h5': |
||
37 | case 'h6': |
||
38 | case 'hr': |
||
39 | case 'html': |
||
40 | case 'li': |
||
41 | case 'p': |
||
42 | case 'ol': |
||
43 | case 'ul': |
||
44 | return true; |
||
45 | default: |
||
46 | return false; |
||
47 | } |
||
48 | } |
||
49 | |||
50 | /** |
||
51 | * @return bool |
||
52 | */ |
||
53 | public function isText() |
||
54 | { |
||
55 | return $this->getTagName() === '#text'; |
||
56 | } |
||
57 | |||
58 | /** |
||
59 | * @return bool |
||
60 | */ |
||
61 | 6 | public function isWhitespace() |
|
62 | { |
||
63 | 6 | return $this->getTagName() === '#text' && trim($this->getValue()) === ''; |
|
64 | } |
||
65 | |||
66 | /** |
||
67 | * @return string |
||
68 | */ |
||
69 | 72 | public function getTagName() |
|
70 | { |
||
71 | 72 | return $this->node->nodeName; |
|
72 | } |
||
73 | |||
74 | /** |
||
75 | * @return string |
||
76 | */ |
||
77 | 54 | public function getValue() |
|
78 | { |
||
79 | 54 | return $this->node->nodeValue; |
|
80 | } |
||
81 | |||
82 | /** |
||
83 | * @return ElementInterface|null |
||
84 | */ |
||
85 | 6 | public function getParent() |
|
86 | { |
||
87 | 6 | return new static($this->node->parentNode) ?: null; |
|
88 | } |
||
89 | |||
90 | /** |
||
91 | * @return bool |
||
92 | */ |
||
93 | 72 | public function hasChildren() |
|
94 | { |
||
95 | 72 | return $this->node->hasChildNodes(); |
|
96 | } |
||
97 | |||
98 | /** |
||
99 | * @return ElementInterface[] |
||
100 | */ |
||
101 | 72 | public function getChildren() |
|
102 | { |
||
103 | 72 | $ret = array(); |
|
104 | /** @var \DOMNode $node */ |
||
105 | 72 | foreach ($this->node->childNodes as $node) { |
|
106 | 72 | $ret[] = new static($node); |
|
107 | 72 | } |
|
108 | |||
109 | 72 | return $ret; |
|
110 | } |
||
111 | |||
112 | /** |
||
113 | * @return ElementInterface|null |
||
114 | */ |
||
115 | public function getNext() |
||
116 | { |
||
117 | if ($this->nextCached === null) { |
||
118 | $nextNode = $this->getNextNode($this->node); |
||
119 | if ($nextNode !== null) { |
||
120 | $this->nextCached = new static($nextNode); |
||
121 | } |
||
122 | } |
||
123 | |||
124 | return $this->nextCached; |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * @param \DomNode $node |
||
129 | * |
||
130 | * @return \DomNode|null |
||
131 | */ |
||
132 | private function getNextNode($node, $checkChildren = true) |
||
133 | { |
||
134 | if ($checkChildren && $node->firstChild) { |
||
135 | return $node->firstChild; |
||
136 | } elseif ($node->nextSibling) { |
||
137 | return $node->nextSibling; |
||
138 | } elseif ($node->parentNode) { |
||
139 | return $this->getNextNode($node->parentNode, false); |
||
140 | } |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * @param string[]|string $tagNames |
||
145 | * |
||
146 | * @return bool |
||
147 | */ |
||
148 | 72 | public function isDescendantOf($tagNames) |
|
149 | { |
||
150 | 72 | if (!is_array($tagNames)) { |
|
151 | $tagNames = array($tagNames); |
||
152 | } |
||
153 | |||
154 | 72 | for ($p = $this->node->parentNode; $p !== false; $p = $p->parentNode) { |
|
155 | 72 | if (is_null($p)) { |
|
156 | 72 | return false; |
|
157 | } |
||
158 | |||
159 | 72 | if (in_array($p->nodeName, $tagNames)) { |
|
160 | 12 | return true; |
|
161 | } |
||
162 | 72 | } |
|
163 | |||
164 | return false; |
||
165 | } |
||
166 | |||
167 | /** |
||
168 | * @param string $markdown |
||
169 | */ |
||
170 | 72 | public function setFinalMarkdown($markdown) |
|
171 | { |
||
172 | 72 | $markdown_node = $this->node->ownerDocument->createTextNode($markdown); |
|
173 | 72 | $this->node->parentNode->replaceChild($markdown_node, $this->node); |
|
174 | 72 | } |
|
175 | |||
176 | /** |
||
177 | * @return string |
||
178 | */ |
||
179 | 63 | public function getChildrenAsString() |
|
180 | { |
||
181 | 63 | return $this->node->C14N(); |
|
182 | } |
||
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) |
|
231 | { |
||
232 | 6 | if ($this->node instanceof \DOMElement) { |
|
233 | 6 | return $this->node->getAttribute($name); |
|
234 | } |
||
235 | |||
236 | return ''; |
||
237 | } |
||
238 | |||
239 | /** |
||
240 | * @param ElementInterface $element |
||
241 | * |
||
242 | * @return bool |
||
243 | */ |
||
244 | 6 | public function equals(ElementInterface $element) |
|
252 | } |
||
253 |