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) |
||
| 67 | |||
| 68 | protected function convertAttributes($attributes) |
||
| 77 | |||
| 78 | protected function convertValue($value) { |
||
| 93 | |||
| 94 | /* Very specific type of integer checking to ensure |
||
| 95 | that we have a number value, and not one that has been |
||
| 96 | mistakenly casted by PHP. Examples below. |
||
| 97 | |||
| 98 | var_dump(isInteger(23)); //bool(true) |
||
| 99 | var_dump(isInteger("23")); //bool(true) |
||
| 100 | var_dump(isInteger(23.5)); //bool(false) |
||
| 101 | var_dump(isInteger(NULL)); //bool(false) |
||
| 102 | var_dump(isInteger("")); //bool(false) |
||
| 103 | */ |
||
| 104 | protected function isInteger($input) |
||
| 108 | |||
| 109 | /* Very specific type of boolean checking to ensure |
||
| 110 | that we have a bool value, and not one that has been |
||
| 111 | mistakenly casted by PHP. Examples below. |
||
| 112 | |||
| 113 | var_dump(isBool(true)); //bool(true) |
||
| 114 | var_dump(isBool("false")); //bool(true) |
||
| 115 | var_dump(isBool(0)); //bool(false) |
||
| 116 | var_dump(isBool(NULL)); //bool(false) |
||
| 117 | var_dump(isBool("")); //bool(false) |
||
| 118 | */ |
||
| 119 | protected function isBool($input) |
||
| 123 | |||
| 124 | public function children() |
||
| 143 | |||
| 144 | public function findFirst($search) |
||
| 148 | |||
| 149 | public function find($search) { |
||
| 168 | |||
| 169 | public function __get($name) |
||
| 182 | |||
| 183 | public function __debugInfo() { |
||
| 208 | } |
||
| 209 |