Complex classes like Cursor 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 Cursor, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | class Cursor implements \Iterator, \Countable, \arrayaccess |
||
| 13 | { |
||
| 14 | /** @var Resource */ |
||
| 15 | private $resource; |
||
| 16 | private $params; |
||
| 17 | private $next_cursor = null; |
||
| 18 | private $current_index = 0; |
||
| 19 | private $total_count = 0; |
||
| 20 | /** @var array */ |
||
| 21 | private $collection; |
||
| 22 | /** @var TwitterAds */ |
||
| 23 | private $twitterAds; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var int|null |
||
| 27 | */ |
||
| 28 | protected $indexLeft; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var int|null |
||
| 32 | */ |
||
| 33 | protected $indexRight; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var bool |
||
| 37 | */ |
||
| 38 | protected static $defaultUseImplicitFetch = false; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var bool |
||
| 42 | */ |
||
| 43 | protected $useImplicitFetch; |
||
| 44 | |||
| 45 | public function __construct(/* @noinspection PhpUnnecessaryFullyQualifiedNameInspection */ |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @return bool |
||
| 56 | */ |
||
| 57 | public function isExhausted() |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @return integer |
||
| 64 | */ |
||
| 65 | public function count() |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @return int |
||
| 72 | */ |
||
| 73 | public function fetched() |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @return Resource | 0 |
||
| 80 | */ |
||
| 81 | public function next() |
||
| 82 | { |
||
| 83 | if ($this->current_index == $this->getIndexRight() && !is_null($this->next_cursor)) { |
||
| 84 | if ($this->getUseImplicitFetch()) { |
||
| 85 | $this->fetchNext(); |
||
| 86 | if ($this->current_index == $this->getIndexRight()) { |
||
| 87 | $this->current_index = null; |
||
| 88 | } else { |
||
| 89 | ++$this->current_index; |
||
| 90 | } |
||
| 91 | } else { |
||
| 92 | $this->current_index = null; |
||
| 93 | } |
||
| 94 | } else { |
||
| 95 | ++$this->current_index; |
||
| 96 | } |
||
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @param array $params |
||
| 101 | * @return Cursor |
||
| 102 | */ |
||
| 103 | public function fetchNext($params = []) |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @param $request |
||
| 135 | * @return $this |
||
| 136 | */ |
||
| 137 | public function fromResponse($request) |
||
| 163 | |||
| 164 | /** |
||
| 165 | * {@inheritdoc} |
||
| 166 | */ |
||
| 167 | public function getIterator() |
||
| 171 | |||
| 172 | /** |
||
| 173 | * @return array |
||
| 174 | */ |
||
| 175 | public function getCollection() |
||
| 179 | |||
| 180 | /** |
||
| 181 | * @param array $collection |
||
| 182 | */ |
||
| 183 | public function setCollection($collection) |
||
| 187 | |||
| 188 | /** |
||
| 189 | * @return TwitterAds |
||
| 190 | */ |
||
| 191 | public function getTwitterAds() |
||
| 195 | |||
| 196 | /** |
||
| 197 | * @param TwitterAds $twitterAds |
||
| 198 | */ |
||
| 199 | public function setTwitterAds($twitterAds) |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Return the current element |
||
| 206 | * @link http://php.net/manual/en/iterator.current.php |
||
| 207 | * @return mixed Can return any type. |
||
| 208 | * @since 5.0.0 |
||
| 209 | */ |
||
| 210 | public function current() |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Return the key of the current element |
||
| 219 | * @link http://php.net/manual/en/iterator.key.php |
||
| 220 | * @return mixed scalar on success, or null on failure. |
||
| 221 | * @since 5.0.0 |
||
| 222 | */ |
||
| 223 | public function key() |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Checks if current position is valid |
||
| 230 | * @link http://php.net/manual/en/iterator.valid.php |
||
| 231 | * @return boolean The return value will be casted to boolean and then evaluated. |
||
| 232 | * Returns true on success or false on failure. |
||
| 233 | * @since 5.0.0 |
||
| 234 | */ |
||
| 235 | public function valid() |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Rewind the Iterator to the first element |
||
| 242 | * @link http://php.net/manual/en/iterator.rewind.php |
||
| 243 | * @return void Any returned value is ignored. |
||
| 244 | * @since 5.0.0 |
||
| 245 | */ |
||
| 246 | public function rewind() |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Whether a offset exists |
||
| 253 | * @link http://php.net/manual/en/arrayaccess.offsetexists.php |
||
| 254 | * @param mixed $offset <p> |
||
| 255 | * An offset to check for. |
||
| 256 | * </p> |
||
| 257 | * @return boolean true on success or false on failure. |
||
| 258 | * </p> |
||
| 259 | * <p> |
||
| 260 | * The return value will be casted to boolean if non-boolean was returned. |
||
| 261 | * @since 5.0.0 |
||
| 262 | */ |
||
| 263 | public function offsetExists($offset) |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Offset to retrieve |
||
| 270 | * @link http://php.net/manual/en/arrayaccess.offsetget.php |
||
| 271 | * @param mixed $offset <p> |
||
| 272 | * The offset to retrieve. |
||
| 273 | * </p> |
||
| 274 | * @return mixed Can return all value types. |
||
| 275 | * @since 5.0.0 |
||
| 276 | */ |
||
| 277 | public function offsetGet($offset) |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Offset to set |
||
| 284 | * @link http://php.net/manual/en/arrayaccess.offsetset.php |
||
| 285 | * @param mixed $offset <p> |
||
| 286 | * The offset to assign the value to. |
||
| 287 | * </p> |
||
| 288 | * @param mixed $value <p> |
||
| 289 | * The value to set. |
||
| 290 | * </p> |
||
| 291 | * @return void |
||
| 292 | * @since 5.0.0 |
||
| 293 | */ |
||
| 294 | public function offsetSet($offset, $value) |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Offset to unset |
||
| 305 | * @link http://php.net/manual/en/arrayaccess.offsetunset.php |
||
| 306 | * @param mixed $offset <p> |
||
| 307 | * The offset to unset. |
||
| 308 | * </p> |
||
| 309 | * @return void |
||
| 310 | * @since 5.0.0 |
||
| 311 | */ |
||
| 312 | public function offsetUnset($offset) |
||
| 316 | |||
| 317 | /** |
||
| 318 | * @return bool |
||
| 319 | */ |
||
| 320 | public static function getDefaultUseImplicitFetch() |
||
| 324 | |||
| 325 | /** |
||
| 326 | * @param bool $use_implicit_fetch |
||
| 327 | */ |
||
| 328 | public static function setDefaultUseImplicitFetch($use_implicit_fetch) |
||
| 332 | |||
| 333 | /** |
||
| 334 | * @return bool |
||
| 335 | */ |
||
| 336 | public function getUseImplicitFetch() |
||
| 342 | |||
| 343 | /** |
||
| 344 | * @param bool $useImplicitFetch |
||
| 345 | */ |
||
| 346 | public function setUseImplicitFetch($useImplicitFetch) |
||
| 350 | |||
| 351 | /** |
||
| 352 | * @return int|null |
||
| 353 | */ |
||
| 354 | public function getIndexLeft() |
||
| 358 | |||
| 359 | /** |
||
| 360 | * @return int|null |
||
| 361 | */ |
||
| 362 | public function getIndexRight() |
||
| 366 | |||
| 367 | /** |
||
| 368 | * @return mixed |
||
| 369 | */ |
||
| 370 | public function getParams() |
||
| 374 | /** |
||
| 375 | * @param mixed $params |
||
| 376 | */ |
||
| 377 | public function setParams($params) |
||
| 381 | } |
||
| 382 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..