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() { |
||
| 14 | |||
| 15 | public function current() { |
||
| 18 | |||
| 19 | public function key() { |
||
| 22 | |||
| 23 | public function next() { |
||
| 26 | |||
| 27 | public function valid() { |
||
| 31 | |||
| 32 | public function parse($data) { |
||
| 50 | |||
| 51 | protected function isElementArray($value) { |
||
| 54 | |||
| 55 | protected function isElementValue($value) { |
||
| 58 | |||
| 59 | protected function parseNameSpace($data) { |
||
| 70 | |||
| 71 | protected function convertAttributes($attributes) { |
||
| 78 | |||
| 79 | protected function convertValue($value) { |
||
| 94 | |||
| 95 | public function children() { |
||
| 106 | |||
| 107 | protected function filterChildren($array) { |
||
| 117 | |||
| 118 | public function hasChildren() { |
||
| 121 | |||
| 122 | public function findFirst($search) { |
||
| 125 | |||
| 126 | public function find($search) { |
||
| 145 | |||
| 146 | protected function findAttribute($search) { |
||
| 155 | |||
| 156 | public function __get($name) { |
||
| 166 | |||
| 167 | public function __debugInfo() { |
||
| 187 | |||
| 188 | /* Very specific type of integer checking to ensure |
||
| 189 | that we have a number value, and not one that has been |
||
| 190 | mistakenly casted by PHP. Examples below. |
||
| 191 | |||
| 192 | var_dump(isInteger(23)); //bool(true) |
||
| 193 | var_dump(isInteger("23")); //bool(true) |
||
| 194 | var_dump(isInteger(23.5)); //bool(false) |
||
| 195 | var_dump(isInteger(NULL)); //bool(false) |
||
| 196 | var_dump(isInteger("")); //bool(false) |
||
| 197 | */ |
||
| 198 | protected function isInteger($input) { |
||
| 201 | |||
| 202 | /* Very specific type of boolean checking to ensure |
||
| 203 | that we have a bool value, and not one that has been |
||
| 204 | mistakenly casted by PHP. Examples below. |
||
| 205 | |||
| 206 | var_dump(isBool(true)); //bool(true) |
||
| 207 | var_dump(isBool("false")); //bool(true) |
||
| 208 | var_dump(isBool(0)); //bool(false) |
||
| 209 | var_dump(isBool(NULL)); //bool(false) |
||
| 210 | var_dump(isBool("")); //bool(false) |
||
| 211 | */ |
||
| 212 | protected function isBool($input) { |
||
| 215 | } |
||
| 216 |