Complex classes like Container 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 Container, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class Container |
||
15 | { |
||
16 | /** @var int */ |
||
17 | private $ownerPid; |
||
18 | |||
19 | /** @var int */ |
||
20 | private $masterPid; |
||
21 | |||
22 | /** @var \Ackintosh\Snidel\Fork\Fork[] */ |
||
23 | private $forks = array(); |
||
24 | |||
25 | /** @var \Ackintosh\Snidel\Result\Result[] */ |
||
26 | private $results = array(); |
||
27 | |||
28 | /** @var \Ackintosh\Snidel\Pcntl */ |
||
29 | private $pcntl; |
||
30 | |||
31 | /** @var \Ackintosh\Snidel\DataRepository */ |
||
32 | private $dataRepository; |
||
33 | |||
34 | /** @var \Ackintosh\Snidel\Error */ |
||
35 | private $error; |
||
36 | |||
37 | /** @var \Ackintosh\Snidel\Task\Queue */ |
||
38 | private $taskQueue; |
||
39 | |||
40 | /** @var \Ackintosh\Snidel\Result\Queue */ |
||
41 | private $resultQueue; |
||
42 | |||
43 | /** @var \Ackintosh\Snidel\Log */ |
||
44 | private $log; |
||
45 | |||
46 | /** @var array */ |
||
47 | private $signals = array( |
||
48 | SIGTERM, |
||
49 | SIGINT, |
||
50 | ); |
||
51 | |||
52 | /** @var int */ |
||
53 | private $concurrency; |
||
54 | |||
55 | /** |
||
56 | * @param int $ownerPid |
||
57 | */ |
||
58 | public function __construct($ownerPid, $log, $concurrency = 5) |
||
69 | |||
70 | /** |
||
71 | * @param \Ackintosh\Snidel\Task |
||
72 | * @return void |
||
73 | * @throws \RuntimeException |
||
74 | */ |
||
75 | public function enqueue($task) |
||
83 | |||
84 | /** |
||
85 | * @return int |
||
86 | */ |
||
87 | public function queuedCount() |
||
91 | |||
92 | /** |
||
93 | * @return \Ackintosh\Snidel\Fork\Fork |
||
94 | */ |
||
95 | private function dequeue() |
||
99 | |||
100 | /** |
||
101 | * @return int |
||
102 | */ |
||
103 | public function dequeuedCount() |
||
107 | |||
108 | /** |
||
109 | * fork process |
||
110 | * |
||
111 | * @return \Ackintosh\Snidel\Fork\Fork |
||
112 | * @throws \RuntimeException |
||
113 | */ |
||
114 | public function fork() |
||
128 | |||
129 | /** |
||
130 | * fork master process |
||
131 | * |
||
132 | * @return int $masterPid |
||
133 | */ |
||
134 | public function forkMaster() |
||
174 | |||
175 | /** |
||
176 | * fork worker process |
||
177 | * |
||
178 | * @param \Ackintosh\Snidel\Task |
||
179 | * @return void |
||
180 | * @throws \RuntimeException |
||
181 | */ |
||
182 | private function forkWorker($task) |
||
233 | |||
234 | /** |
||
235 | * @return bool |
||
236 | */ |
||
237 | public function existsMaster() |
||
241 | |||
242 | /** |
||
243 | * send signal to master process |
||
244 | * |
||
245 | * @return void |
||
246 | */ |
||
247 | public function sendSignalToMaster($sig = SIGTERM) |
||
258 | |||
259 | /** |
||
260 | * |
||
261 | * @param string $tag |
||
262 | * @return bool |
||
263 | */ |
||
264 | public function hasTag($tag) |
||
274 | |||
275 | /** |
||
276 | * @return void |
||
277 | */ |
||
278 | public function wait() |
||
290 | |||
291 | /** |
||
292 | * wait child |
||
293 | * |
||
294 | * @return \Ackintosh\Snidel\Result\Result |
||
295 | */ |
||
296 | public function waitForChild() |
||
316 | |||
317 | /** |
||
318 | * @return array |
||
319 | */ |
||
320 | public function getChildPids() |
||
324 | |||
325 | /** |
||
326 | * return fork |
||
327 | * |
||
328 | * @param int $pid |
||
329 | * @return \Ackintosh\Snidel\Fork\Fork |
||
330 | */ |
||
331 | public function get($pid) |
||
335 | |||
336 | public function getCollection($tag = null) |
||
347 | |||
348 | /** |
||
349 | * return results |
||
350 | * |
||
351 | * @param string $tag |
||
352 | * @return \Ackintosh\Snidel\Result\Collection |
||
353 | */ |
||
354 | private function getCollectionWithTag($tag) |
||
368 | |||
369 | /** |
||
370 | * @return bool |
||
371 | */ |
||
372 | public function hasError() |
||
376 | |||
377 | /** |
||
378 | * @return \Ackintosh\Sniden\Error |
||
379 | */ |
||
380 | public function getError() |
||
384 | |||
385 | public function __destruct() |
||
390 | } |
||
391 |
This check looks for the bodies of
elseif
statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.These
elseif
bodies can be removed. If you have an empty elseif but statements in theelse
branch, consider inverting the condition.