Complex classes like DoctrineAdapter 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 DoctrineAdapter, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | class DoctrineAdapter extends AbstractAdapter implements AdapterInterface |
||
23 | { |
||
24 | /** |
||
25 | * @var Doctrine\ORM\EntityManager. |
||
26 | */ |
||
27 | public $em; |
||
28 | |||
29 | /** |
||
30 | * @var int. |
||
31 | */ |
||
32 | public $priority = 0; |
||
33 | |||
34 | /** |
||
35 | * Does a queue already exist? |
||
36 | * |
||
37 | * Throws an exception if the adapter cannot determine if a queue exists. |
||
38 | * use isSupported('isExists') to determine if an adapter can test for |
||
39 | * queue existance. |
||
40 | * |
||
41 | * @param string $name |
||
42 | * |
||
43 | * @return bool |
||
44 | * |
||
45 | * @throws ZendQueue\Exception |
||
46 | */ |
||
47 | public function isExists($name) |
||
57 | |||
58 | /** |
||
59 | * Create a new queue. |
||
60 | * |
||
61 | * Visibility timeout is how long a message is left in the queue "invisible" |
||
62 | * to other readers. If the message is acknowleged (deleted) before the |
||
63 | * timeout, then the message is deleted. However, if the timeout expires |
||
64 | * then the message will be made available to other queue readers. |
||
65 | * |
||
66 | * @param string $name Queue name |
||
67 | * @param int $timeout Default visibility timeout |
||
68 | * |
||
69 | * @return bool |
||
70 | * |
||
71 | * @throws ZendQueue\Exception - database error |
||
72 | */ |
||
73 | public function create($name, $timeout = null) |
||
89 | |||
90 | /** |
||
91 | * Delete a queue and all of it's messages. |
||
92 | * |
||
93 | * Returns false if the queue is not found, true if the queue exists |
||
94 | * |
||
95 | * @param string $name Queue name |
||
96 | * |
||
97 | * @return bool |
||
98 | * |
||
99 | * @throws ZendQueue\Exception |
||
100 | */ |
||
101 | public function delete($name) |
||
125 | |||
126 | /* |
||
127 | * Get an array of all available queues |
||
128 | * |
||
129 | * Not all adapters support getQueues(), use isSupported('getQueues') |
||
130 | * to determine if the adapter supports this feature. |
||
131 | * |
||
132 | * @return array |
||
133 | */ |
||
134 | public function getQueues() |
||
147 | |||
148 | /** |
||
149 | * Return the approximate number of messages in the queue. |
||
150 | * |
||
151 | * @param ZendQueue\Queue $queue |
||
152 | * |
||
153 | * @return int |
||
154 | * |
||
155 | * @throws ZendQueue\Exception |
||
156 | */ |
||
157 | public function count(Queue $queue = null) |
||
177 | |||
178 | /** |
||
179 | * Send a message to the queue. |
||
180 | * |
||
181 | * @param string $message Message to send to the active queue |
||
182 | * @param ZendQueue\Queue $queue |
||
183 | * |
||
184 | * @return ZendQueue\Message |
||
185 | * |
||
186 | * @throws ZendQueue\Exception |
||
187 | */ |
||
188 | public function send($message, Queue $queue = null) |
||
219 | |||
220 | /** |
||
221 | * Get messages in the queue. |
||
222 | * |
||
223 | * @param int $maxMessages Maximum number of messages to return |
||
224 | * @param int $timeout Visibility timeout for these messages |
||
225 | * @param ZendQueue\Queue $queue |
||
226 | * |
||
227 | * @return ZendQueue\MessageIterator |
||
228 | * |
||
229 | * @throws ZendQueue\Exception Database error |
||
230 | */ |
||
231 | public function receive($maxMessages = null, $timeout = null, Queue $queue = null) |
||
271 | |||
272 | /** |
||
273 | * Delete a message from the queue. |
||
274 | * |
||
275 | * Returns true if the message is deleted, false if the deletion is |
||
276 | * unsuccessful. |
||
277 | * |
||
278 | * @param ZendQueue\Message $message |
||
279 | * |
||
280 | * @return bool |
||
281 | * |
||
282 | * @throws ZendQueue\Exception - database error |
||
283 | */ |
||
284 | public function deleteMessage(Message $message) |
||
297 | |||
298 | /** |
||
299 | * Return a list of queue capabilities functions. |
||
300 | * |
||
301 | * $array['function name'] = true or false |
||
302 | * true is supported, false is not supported. |
||
303 | * |
||
304 | * @param string $name |
||
305 | * |
||
306 | * @return array |
||
307 | */ |
||
308 | public function getCapabilities() |
||
321 | |||
322 | /** |
||
323 | * Retry failed messages. |
||
324 | * |
||
325 | * @param int $id |
||
326 | */ |
||
327 | public function retry($id = null) |
||
343 | |||
344 | /** |
||
345 | * Delete a failed message. |
||
346 | * |
||
347 | * @param int $id |
||
348 | */ |
||
349 | public function forget($id) |
||
357 | |||
358 | /** |
||
359 | * {@inheritdoc} |
||
360 | */ |
||
361 | public function setPriority($priority) |
||
365 | |||
366 | /** |
||
367 | * {@inheritdoc} |
||
368 | */ |
||
369 | public function showMessages($queueName) |
||
388 | |||
389 | /** |
||
390 | * {@inheritdoc} |
||
391 | */ |
||
392 | public function flush() |
||
398 | |||
399 | /** |
||
400 | * {@inheritdoc} |
||
401 | */ |
||
402 | public function logException($message, $e) |
||
431 | |||
432 | /** |
||
433 | * Create a new message. |
||
434 | * |
||
435 | * @param ZendQueue\Queue $queue |
||
436 | * @param string $body |
||
437 | */ |
||
438 | protected function createMessage(Queue $queue, $body) |
||
463 | |||
464 | /** |
||
465 | * Get messages of the queue. |
||
466 | * |
||
467 | * @param int $maxMessages |
||
468 | * @param int $timeout |
||
469 | * @param ZendQueue\Queue $queue |
||
470 | * @param int $microtime |
||
471 | */ |
||
472 | protected function getMessages($maxMessages, $timeout, $queue = null, $microtime = null) |
||
506 | |||
507 | /** |
||
508 | * Get the queue entity. |
||
509 | * |
||
510 | * @param string $name |
||
511 | * |
||
512 | * @return Queue Entity |
||
513 | * |
||
514 | * @throws ZendQueue\Exception |
||
515 | */ |
||
516 | protected function getQueueEntity($name) |
||
530 | } |
||
531 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: