Complex classes like XMLReaderElement 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 XMLReaderElement, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
4 | class XMLReaderElement implements \Iterator { |
||
5 | |||
6 | protected $namespace; |
||
7 | protected $name; |
||
8 | protected $attributes; |
||
9 | protected $value; |
||
10 | |||
11 | public function rewind() |
||
15 | |||
16 | public function current() |
||
20 | |||
21 | public function key() |
||
25 | |||
26 | public function next() |
||
30 | |||
31 | public function valid() |
||
36 | |||
37 | public function parse($data) |
||
56 | |||
57 | protected function isElementArray($value) { |
||
60 | |||
61 | protected function isElementValue($value) { |
||
64 | |||
65 | protected function parseNameSpace($data) { |
||
76 | |||
77 | protected function parseAttributes($data) { |
||
80 | |||
81 | protected function convertAttributes($attributes) |
||
90 | |||
91 | protected function convertValue($value) { |
||
106 | |||
107 | /* Very specific type of integer checking to ensure |
||
108 | that we have a number value, and not one that has been |
||
109 | mistakenly casted by PHP. Examples below. |
||
110 | |||
111 | var_dump(isInteger(23)); //bool(true) |
||
112 | var_dump(isInteger("23")); //bool(true) |
||
113 | var_dump(isInteger(23.5)); //bool(false) |
||
114 | var_dump(isInteger(NULL)); //bool(false) |
||
115 | var_dump(isInteger("")); //bool(false) |
||
116 | */ |
||
117 | protected function isInteger($input) |
||
121 | |||
122 | /* Very specific type of boolean checking to ensure |
||
123 | that we have a bool value, and not one that has been |
||
124 | mistakenly casted by PHP. Examples below. |
||
125 | |||
126 | var_dump(isBool(true)); //bool(true) |
||
127 | var_dump(isBool("false")); //bool(true) |
||
128 | var_dump(isBool(0)); //bool(false) |
||
129 | var_dump(isBool(NULL)); //bool(false) |
||
130 | var_dump(isBool("")); //bool(false) |
||
131 | */ |
||
132 | protected function isBool($input) |
||
136 | |||
137 | public function children() |
||
156 | |||
157 | public function findFirst($search) |
||
161 | |||
162 | public function find($search) { |
||
181 | |||
182 | public function __get($name) |
||
195 | |||
196 | public function __debugInfo() { |
||
221 | } |
||
222 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.