| Total Complexity | 43 |
| Total Lines | 333 |
| Duplicated Lines | 6.01 % |
| Coverage | 35.48% |
| Changes | 0 | ||
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like AHTMLNodeList 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.
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 AHTMLNodeList, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 5 | class AHTMLNodeList implements \Iterator, \Countable, \ArrayAccess |
||
| 6 | { |
||
| 7 | |||
| 8 | /** |
||
| 9 | * @var |
||
| 10 | */ |
||
| 11 | private $nodeList; |
||
| 12 | |||
| 13 | /** |
||
| 14 | * @var |
||
| 15 | */ |
||
| 16 | private $doc; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @var int |
||
| 20 | */ |
||
| 21 | private $counter = 0; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * AHTMLNodeList constructor. |
||
| 25 | * |
||
| 26 | * @param $nodeList |
||
| 27 | * @param $doc |
||
| 28 | */ |
||
| 29 | 3 | public function __construct($nodeList, $doc) |
|
| 30 | { |
||
| 31 | 3 | $this->nodeList = $nodeList; |
|
| 32 | 3 | $this->doc = $doc; |
|
| 33 | 3 | } |
|
| 34 | |||
| 35 | /* |
||
| 36 | abstract public boolean offsetExists ( mixed $offset ) |
||
| 37 | abstract public mixed offsetGet ( mixed $offset ) |
||
| 38 | abstract public void offsetSet ( mixed $offset , mixed $value ) |
||
| 39 | abstract public void offsetUnset ( mixed $offset ) |
||
| 40 | */ |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @param mixed $offset |
||
| 44 | * |
||
| 45 | * @return bool |
||
| 46 | */ |
||
| 47 | public function offsetExists($offset) |
||
| 48 | { |
||
| 49 | return 0 <= $offset && $offset < $this->nodeList->length; |
||
| 50 | } |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @param mixed $offset |
||
| 54 | * |
||
| 55 | * @return AHTMLNode |
||
| 56 | */ |
||
| 57 | public function offsetGet($offset) |
||
| 58 | { |
||
| 59 | return new AHTMLNode($this->nodeList->item($offset), $this->doc); |
||
| 60 | } |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @param mixed $offset |
||
| 64 | * @param mixed $value |
||
| 65 | */ |
||
| 66 | public function offsetSet($offset, $value) |
||
| 67 | { |
||
| 68 | \trigger_error('offsetSet not implemented', E_USER_WARNING); |
||
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @param mixed $offset |
||
| 73 | */ |
||
| 74 | public function offsetUnset($offset) |
||
| 75 | { |
||
| 76 | \trigger_error('offsetUnset not implemented', E_USER_WARNING); |
||
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @return mixed |
||
| 81 | */ |
||
| 82 | public function count() |
||
| 83 | { |
||
| 84 | return $this->nodeList->length; |
||
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * |
||
| 89 | */ |
||
| 90 | 2 | public function rewind() |
|
| 91 | { |
||
| 92 | 2 | $this->counter = 0; |
|
| 93 | 2 | } |
|
| 94 | |||
| 95 | /** |
||
| 96 | * @return AHTMLNode |
||
| 97 | */ |
||
| 98 | 2 | public function current() |
|
| 99 | { |
||
| 100 | 2 | return new AHTMLNode($this->nodeList->item($this->counter), $this->doc); |
|
| 101 | } |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @return int |
||
| 105 | */ |
||
| 106 | public function key() |
||
| 107 | { |
||
| 108 | return $this->counter; |
||
| 109 | } |
||
| 110 | |||
| 111 | /** |
||
| 112 | * |
||
| 113 | */ |
||
| 114 | 2 | public function next() |
|
| 115 | { |
||
| 116 | 2 | $this->counter++; |
|
| 117 | 2 | } |
|
| 118 | |||
| 119 | /** |
||
| 120 | * @return bool |
||
| 121 | */ |
||
| 122 | 2 | public function valid() |
|
| 123 | { |
||
| 124 | 2 | return $this->nodeList && $this->counter < $this->nodeList->length; |
|
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @return AHTMLNode|null |
||
| 129 | */ |
||
| 130 | public function last() |
||
| 131 | { |
||
| 132 | return ($this->nodeList->length > 0) ? new AHTMLNode($this->nodeList->item($this->nodeList->length - 1), $this->doc) : null; |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @return $this |
||
| 137 | */ |
||
| 138 | public function remove() |
||
| 139 | { |
||
| 140 | foreach ($this as $node) |
||
| 141 | { |
||
| 142 | $node->remove(); |
||
| 143 | } |
||
| 144 | |||
| 145 | return $this; |
||
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @param $c |
||
| 150 | * |
||
| 151 | * @return array |
||
| 152 | */ |
||
| 153 | public function map($c) |
||
| 154 | { |
||
| 155 | $ret = array(); |
||
| 156 | foreach ($this as $node) |
||
| 157 | { |
||
| 158 | $ret[] = $c($node); |
||
| 159 | } |
||
| 160 | |||
| 161 | return $ret; |
||
| 162 | } |
||
| 163 | |||
| 164 | |||
| 165 | //math methods |
||
| 166 | |||
| 167 | /** |
||
| 168 | * @param $nl |
||
| 169 | * @param string $op |
||
| 170 | * |
||
| 171 | * @return AHTMLNodeList |
||
| 172 | */ |
||
| 173 | public function doMath($nl, $op = 'plus') |
||
| 174 | { |
||
| 175 | $paths = array(); |
||
| 176 | $other_paths = array(); |
||
| 177 | |||
| 178 | foreach ($this as $node) |
||
| 179 | { |
||
| 180 | $paths[] = $node->node->getNodePath(); |
||
| 181 | } |
||
| 182 | foreach ($nl as $node) |
||
| 183 | { |
||
| 184 | $other_paths[] = $node->node->getNodePath(); |
||
| 185 | } |
||
| 186 | switch ($op) |
||
| 187 | { |
||
| 188 | case 'plus': |
||
| 189 | $new_paths = \array_unique(\array_merge($paths, $other_paths)); |
||
| 190 | break; |
||
| 191 | case 'minus': |
||
| 192 | $new_paths = \array_diff($paths, $other_paths); |
||
| 193 | break; |
||
| 194 | case 'intersect': |
||
| 195 | $new_paths = \array_intersect($paths, $other_paths); |
||
| 196 | break; |
||
| 197 | } |
||
| 198 | |||
| 199 | return new AHTMLNodeList($this->doc->xpath->query(implode('|', $new_paths)), $this->doc); |
||
|
|
|||
| 200 | } |
||
| 201 | |||
| 202 | /** |
||
| 203 | * @param $nl |
||
| 204 | * |
||
| 205 | * @return AHTMLNodeList |
||
| 206 | */ |
||
| 207 | public function minus($nl) |
||
| 208 | { |
||
| 209 | return $this->doMath($nl, 'minus'); |
||
| 210 | } |
||
| 211 | |||
| 212 | /** |
||
| 213 | * @param $nl |
||
| 214 | * |
||
| 215 | * @return AHTMLNodeList |
||
| 216 | */ |
||
| 217 | public function plus($nl) |
||
| 218 | { |
||
| 219 | return $this->doMath($nl, 'plus'); |
||
| 220 | } |
||
| 221 | |||
| 222 | /** |
||
| 223 | * @param $nl |
||
| 224 | * |
||
| 225 | * @return AHTMLNodeList |
||
| 226 | */ |
||
| 227 | public function intersect($nl) |
||
| 228 | { |
||
| 229 | return $this->doMath($nl, 'intersect'); |
||
| 230 | } |
||
| 231 | |||
| 232 | |||
| 233 | // magic methods |
||
| 234 | |||
| 235 | /** |
||
| 236 | * @param $key |
||
| 237 | * @param $values |
||
| 238 | * |
||
| 239 | * @return array|string |
||
| 240 | */ |
||
| 241 | 2 | public function __call($key, $values) |
|
| 242 | { |
||
| 243 | 2 | $key = \strtolower(\str_replace('_', '', $key)); |
|
| 244 | switch ($key) |
||
| 245 | { |
||
| 246 | 2 | case 'to_a': |
|
| 247 | $retval = array(); |
||
| 248 | foreach ($this as $node) |
||
| 249 | { |
||
| 250 | $retval[] = new AHTMLNode($this->nodeList->item($this->counter), $this->doc); |
||
| 251 | } |
||
| 252 | |||
| 253 | return $retval; |
||
| 254 | } |
||
| 255 | // otherwise |
||
| 256 | |||
| 257 | 2 | $retval = array(); |
|
| 258 | |||
| 259 | /* |
||
| 260 | if(preg_match(TAGS_REGEX, $key, $m)) return $this->find($m[1]); |
||
| 261 | if(preg_match(TAG_REGEX, $key, $m)) return $this->find($m[1], 0); |
||
| 262 | */ |
||
| 263 | |||
| 264 | 2 | View Code Duplication | if (\preg_match(ATTRIBUTES_REGEX, $key, $m) || \preg_match('/^((clean|trim|str).*)s$/', $key, $m)) |
| 265 | { |
||
| 266 | foreach ($this as $node) |
||
| 267 | { |
||
| 268 | $arg = $m[1]; |
||
| 269 | $retval[] = $node->$arg; |
||
| 270 | } |
||
| 271 | |||
| 272 | return $retval; |
||
| 273 | } |
||
| 274 | |||
| 275 | 2 | View Code Duplication | if (\preg_match(ATTRIBUTE_REGEX, $key, $m)) |
| 276 | { |
||
| 277 | 2 | foreach ($this as $node) |
|
| 278 | { |
||
| 279 | 2 | $arg = $m[1]; |
|
| 280 | 2 | $retval[] = $node->$arg; |
|
| 281 | } |
||
| 282 | |||
| 283 | 2 | return \implode('', $retval); |
|
| 284 | } |
||
| 285 | |||
| 286 | // what now? |
||
| 287 | 2 | foreach ($this as $node) |
|
| 288 | { |
||
| 289 | 2 | $retval[] = isset($values[0]) ? $node->$key($values[0]) : $node->$key(); |
|
| 290 | } |
||
| 291 | |||
| 292 | 2 | return \implode('', $retval); |
|
| 293 | } |
||
| 294 | |||
| 295 | /** |
||
| 296 | * @param $key |
||
| 297 | * |
||
| 298 | * @return mixed |
||
| 299 | */ |
||
| 300 | 1 | public function __get($key) |
|
| 301 | { |
||
| 302 | 1 | return $this->$key(); |
|
| 303 | } |
||
| 304 | |||
| 305 | /** |
||
| 306 | * @param $name |
||
| 307 | * @param $value |
||
| 308 | */ |
||
| 309 | 1 | public function __set($name, $value) |
|
| 310 | { |
||
| 311 | 1 | throw new \InvalidArgumentException(__METHOD__); |
|
| 312 | } |
||
| 313 | |||
| 314 | /** |
||
| 315 | * @param $name |
||
| 316 | * |
||
| 317 | * @return bool |
||
| 318 | */ |
||
| 319 | public function __isset($name) |
||
| 320 | { |
||
| 321 | return true; |
||
| 322 | } |
||
| 323 | |||
| 324 | /** |
||
| 325 | * @return string |
||
| 326 | */ |
||
| 327 | 2 | public function __toString() |
|
| 328 | { |
||
| 329 | 2 | return (string)$this->html(); |
|
| 330 | } |
||
| 331 | |||
| 332 | /** |
||
| 333 | * @return mixed |
||
| 334 | */ |
||
| 335 | public function length() |
||
| 338 | } |
||
| 339 | } |
||
| 340 |