| Total Complexity | 46 |
| Total Lines | 375 |
| Duplicated Lines | 0 % |
| Coverage | 98.37% |
| Changes | 0 | ||
Complex classes like Collection 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 Collection, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class Collection extends AbstractJsonDeserializeObject implements \Iterator, \JsonSerializable, \Countable, \ArrayAccess |
||
| 14 | { |
||
| 15 | const ID = 'id'; |
||
| 16 | use ContextTrait; |
||
| 17 | const COLLECTION_TYPE = Collection::TYPE_LIST; |
||
|
1 ignored issue
–
show
|
|||
| 18 | const TYPE_LIST = 'List'; |
||
| 19 | const TYPE_MAP = 'Map'; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @var string |
||
| 23 | */ |
||
| 24 | protected $type; |
||
| 25 | |||
| 26 | protected $pos = 0; |
||
| 27 | |||
| 28 | protected $keys = []; |
||
| 29 | |||
| 30 | protected $index = []; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @param array $data |
||
| 34 | * @param Context|callable $context |
||
| 35 | */ |
||
| 36 | 555 | final public function __construct(array $data = [], $context = null) |
|
| 37 | { |
||
| 38 | 555 | parent::__construct($data, $context); |
|
| 39 | 555 | $this->indexData(); |
|
| 40 | 555 | } |
|
| 41 | |||
| 42 | /** |
||
| 43 | * @return string |
||
| 44 | */ |
||
| 45 | 482 | public function getType() |
|
| 46 | { |
||
| 47 | 482 | return $this->type; |
|
| 48 | } |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @param string $type |
||
| 52 | * @internal |
||
| 53 | * @return $this |
||
| 54 | */ |
||
| 55 | 42 | public function setType($type) |
|
| 56 | { |
||
| 57 | 42 | $this->type = $type; |
|
| 58 | |||
| 59 | 42 | return $this; |
|
| 60 | } |
||
| 61 | |||
| 62 | 555 | protected function indexData() |
|
| 63 | { |
||
| 64 | 555 | $this->keys = array_keys($this->rawData); |
|
| 65 | 555 | foreach ($this->rawData as $offset => $row) { |
|
| 66 | 357 | $this->indexRow($offset, $row); |
|
| 67 | } |
||
| 68 | 555 | } |
|
| 69 | |||
| 70 | /** |
||
| 71 | * @param array $rawData |
||
| 72 | * @internal |
||
| 73 | * @return $this |
||
| 74 | */ |
||
| 75 | 30 | public function setRawData(array $rawData) |
|
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @param string $indexName |
||
| 85 | * @param int $offset |
||
| 86 | * @param $key |
||
| 87 | */ |
||
| 88 | 516 | protected function addToIndex($indexName, $offset, $key) |
|
| 89 | { |
||
| 90 | 516 | if (!is_null($key)) { |
|
| 91 | 312 | $this->index[$indexName][$key] = $offset; |
|
| 92 | } |
||
| 93 | 516 | } |
|
| 94 | |||
| 95 | 312 | protected function indexRow($offset, $row) |
|
| 96 | { |
||
| 97 | 312 | $id = null; |
|
| 98 | 312 | if ($row instanceof Resource) { |
|
| 99 | 1 | $id = $row->getId(); |
|
| 100 | 311 | } elseif (is_array($row)) { |
|
| 101 | 158 | $id = isset($row[static::ID]) ? $row[static::ID] : null; |
|
| 102 | } |
||
| 103 | 312 | $this->addToIndex(static::ID, $offset, $id); |
|
| 104 | 312 | } |
|
| 105 | |||
| 106 | /** |
||
| 107 | * @param $id |
||
| 108 | * @return static |
||
| 109 | */ |
||
| 110 | 58 | public function getById($id) |
|
| 111 | { |
||
| 112 | 58 | return $this->getBy(static::ID, $id); |
|
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @param $indexName |
||
| 117 | * @param $key |
||
| 118 | * @return mixed|null |
||
| 119 | */ |
||
| 120 | 90 | public function getBy($indexName, $key) |
|
| 121 | { |
||
| 122 | 90 | if (isset($this->index[$indexName][$key])) { |
|
| 123 | 88 | $key = $this->index[$indexName][$key]; |
|
| 124 | |||
| 125 | 88 | return $this->getAt($key); |
|
| 126 | } |
||
| 127 | 6 | return null; |
|
| 128 | } |
||
| 129 | |||
| 130 | 341 | public function add($object) |
|
| 131 | { |
||
| 132 | 341 | $this->setAt(null, $object); |
|
| 133 | |||
| 134 | 340 | return $this; |
|
| 135 | } |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @param $offset |
||
| 139 | * @internal |
||
| 140 | */ |
||
| 141 | 314 | protected function initialize($offset) |
|
| 142 | { |
||
| 143 | 314 | $type = $this->getType(); |
|
| 144 | 314 | if ($this->isDeserializableType($type)) { |
|
| 145 | /** |
||
| 146 | * @var JsonDeserializeInterface $type |
||
| 147 | */ |
||
| 148 | 301 | $value = $type::fromArray($this->getRaw($offset), $this->getContextCallback()); |
|
| 149 | 301 | if ($value instanceof ObjectTreeInterface) { |
|
| 150 | 299 | $value->parentSet($this); |
|
| 151 | 299 | $value->rootSet($this->rootGet()); |
|
| 152 | } |
||
| 153 | 301 | $this->typeData[$offset] = $value; |
|
| 154 | } |
||
| 155 | 314 | $this->initialized[$offset] = true; |
|
| 156 | 314 | } |
|
| 157 | |||
| 158 | /** |
||
| 159 | * @return array |
||
| 160 | */ |
||
| 161 | 106 | public function toArray() |
|
| 162 | { |
||
| 163 | 106 | $values = []; |
|
| 164 | 106 | foreach ($this->typeData as $key => $value) { |
|
| 165 | 106 | if ($value instanceof JsonDeserializeInterface) { |
|
| 166 | 102 | $values[$key] = $value->toArray(); |
|
| 167 | } else { |
||
| 168 | 106 | $values[$key] = $value; |
|
| 169 | } |
||
| 170 | } |
||
| 171 | |||
| 172 | 106 | return $values; |
|
| 173 | } |
||
| 174 | |||
| 175 | /** |
||
| 176 | * @param $offset |
||
| 177 | * @return mixed |
||
| 178 | */ |
||
| 179 | 334 | public function getAt($offset) |
|
| 180 | { |
||
| 181 | 334 | return $this->getTyped($offset); |
|
| 182 | } |
||
| 183 | |||
| 184 | /** |
||
| 185 | * @param $offset |
||
| 186 | * @param mixed $default |
||
| 187 | * @return array |
||
| 188 | */ |
||
| 189 | 301 | protected function getRaw($offset, $default = []) |
|
| 190 | { |
||
| 191 | 301 | return parent::getRaw($offset, $default); |
|
| 192 | } |
||
| 193 | |||
| 194 | /** |
||
| 195 | * @param $offset |
||
| 196 | * @param $object |
||
| 197 | * @return $this |
||
| 198 | */ |
||
| 199 | 346 | public function setAt($offset, $object) |
|
| 200 | { |
||
| 201 | 346 | $type = $this->getType(); |
|
| 202 | 346 | if (!$this->isValidType($type, $object)) { |
|
| 203 | 1 | throw new \InvalidArgumentException(sprintf(Message::WRONG_TYPE, $offset, $type)); |
|
| 204 | } |
||
| 205 | |||
| 206 | 345 | if ($object instanceof ContextAwareInterface) { |
|
| 207 | 336 | $object->setContext($this->getContextCallback()); |
|
| 208 | } |
||
| 209 | 345 | if ($object instanceof ObjectTreeInterface) { |
|
| 210 | 336 | $object->parentSet($this); |
|
| 211 | 336 | $object->rootSet($this->rootGet()); |
|
| 212 | } |
||
| 213 | 345 | if (is_null($offset)) { |
|
| 214 | 343 | $this->typeData[] = $object; |
|
| 215 | 343 | $offset = count($this->typeData) - 1; |
|
| 216 | } else { |
||
| 217 | 3 | $this->typeData[$offset] = $object; |
|
| 218 | } |
||
| 219 | 345 | $this->initialized[$offset] = true; |
|
| 220 | 345 | if (!in_array($offset, $this->keys)) { |
|
| 221 | 345 | $this->keys[] = $offset; |
|
| 222 | } |
||
| 223 | 345 | $this->indexRow($offset, $object); |
|
| 224 | |||
| 225 | 345 | return $this; |
|
| 226 | } |
||
| 227 | |||
| 228 | /** |
||
| 229 | * (PHP 5 >= 5.1.0)<br/> |
||
| 230 | * Count elements of an object |
||
| 231 | * @link http://php.net/manual/en/countable.count.php |
||
| 232 | * @return int The custom count as an integer. |
||
| 233 | * </p> |
||
| 234 | * <p> |
||
| 235 | * The return value is cast to an integer. |
||
| 236 | */ |
||
| 237 | 111 | public function count() |
|
| 238 | { |
||
| 239 | 111 | $rawKeys = array_keys($this->rawData); |
|
| 240 | 111 | $typeKeys = array_keys($this->typeData); |
|
| 241 | 111 | $keys = array_merge($rawKeys, $typeKeys); |
|
| 242 | 111 | $uniqueKeys = array_unique($keys); |
|
| 243 | 111 | return count($uniqueKeys); |
|
| 244 | } |
||
| 245 | |||
| 246 | |||
| 247 | /** |
||
| 248 | * (PHP 5 >= 5.0.0)<br/> |
||
| 249 | * Return the current element |
||
| 250 | * @link http://php.net/manual/en/iterator.current.php |
||
| 251 | * @return mixed Can return any type. |
||
| 252 | */ |
||
| 253 | 221 | public function current() |
|
| 254 | { |
||
| 255 | 221 | if (isset($this->keys[$this->pos])) { |
|
| 256 | 221 | return $this->getAt($this->keys[$this->pos]); |
|
| 257 | } |
||
| 258 | return null; |
||
| 259 | } |
||
| 260 | |||
| 261 | /** |
||
| 262 | * (PHP 5 >= 5.0.0)<br/> |
||
| 263 | * Move forward to next element |
||
| 264 | * @link http://php.net/manual/en/iterator.next.php |
||
| 265 | * @return void Any returned value is ignored. |
||
| 266 | */ |
||
| 267 | 64 | public function next() |
|
| 268 | { |
||
| 269 | 64 | $this->pos++; |
|
| 270 | 64 | } |
|
| 271 | |||
| 272 | /** |
||
| 273 | * (PHP 5 >= 5.0.0)<br/> |
||
| 274 | * Return the key of the current element |
||
| 275 | * @link http://php.net/manual/en/iterator.key.php |
||
| 276 | * @return mixed scalar on success, or null on failure. |
||
| 277 | */ |
||
| 278 | 33 | public function key() |
|
| 284 | } |
||
| 285 | |||
| 286 | /** |
||
| 287 | * (PHP 5 >= 5.0.0)<br/> |
||
| 288 | * Checks if current position is valid |
||
| 289 | * @link http://php.net/manual/en/iterator.valid.php |
||
| 290 | * @return boolean The return value will be casted to boolean and then evaluated. |
||
| 291 | * Returns true on success or false on failure. |
||
| 292 | */ |
||
| 293 | 66 | public function valid() |
|
| 294 | { |
||
| 295 | 66 | if (isset($this->keys[$this->pos])) { |
|
| 296 | 64 | return $this->offsetExists($this->keys[$this->pos]); |
|
| 297 | } |
||
| 298 | 40 | return false; |
|
| 299 | } |
||
| 300 | |||
| 301 | /** |
||
| 302 | * (PHP 5 >= 5.0.0)<br/> |
||
| 303 | * Rewind the Iterator to the first element |
||
| 304 | * @link http://php.net/manual/en/iterator.rewind.php |
||
| 305 | * @return void Any returned value is ignored. |
||
| 306 | */ |
||
| 307 | 68 | public function rewind() |
|
| 308 | { |
||
| 309 | 68 | $this->pos = 0; |
|
| 310 | 68 | } |
|
| 311 | |||
| 312 | /** |
||
| 313 | * (PHP 5 >= 5.0.0)<br/> |
||
| 314 | * Whether a offset exists |
||
| 315 | * @link http://php.net/manual/en/arrayaccess.offsetexists.php |
||
| 316 | * @param mixed $offset <p> |
||
| 317 | * An offset to check for. |
||
| 318 | * </p> |
||
| 319 | * @return boolean true on success or false on failure. |
||
| 320 | * </p> |
||
| 321 | * <p> |
||
| 322 | * The return value will be casted to boolean if non-boolean was returned. |
||
| 323 | */ |
||
| 324 | 70 | public function offsetExists($offset) |
|
| 325 | { |
||
| 326 | 70 | return isset($this->rawData[$offset]) || isset($this->typeData[$offset]); |
|
| 327 | } |
||
| 328 | |||
| 329 | /** |
||
| 330 | * (PHP 5 >= 5.0.0)<br/> |
||
| 331 | * Offset to retrieve |
||
| 332 | * @link http://php.net/manual/en/arrayaccess.offsetget.php |
||
| 333 | * @param mixed $offset <p> |
||
| 334 | * The offset to retrieve. |
||
| 335 | * </p> |
||
| 336 | * @return mixed Can return all value types. |
||
| 337 | */ |
||
| 338 | 5 | public function offsetGet($offset) |
|
| 339 | { |
||
| 340 | 5 | return $this->getAt($offset); |
|
| 341 | } |
||
| 342 | |||
| 343 | /** |
||
| 344 | * (PHP 5 >= 5.0.0)<br/> |
||
| 345 | * Offset to set |
||
| 346 | * @link http://php.net/manual/en/arrayaccess.offsetset.php |
||
| 347 | * @param mixed $offset <p> |
||
| 348 | * The offset to assign the value to. |
||
| 349 | * </p> |
||
| 350 | * @param mixed $value <p> |
||
| 351 | * The value to set. |
||
| 352 | * </p> |
||
| 353 | * @return void |
||
| 354 | */ |
||
| 355 | 4 | public function offsetSet($offset, $value) |
|
| 356 | { |
||
| 357 | 4 | $this->setAt($offset, $value); |
|
| 358 | 4 | } |
|
| 359 | |||
| 360 | /** |
||
| 361 | * (PHP 5 >= 5.0.0)<br/> |
||
| 362 | * Offset to unset |
||
| 363 | * @link http://php.net/manual/en/arrayaccess.offsetunset.php |
||
| 364 | * @param mixed $offset <p> |
||
| 365 | * The offset to unset. |
||
| 366 | * </p> |
||
| 367 | * @return void |
||
| 368 | */ |
||
| 369 | 3 | public function offsetUnset($offset) |
|
| 377 | 3 | } |
|
| 378 | |||
| 379 | 283 | protected function toJson() |
|
| 380 | { |
||
| 381 | 283 | $data = parent::toJson(); |
|
| 388 | } |
||
| 389 | } |
||
| 390 |
This check looks for accesses to local static members using the fully qualified name instead of
self::.While this is perfectly valid, the fully qualified name of
Certificate::TRIPLEDES_CBCcould just as well be replaced byself::TRIPLEDES_CBC. Referencing local members withself::assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.