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 |
||
32 | class Cursor implements \Iterator, \Countable, \arrayaccess { |
||
33 | /** |
||
34 | * @var ResponseInterface |
||
35 | */ |
||
36 | protected $response; |
||
37 | |||
38 | /** |
||
39 | * @var Api |
||
40 | */ |
||
41 | protected $api; |
||
42 | |||
43 | /** |
||
44 | * @var AbstractObject[] |
||
45 | */ |
||
46 | protected $objects = array(); |
||
47 | |||
48 | /** |
||
49 | * @var int|null |
||
50 | */ |
||
51 | protected $indexLeft; |
||
52 | |||
53 | /** |
||
54 | * @var int|null |
||
55 | */ |
||
56 | protected $indexRight; |
||
57 | |||
58 | /** |
||
59 | * @var int|null |
||
60 | */ |
||
61 | protected $position; |
||
62 | |||
63 | /** |
||
64 | * @var AbstractObject |
||
65 | */ |
||
66 | protected $objectPrototype; |
||
67 | |||
68 | /** |
||
69 | * @var bool |
||
70 | */ |
||
71 | protected static $defaultUseImplicitFetch = false; |
||
72 | |||
73 | /** |
||
74 | * @var bool |
||
75 | */ |
||
76 | protected $useImplicitFectch; |
||
77 | |||
78 | 24 | public function __construct( |
|
79 | ResponseInterface $response, |
||
80 | AbstractObject $object_prototype, |
||
81 | Api $api = null) { |
||
82 | 24 | $this->response = $response; |
|
83 | 24 | $this->objectPrototype = $object_prototype; |
|
84 | 24 | $this->api = $api !== null ? $api : Api::instance(); |
|
85 | 24 | $this->appendResponse($response); |
|
86 | 22 | } |
|
87 | |||
88 | /** |
||
89 | * @param array $object_data |
||
90 | * @return AbstractObject |
||
91 | */ |
||
92 | 20 | protected function createObject(array $object_data) { |
|
93 | 20 | $object = clone $this->objectPrototype; |
|
94 | 20 | $object->setDataWithoutValidation($object_data); |
|
95 | 20 | if ($object instanceof AbstractCrudObject) { |
|
|
|||
96 | $object->setApi($this->api); |
||
97 | } |
||
98 | 20 | return $object; |
|
99 | } |
||
100 | |||
101 | /** |
||
102 | * @param ResponseInterface $response |
||
103 | * @return array |
||
104 | * @throws \InvalidArgumentException |
||
105 | */ |
||
106 | 24 | protected function assureResponseData(ResponseInterface $response) { |
|
107 | 24 | $content = $response->getContent(); |
|
108 | |||
109 | // First, check if the content contains data |
||
110 | 24 | if (isset($content['data'])) { |
|
111 | 20 | $data = $content['data']; |
|
112 | |||
113 | // If data is an object (represented by a map instead of a pure list), |
||
114 | // wrap the object into an array |
||
115 | 20 | if (array_keys($data) !== range(0, count($data) - 1)) { |
|
116 | 10 | $data = array($data); |
|
117 | 10 | } |
|
118 | 18 | return $data; |
|
119 | } |
||
120 | |||
121 | // Second, check if the content contains targetingsentencelines |
||
122 | 4 | if (isset($content['targetingsentencelines'])) { |
|
123 | return $content['targetingsentencelines']; |
||
124 | } |
||
125 | |||
126 | // Third, check if the content is an array of objects indexed by id |
||
127 | 4 | $is_id_indexed_array = true; |
|
128 | 4 | $objects = array(); |
|
129 | 4 | foreach ($content as $key => $value) { |
|
130 | 2 | if ($key === '__fb_trace_id__') { |
|
131 | continue; |
||
132 | } |
||
133 | |||
134 | 2 | if ($value !== null && |
|
135 | 2 | array_keys($value) !== range(0, count($value) - 1) && |
|
136 | 2 | isset($value['id']) && |
|
137 | 2 | $value['id'] !== null && |
|
138 | 2 | $value['id'] === $key) { |
|
139 | $objects[] = $value; |
||
140 | } else { |
||
141 | 2 | $is_id_indexed_array = false; |
|
142 | 2 | break; |
|
143 | } |
||
144 | 4 | } |
|
145 | 4 | if ($is_id_indexed_array) { |
|
146 | 2 | return $objects; |
|
147 | } |
||
148 | |||
149 | // Fourth, check if the content itself is an object (represented by a map |
||
150 | // instead of a pure list) |
||
151 | 2 | if (array_keys($content) !== range(0, count($content) - 1)) { |
|
152 | 2 | return array($content); |
|
153 | } |
||
154 | |||
155 | throw new \InvalidArgumentException("Malformed response data"); |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * @param ResponseInterface $response |
||
160 | */ |
||
161 | 2 | protected function prependResponse(ResponseInterface $response) { |
|
162 | 2 | $this->response = $response; |
|
163 | 2 | $data = $this->assureResponseData($response); |
|
164 | 2 | if (empty($data)) { |
|
165 | return; |
||
166 | } |
||
167 | |||
168 | 2 | $left_index = $this->indexLeft; |
|
169 | 2 | $count = count($data); |
|
170 | 2 | $position = $count - 1; |
|
171 | 2 | for ($i = $left_index - 1; $i >= $left_index - $count; $i--) { |
|
172 | 2 | $this->objects[$i] = $this->createObject($data[$position--]); |
|
173 | 2 | --$this->indexLeft; |
|
174 | 2 | } |
|
175 | 2 | } |
|
176 | |||
177 | /** |
||
178 | * @param ResponseInterface $response |
||
179 | */ |
||
180 | 24 | protected function appendResponse(ResponseInterface $response) { |
|
181 | 24 | $this->response = $response; |
|
182 | 24 | $data = $this->assureResponseData($response); |
|
183 | 22 | if (empty($data)) { |
|
184 | 2 | return; |
|
185 | } |
||
186 | |||
187 | 20 | if ($this->indexRight === null) { |
|
188 | 20 | $this->indexLeft = 0; |
|
189 | 20 | $this->indexRight = -1; |
|
190 | 20 | $this->position = 0; |
|
191 | 20 | } |
|
192 | |||
193 | 20 | $this->indexRight += count($data); |
|
194 | |||
195 | 20 | foreach ($data as $object_data) { |
|
196 | 20 | $this->objects[] = $this->createObject($object_data); |
|
197 | 20 | } |
|
198 | 20 | } |
|
199 | |||
200 | /** |
||
201 | * @return bool |
||
202 | */ |
||
203 | 2 | public static function getDefaultUseImplicitFetch() { |
|
206 | |||
207 | /** |
||
208 | * @param bool $use_implicit_fectch |
||
209 | */ |
||
210 | 2 | public static function setDefaultUseImplicitFetch($use_implicit_fectch) { |
|
213 | |||
214 | /** |
||
215 | * @return bool |
||
216 | */ |
||
217 | 4 | public function getUseImplicitFetch() { |
|
218 | 4 | return $this->useImplicitFectch !== null |
|
219 | 4 | ? $this->useImplicitFectch |
|
220 | 4 | : static::$defaultUseImplicitFetch; |
|
221 | } |
||
222 | |||
223 | /** |
||
224 | * @param bool $use_implicit_fectch |
||
225 | */ |
||
226 | 2 | public function setUseImplicitFetch($use_implicit_fectch) { |
|
229 | |||
230 | /** |
||
231 | * @return string|null |
||
232 | */ |
||
233 | 8 | public function getBefore() { |
|
234 | 8 | $content = $this->getLastResponse()->getContent(); |
|
235 | 8 | return isset($content['paging']['cursors']['before']) |
|
236 | 8 | ? $content['paging']['cursors']['before'] |
|
237 | 8 | : null; |
|
238 | } |
||
239 | |||
240 | /** |
||
241 | * @return string|null |
||
242 | */ |
||
243 | 9 | public function getAfter() { |
|
244 | 9 | $content = $this->getLastResponse()->getContent(); |
|
245 | 9 | return isset($content['paging']['cursors']['after']) |
|
246 | 9 | ? $content['paging']['cursors']['after'] |
|
247 | 9 | : null; |
|
248 | } |
||
249 | |||
250 | /** |
||
251 | * @return RequestInterface |
||
252 | */ |
||
253 | 4 | protected function createUndirectionalizedRequest() { |
|
254 | 4 | $request = $this->getLastResponse()->getRequest()->createClone(); |
|
255 | 4 | $params = $request->getQueryParams(); |
|
256 | 4 | if (array_key_exists('before', $params)) { |
|
257 | 2 | unset($params['before']); |
|
258 | 2 | } |
|
259 | 4 | if (array_key_exists('after', $params)) { |
|
260 | 2 | unset($params['after']); |
|
261 | 2 | } |
|
262 | |||
263 | 4 | return $request; |
|
264 | } |
||
265 | |||
266 | /** |
||
267 | * @return string|null |
||
268 | */ |
||
269 | 6 | public function getPrevious() { |
|
270 | 6 | $content = $this->getLastResponse()->getContent(); |
|
271 | 6 | if (isset($content['paging']['previous'])) { |
|
272 | 1 | return $content['paging']['previous']; |
|
273 | } |
||
274 | |||
275 | 6 | $before = $this->getBefore(); |
|
276 | 6 | if ($before !== null) { |
|
277 | 1 | $request = $this->createUndirectionalizedRequest(); |
|
278 | 1 | $request->getQueryParams()->offsetSet('before', $before); |
|
279 | 1 | return $request->getUrl(); |
|
280 | } |
||
281 | |||
282 | 6 | return null; |
|
283 | } |
||
284 | |||
285 | /** |
||
286 | * @return string|null |
||
287 | */ |
||
288 | 10 | public function getNext() { |
|
303 | |||
304 | /** |
||
305 | * @param string $url |
||
306 | * @return RequestInterface |
||
307 | */ |
||
308 | 8 | protected function createRequestFromUrl($url) { |
|
309 | 8 | $components = parse_url($url); |
|
310 | 8 | $request = $this->getLastResponse()->getRequest()->createClone(); |
|
311 | 8 | $request->setDomain($components['host']); |
|
312 | 8 | $query = isset($components['query']) |
|
313 | 8 | ? Util::parseUrlQuery($components['query']) |
|
314 | 8 | : array(); |
|
315 | 8 | $request->getQueryParams()->enhance($query); |
|
316 | |||
317 | 8 | return $request; |
|
318 | } |
||
319 | |||
320 | /** |
||
321 | * @return RequestInterface|null |
||
322 | */ |
||
323 | 6 | public function createBeforeRequest() { |
|
324 | 6 | $url = $this->getPrevious(); |
|
325 | 6 | return $url !== null ? $this->createRequestFromUrl($url) : null; |
|
326 | } |
||
327 | |||
328 | /** |
||
329 | * @return RequestInterface|null |
||
330 | */ |
||
331 | 10 | public function createAfterRequest() { |
|
332 | 10 | $url = $this->getNext(); |
|
333 | 10 | return $url !== null ? $this->createRequestFromUrl($url) : null; |
|
334 | } |
||
335 | |||
336 | 6 | public function fetchBefore() { |
|
337 | 6 | $request = $this->createBeforeRequest(); |
|
338 | 6 | if (!$request) { |
|
339 | 6 | return; |
|
340 | } |
||
341 | |||
342 | 2 | $this->prependResponse($request->execute()); |
|
343 | 2 | } |
|
344 | |||
345 | 10 | public function fetchAfter() { |
|
346 | 10 | $request = $this->createAfterRequest(); |
|
347 | 10 | if (!$request) { |
|
348 | 4 | return; |
|
349 | } |
||
350 | |||
351 | 8 | $this->appendResponse($request->execute()); |
|
352 | 8 | } |
|
353 | |||
354 | /** |
||
355 | * @deprecated Use getArrayCopy() |
||
356 | * @return AbstractObject[] |
||
357 | */ |
||
358 | 2 | public function getObjects() { |
|
361 | |||
362 | /** |
||
363 | * @param bool $ksort |
||
364 | * @return AbstractObject[] |
||
365 | */ |
||
366 | 2 | public function getArrayCopy($ksort = false) { |
|
367 | 2 | if ($ksort) { |
|
368 | // Sort the main array to improve best case performance in future |
||
369 | // invocations |
||
370 | 2 | ksort($this->objects); |
|
371 | 2 | } |
|
372 | |||
373 | 2 | return $this->objects; |
|
374 | } |
||
375 | |||
376 | /** |
||
377 | * @deprecated Use getLastResponse() |
||
378 | * @return ResponseInterface |
||
379 | */ |
||
380 | 2 | public function getResponse() { |
|
381 | 2 | return $this->response; |
|
382 | } |
||
383 | |||
384 | /** |
||
385 | * @return ResponseInterface |
||
386 | */ |
||
387 | 12 | public function getLastResponse() { |
|
390 | |||
391 | /** |
||
392 | * @return int |
||
393 | */ |
||
394 | 8 | public function getIndexLeft() { |
|
397 | |||
398 | /** |
||
399 | * @return int |
||
400 | */ |
||
401 | 6 | public function getIndexRight() { |
|
404 | |||
405 | 4 | public function rewind() { |
|
408 | |||
409 | 2 | public function end() { |
|
412 | |||
413 | /** |
||
414 | * @param int $position |
||
415 | */ |
||
416 | 2 | public function seekTo($position) { |
|
417 | 2 | $position = array_key_exists($position, $this->objects) ? $position : null; |
|
418 | 2 | $this->position = $position; |
|
419 | 2 | } |
|
420 | |||
421 | /** |
||
422 | * @return AbstractObject|bool |
||
423 | */ |
||
424 | 2 | public function current() { |
|
425 | 2 | return isset($this->objects[$this->position]) |
|
426 | 2 | ? $this->objects[$this->position] |
|
427 | 2 | : false; |
|
428 | } |
||
429 | |||
430 | /** |
||
431 | * @return int |
||
432 | */ |
||
433 | 4 | public function key() { |
|
436 | |||
437 | 4 | public function prev() { |
|
453 | |||
454 | 6 | public function next() { |
|
470 | |||
471 | /** |
||
472 | * @return bool |
||
473 | */ |
||
474 | 4 | public function valid() { |
|
477 | |||
478 | /** |
||
479 | * @return int |
||
480 | */ |
||
481 | 10 | public function count() { |
|
484 | |||
485 | /** |
||
486 | * @param mixed $offset |
||
487 | * @param mixed $value |
||
488 | */ |
||
489 | 2 | public function offsetSet($offset, $value) { |
|
496 | |||
497 | /** |
||
498 | * @param mixed $offset |
||
499 | * @return bool |
||
500 | */ |
||
501 | 2 | public function offsetExists($offset) { |
|
504 | |||
505 | /** |
||
506 | * @param mixed $offset |
||
507 | */ |
||
508 | 2 | public function offsetUnset($offset) { |
|
511 | |||
512 | /** |
||
513 | * @param mixed $offset |
||
514 | * @return mixed |
||
515 | */ |
||
516 | 2 | public function offsetGet($offset) { |
|
519 | } |
||
520 |
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.