Complex classes like QueueService 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 QueueService, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
24 | class QueueService |
||
25 | { |
||
26 | const MICROSECONDS_PER_SECOND = 1000000; |
||
27 | |||
28 | const PRIORITY_HIGH = 1; |
||
29 | |||
30 | /** |
||
31 | * var ZendQueue\Adapter\AbstractAdapter. |
||
32 | */ |
||
33 | public $adapter; |
||
34 | |||
35 | /** |
||
36 | * var LoggerInterface. |
||
37 | */ |
||
38 | protected $logger; |
||
39 | |||
40 | /** |
||
41 | * var ContainerAwareCommand. |
||
42 | */ |
||
43 | protected $command; |
||
44 | |||
45 | /** |
||
46 | * var OutputInterface. |
||
47 | */ |
||
48 | protected $output; |
||
49 | |||
50 | /** |
||
51 | * var \ZendQueue\Queue. |
||
52 | */ |
||
53 | protected $queue; |
||
54 | |||
55 | /** |
||
56 | * var array. |
||
57 | */ |
||
58 | protected $config; |
||
59 | |||
60 | /** |
||
61 | * var bool. |
||
62 | */ |
||
63 | protected $running; |
||
64 | |||
65 | /** |
||
66 | * var integer. |
||
67 | */ |
||
68 | protected $processTimeout = 60; |
||
69 | |||
70 | /** |
||
71 | * @param LoggerInterface $logger |
||
72 | * @param array $config |
||
73 | */ |
||
74 | public function __construct(LoggerInterface $logger, array $config = array()) |
||
75 | { |
||
76 | $this->logger = $logger; |
||
77 | $this->config = $config; |
||
78 | |||
79 | $this->processTimeout = isset($this->config['process_timeout']) ? $this->config['process_timeout'] : null; |
||
80 | |||
81 | $this->running = true; |
||
82 | $this->output = new ConsoleOutput(); |
||
83 | |||
84 | if (php_sapi_name() == 'cli') { |
||
85 | pcntl_signal(SIGTERM, function () { |
||
86 | $this->stop(); |
||
87 | }); |
||
88 | |||
89 | pcntl_signal(SIGINT, function () { |
||
90 | $this->stop(); |
||
91 | }); |
||
92 | } |
||
93 | } |
||
94 | |||
95 | public function setUp($config) |
||
99 | |||
100 | /** |
||
101 | * @param string $name |
||
102 | * |
||
103 | * @return $this |
||
104 | */ |
||
105 | public function attach($name) |
||
113 | |||
114 | /** |
||
115 | * @param int $maxMessages |
||
116 | * @param int $timeout |
||
117 | */ |
||
118 | public function receive($maxMessages = 1, $timeout = null) |
||
126 | |||
127 | /** |
||
128 | * @param array $args |
||
129 | * @param int $priority |
||
130 | */ |
||
131 | public function push(array $args) |
||
144 | |||
145 | /** |
||
146 | * @param string $name |
||
147 | * |
||
148 | * @return $this |
||
149 | */ |
||
150 | public function highPriority() |
||
156 | |||
157 | /** |
||
158 | * @param string $name |
||
159 | * @param int $timeout |
||
160 | */ |
||
161 | public function create($name, $timeout = null) |
||
165 | |||
166 | /** |
||
167 | * @param string $name |
||
168 | * |
||
169 | * @return bool |
||
170 | */ |
||
171 | public function delete($name) |
||
175 | |||
176 | /** |
||
177 | * @param string $queueName |
||
178 | */ |
||
179 | public function showMessages($queueName) |
||
183 | |||
184 | /** |
||
185 | * @return bool |
||
186 | */ |
||
187 | public function flush() |
||
191 | |||
192 | /** |
||
193 | * @return int |
||
194 | */ |
||
195 | public function countMessages() |
||
199 | |||
200 | /** |
||
201 | * @return int |
||
202 | */ |
||
203 | public function count() |
||
207 | |||
208 | /** |
||
209 | * @param ContainerAwareCommand $command |
||
210 | */ |
||
211 | public function setCommand(ContainerAwareCommand $command) |
||
215 | |||
216 | /** |
||
217 | * @param OutputInterface $output |
||
218 | */ |
||
219 | public function setOutput(OutputInterface $output) |
||
223 | |||
224 | /** |
||
225 | * @return OutputInterface |
||
226 | */ |
||
227 | public function getOutput() |
||
231 | |||
232 | public function isEnabled() |
||
236 | |||
237 | public function isRunning() |
||
241 | |||
242 | public function setProcessTimeout($processTimeout) |
||
246 | |||
247 | public function listen($name = null, $sleep = 0, $work = true) |
||
271 | |||
272 | protected function stop() |
||
276 | |||
277 | protected function loop($name = null) |
||
306 | |||
307 | /** |
||
308 | * @param MessageIterator $messages |
||
309 | */ |
||
310 | protected function handle(\ZendQueue\Message\MessageIterator $messages) |
||
320 | |||
321 | protected function run($message) |
||
361 | |||
362 | /** |
||
363 | * @param ZendQueue\Message $message |
||
364 | */ |
||
365 | protected function getUnseralizedBody(\ZendQueue\Message $message) |
||
403 | } |
||
404 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.