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 convertAttributes($attributes) |
||
| 78 | { |
||
| 79 | foreach($attributes as $k=>$attribute) |
||
| 80 | { |
||
| 81 | $attributes[$k] = $this->convertValue($attribute); |
||
| 82 | } |
||
| 83 | |||
| 84 | return $attributes; |
||
| 85 | } |
||
| 86 | |||
| 87 | protected function convertValue($value) { |
||
| 88 | if (is_string($value)) |
||
| 89 | { |
||
| 90 | if ($this->isBool($value)) |
||
| 91 | { |
||
| 92 | return filter_var($value, FILTER_VALIDATE_BOOLEAN); |
||
| 93 | } |
||
| 94 | |||
| 95 | if ($this->isInteger($value)) |
||
| 96 | { |
||
| 97 | return (int) $value; |
||
| 98 | } |
||
| 99 | } |
||
| 100 | return $value; |
||
| 101 | } |
||
| 102 | |||
| 103 | /* Very specific type of integer checking to ensure |
||
| 104 | that we have a number value, and not one that has been |
||
| 105 | mistakenly casted by PHP. Examples below. |
||
| 106 | |||
| 107 | var_dump(isInteger(23)); //bool(true) |
||
| 108 | var_dump(isInteger("23")); //bool(true) |
||
| 109 | var_dump(isInteger(23.5)); //bool(false) |
||
| 110 | var_dump(isInteger(NULL)); //bool(false) |
||
| 111 | var_dump(isInteger("")); //bool(false) |
||
| 112 | */ |
||
| 113 | protected function isInteger($input) |
||
| 114 | { |
||
| 115 | return(ctype_digit(strval($input))); |
||
| 116 | } |
||
| 117 | |||
| 118 | /* Very specific type of boolean checking to ensure |
||
| 119 | that we have a bool value, and not one that has been |
||
| 120 | mistakenly casted by PHP. Examples below. |
||
| 121 | |||
| 122 | var_dump(isBool(true)); //bool(true) |
||
| 123 | var_dump(isBool("false")); //bool(true) |
||
| 124 | var_dump(isBool(0)); //bool(false) |
||
| 125 | var_dump(isBool(NULL)); //bool(false) |
||
| 126 | var_dump(isBool("")); //bool(false) |
||
| 127 | */ |
||
| 128 | protected function isBool($input) |
||
| 129 | { |
||
| 130 | return in_array(strtolower($input), ['true', 'false']) !== false; |
||
| 131 | } |
||
| 132 | |||
| 133 | public function children() |
||
| 134 | { |
||
| 135 | if ($this->value instanceof XMLReaderElement) { |
||
| 136 | return [$this->value]; |
||
| 137 | } |
||
| 138 | |||
| 139 | if (is_array($this->value)) { |
||
| 140 | $results = []; |
||
| 141 | foreach($this->value as $value) { |
||
| 142 | if ($value instanceof XMLReaderElement) { |
||
| 143 | $results[] = $value; |
||
| 144 | } |
||
| 145 | } |
||
| 146 | return $results; |
||
| 147 | } |
||
| 148 | |||
| 149 | return []; |
||
| 150 | } |
||
| 151 | |||
| 152 | public function hasChildren() |
||
| 153 | { |
||
| 154 | return !empty($this->children()); |
||
| 155 | } |
||
| 156 | |||
| 157 | public function findFirst($search) |
||
| 161 | |||
| 162 | public function find($search) { |
||
| 181 | |||
| 182 | public function __get($name) |
||
| 183 | { |
||
| 184 | /* Access the Elements Attributes */ |
||
| 185 | if (is_array($this->value)) { |
||
| 186 | foreach($this->value as $value) { |
||
| 187 | if ($value instanceof XMLReaderElement && $value->name == $name) { |
||
| 195 | |||
| 196 | public function __debugInfo() { |
||
| 216 | } |
||
| 217 |