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 |
||
24 | class DoctrineAdapter extends AbstractAdapter implements AdapterInterface |
||
25 | { |
||
26 | /** |
||
27 | * @var Doctrine\ORM\EntityManager |
||
28 | */ |
||
29 | public $em; |
||
30 | |||
31 | /** |
||
32 | * @var int |
||
33 | */ |
||
34 | public $priority = 0; |
||
35 | |||
36 | /** |
||
37 | * Does a queue already exist? |
||
38 | * |
||
39 | * Throws an exception if the adapter cannot determine if a queue exists. |
||
40 | * use isSupported('isExists') to determine if an adapter can test for |
||
41 | * queue existance. |
||
42 | * |
||
43 | * @param string $name |
||
44 | * |
||
45 | * @return bool |
||
46 | * |
||
47 | * @throws ZendQueue\Exception |
||
48 | */ |
||
49 | public function isExists($name) |
||
59 | |||
60 | /** |
||
61 | * Create a new queue. |
||
62 | * |
||
63 | * Visibility timeout is how long a message is left in the queue "invisible" |
||
64 | * to other readers. If the message is acknowleged (deleted) before the |
||
65 | * timeout, then the message is deleted. However, if the timeout expires |
||
66 | * then the message will be made available to other queue readers. |
||
67 | * |
||
68 | * @param string $name Queue name |
||
69 | * @param int $timeout Default visibility timeout |
||
70 | * |
||
71 | * @return bool |
||
72 | * |
||
73 | * @throws ZendQueue\Exception - database error |
||
74 | */ |
||
75 | public function create($name, $timeout = null) |
||
91 | |||
92 | /** |
||
93 | * Delete a queue and all of it's messages. |
||
94 | * |
||
95 | * Returns false if the queue is not found, true if the queue exists |
||
96 | * |
||
97 | * @param string $name Queue name |
||
98 | * |
||
99 | * @return bool |
||
100 | * |
||
101 | * @throws ZendQueue\Exception |
||
102 | */ |
||
103 | public function delete($name) |
||
127 | |||
128 | /* |
||
129 | * Get an array of all available queues |
||
130 | * |
||
131 | * Not all adapters support getQueues(), use isSupported('getQueues') |
||
132 | * to determine if the adapter supports this feature. |
||
133 | * |
||
134 | * @return array |
||
135 | */ |
||
136 | public function getQueues() |
||
149 | |||
150 | /** |
||
151 | * Return the approximate number of messages in the queue. |
||
152 | * |
||
153 | * @param ZendQueue\Queue $queue |
||
154 | * |
||
155 | * @return int |
||
156 | * |
||
157 | * @throws ZendQueue\Exception |
||
158 | */ |
||
159 | public function count(Queue $queue = null) |
||
179 | |||
180 | /** |
||
181 | * Send a message to the queue. |
||
182 | * |
||
183 | * @param string $message Message to send to the active queue |
||
184 | * @param ZendQueue\Queue $queue |
||
185 | * |
||
186 | * @return ZendQueue\Message |
||
187 | * |
||
188 | * @throws ZendQueue\Exception |
||
189 | */ |
||
190 | public function send($message, Queue $queue = null) |
||
221 | |||
222 | /** |
||
223 | * Get messages in the queue. |
||
224 | * |
||
225 | * @param int $maxMessages Maximum number of messages to return |
||
226 | * @param int $timeout Visibility timeout for these messages |
||
227 | * @param ZendQueue\Queue $queue |
||
228 | * |
||
229 | * @return ZendQueue\MessageIterator |
||
230 | * |
||
231 | * @throws ZendQueue\Exception Database error |
||
232 | */ |
||
233 | public function receive($maxMessages = null, $timeout = null, Queue $queue = null) |
||
273 | |||
274 | /** |
||
275 | * Delete a message from the queue. |
||
276 | * |
||
277 | * Returns true if the message is deleted, false if the deletion is |
||
278 | * unsuccessful. |
||
279 | * |
||
280 | * @param ZendQueue\Message $message |
||
281 | * |
||
282 | * @return bool |
||
283 | * |
||
284 | * @throws ZendQueue\Exception - database error |
||
285 | */ |
||
286 | public function deleteMessage(Message $message) |
||
299 | |||
300 | /** |
||
301 | * Return a list of queue capabilities functions. |
||
302 | * |
||
303 | * $array['function name'] = true or false |
||
304 | * true is supported, false is not supported. |
||
305 | * |
||
306 | * @param string $name |
||
307 | * |
||
308 | * @return array |
||
309 | */ |
||
310 | public function getCapabilities() |
||
323 | |||
324 | /** |
||
325 | * Retry failed messages. |
||
326 | * |
||
327 | * @param int $id |
||
328 | */ |
||
329 | public function retry($id = null) |
||
345 | |||
346 | /** |
||
347 | * Delete a failed message. |
||
348 | * |
||
349 | * @param int $id |
||
350 | */ |
||
351 | public function forget($id) |
||
359 | |||
360 | /** |
||
361 | * {@inheritdoc} |
||
362 | */ |
||
363 | public function setPriority($priority) |
||
367 | |||
368 | /** |
||
369 | * {@inheritdoc} |
||
370 | */ |
||
371 | public function showMessages($queueName) |
||
390 | |||
391 | /** |
||
392 | * {@inheritdoc} |
||
393 | */ |
||
394 | public function flush() |
||
400 | |||
401 | /** |
||
402 | * {@inheritdoc} |
||
403 | */ |
||
404 | public function logException($message, $e) |
||
433 | |||
434 | /** |
||
435 | * Create a new message. |
||
436 | * |
||
437 | * @param ZendQueue\Queue $queue |
||
438 | * @param string $body |
||
439 | */ |
||
440 | protected function createMessage(Queue $queue, $body) |
||
465 | |||
466 | /** |
||
467 | * Get messages of the queue. |
||
468 | * |
||
469 | * @param int $maxMessages |
||
470 | * @param int $timeout |
||
471 | * @param ZendQueue\Queue $queue |
||
472 | * @param int $microtime |
||
473 | */ |
||
474 | protected function getMessages($maxMessages, $timeout, $queue = null, $microtime = null) |
||
510 | |||
511 | /** |
||
512 | * Get the queue entity. |
||
513 | * |
||
514 | * @param string $name |
||
515 | * |
||
516 | * @return Queue Entity |
||
517 | * |
||
518 | * @throws ZendQueue\Exception |
||
519 | */ |
||
520 | protected function getQueueEntity($name) |
||
534 | } |
||
535 |
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: