Complex classes like XMLReader 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 XMLReader, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | class XMLReader |
||
9 | { |
||
10 | /** |
||
11 | * XML Reader |
||
12 | * |
||
13 | * @var \XMLReader |
||
14 | */ |
||
15 | private $reader; |
||
16 | |||
17 | /** |
||
18 | * XML document |
||
19 | * |
||
20 | * @var \Inspirum\XML\Services\XML |
||
21 | */ |
||
22 | private $xml; |
||
23 | |||
24 | /** |
||
25 | * XMLReader constructor |
||
26 | * |
||
27 | * @param string $filepath |
||
28 | */ |
||
29 | 17 | public function __construct(string $filepath) |
|
34 | |||
35 | /** |
||
36 | * XMLReader destructor |
||
37 | */ |
||
38 | 13 | public function __destruct() |
|
42 | |||
43 | /** |
||
44 | * Open file |
||
45 | * |
||
46 | * @param string $filepath |
||
47 | * |
||
48 | * @return \XMLReader |
||
49 | * |
||
50 | * @throws \Exception |
||
51 | */ |
||
52 | 17 | private function open(string $filepath): BaseXMLReader |
|
68 | |||
69 | /** |
||
70 | * Parse file and yield next node |
||
71 | * |
||
72 | * @param string $nodeName |
||
73 | * |
||
74 | * @return \Generator|\Inspirum\XML\Services\XMLNode[] |
||
75 | */ |
||
76 | 7 | public function iterateNode(string $nodeName): iterable |
|
92 | |||
93 | /** |
||
94 | * Get next node |
||
95 | * |
||
96 | * @param string $nodeName |
||
97 | * |
||
98 | * @return \Inspirum\XML\Services\XMLNode|null |
||
99 | */ |
||
100 | 8 | public function nextNode(string $nodeName): ?XMLNode |
|
110 | |||
111 | /** |
||
112 | * Move to first element by tag name |
||
113 | * |
||
114 | * @param string $nodeName |
||
115 | * |
||
116 | * @return bool |
||
117 | */ |
||
118 | 14 | private function moveToNode(string $nodeName): bool |
|
128 | |||
129 | /** |
||
130 | * Move to next sibling element by tag name |
||
131 | * |
||
132 | * @param string $nodeName |
||
133 | * |
||
134 | * @return bool |
||
135 | */ |
||
136 | 5 | private function moveToNextNode(string $nodeName): bool |
|
152 | |||
153 | /** |
||
154 | * Return associative array of element by name |
||
155 | * |
||
156 | * @return \Inspirum\XML\Services\XMLNode|null |
||
157 | */ |
||
158 | 10 | private function readNode(): ?XMLNode |
|
198 | |||
199 | /** |
||
200 | * Move to next node in document |
||
201 | * |
||
202 | * @return bool |
||
203 | * |
||
204 | * @throws \DOMException |
||
205 | */ |
||
206 | 14 | private function read(): bool |
|
212 | |||
213 | /** |
||
214 | * Get current node name |
||
215 | * |
||
216 | * @return string |
||
217 | */ |
||
218 | 12 | private function getNodeName(): string |
|
222 | |||
223 | /** |
||
224 | * Get current node type |
||
225 | * |
||
226 | * @return int |
||
227 | */ |
||
228 | 12 | private function getNodeType(): int |
|
232 | |||
233 | /** |
||
234 | * Get current node value |
||
235 | * |
||
236 | * @return string |
||
237 | */ |
||
238 | 10 | private function getNodeValue(): string |
|
242 | |||
243 | /** |
||
244 | * If current node is element open tag |
||
245 | * |
||
246 | * @return bool |
||
247 | */ |
||
248 | 12 | private function isNodeElementType(): bool |
|
252 | |||
253 | /** |
||
254 | * If current node is element open tag |
||
255 | * |
||
256 | * @return bool |
||
257 | */ |
||
258 | 10 | private function isNodeEmptyElementType(): bool |
|
262 | |||
263 | /** |
||
264 | * If current node is element close tag |
||
265 | * |
||
266 | * @return bool |
||
267 | */ |
||
268 | 9 | private function isNodeElementEndType(): bool |
|
272 | |||
273 | /** |
||
274 | * If current node is text content |
||
275 | * |
||
276 | * @return bool |
||
277 | */ |
||
278 | 8 | private function isNodeTextType(): bool |
|
282 | |||
283 | /** |
||
284 | * If current node is given node type |
||
285 | * |
||
286 | * @param int $type |
||
287 | * |
||
288 | * @return bool |
||
289 | */ |
||
290 | 12 | private function isNodeType(int $type): bool |
|
294 | |||
295 | /** |
||
296 | * Get current node attributes |
||
297 | * |
||
298 | * @return array<string,string> |
||
299 | */ |
||
300 | 10 | private function getNodeAttributes(): array |
|
313 | |||
314 | /** |
||
315 | * Register custom error handler to throw Exception on warning message |
||
316 | * |
||
317 | * @param callable $callback |
||
318 | * |
||
319 | * @return mixed |
||
320 | * |
||
321 | * @throws \DOMException |
||
322 | */ |
||
323 | 17 | protected function withErrorHandler(callable $callback) |
|
339 | } |
||
340 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.