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 |
||
16 | class Container |
||
17 | { |
||
18 | /** @var int */ |
||
19 | private $ownerPid; |
||
20 | |||
21 | /** @var int */ |
||
22 | private $masterPid; |
||
23 | |||
24 | /** @var int[] */ |
||
25 | private $workerPids = array(); |
||
|
|||
26 | |||
27 | /** @var \Ackintosh\Snidel\Fork\Fork[] */ |
||
28 | private $forks = array(); |
||
29 | |||
30 | /** @var \Ackintosh\Snidel\Result\Result[] */ |
||
31 | private $results = array(); |
||
32 | |||
33 | /** @var \Ackintosh\Snidel\Pcntl */ |
||
34 | private $pcntl; |
||
35 | |||
36 | /** @var \Ackintosh\Snidel\DataRepository */ |
||
37 | private $dataRepository; |
||
38 | |||
39 | /** @var \Ackintosh\Snidel\Error */ |
||
40 | private $error; |
||
41 | |||
42 | /** @var \Ackintosh\Snidel\Task\Queue */ |
||
43 | private $taskQueue; |
||
44 | |||
45 | /** @var \Ackintosh\Snidel\Result\Queue */ |
||
46 | private $resultQueue; |
||
47 | |||
48 | /** @var \Ackintosh\Snidel\Log */ |
||
49 | private $log; |
||
50 | |||
51 | /** @var array */ |
||
52 | private $signals = array( |
||
53 | SIGTERM, |
||
54 | SIGINT, |
||
55 | ); |
||
56 | |||
57 | /** @var int */ |
||
58 | private $concurrency; |
||
59 | |||
60 | /** |
||
61 | * @param int $ownerPid |
||
62 | */ |
||
63 | public function __construct($ownerPid, $log, $concurrency = 5) |
||
74 | |||
75 | /** |
||
76 | * @param \Ackintosh\Snidel\Task |
||
77 | * @return void |
||
78 | * @throws \RuntimeException |
||
79 | */ |
||
80 | public function enqueue($task) |
||
88 | |||
89 | /** |
||
90 | * @return int |
||
91 | */ |
||
92 | public function queuedCount() |
||
96 | |||
97 | /** |
||
98 | * @return \Ackintosh\Snidel\Fork\Fork |
||
99 | */ |
||
100 | private function dequeue() |
||
104 | |||
105 | /** |
||
106 | * @return int |
||
107 | */ |
||
108 | public function dequeuedCount() |
||
112 | |||
113 | /** |
||
114 | * fork process |
||
115 | * |
||
116 | * @return \Ackintosh\Snidel\Fork\Fork |
||
117 | * @throws \RuntimeException |
||
118 | */ |
||
119 | public function fork() |
||
133 | |||
134 | /** |
||
135 | * fork master process |
||
136 | * |
||
137 | * @return int $masterPid |
||
138 | */ |
||
139 | public function forkMaster() |
||
191 | |||
192 | /** |
||
193 | * fork worker process |
||
194 | * |
||
195 | * @param \Ackintosh\Snidel\Task |
||
196 | * @return \Ackintosh\Snidel\Worker |
||
197 | * @throws \RuntimeException |
||
198 | */ |
||
199 | private function forkWorker($task) |
||
247 | |||
248 | /** |
||
249 | * @return bool |
||
250 | */ |
||
251 | public function existsMaster() |
||
255 | |||
256 | /** |
||
257 | * send signal to master process |
||
258 | * |
||
259 | * @return void |
||
260 | */ |
||
261 | public function sendSignalToMaster($sig = SIGTERM) |
||
272 | |||
273 | /** |
||
274 | * |
||
275 | * @param string $tag |
||
276 | * @return bool |
||
277 | */ |
||
278 | public function hasTag($tag) |
||
288 | |||
289 | /** |
||
290 | * @return void |
||
291 | */ |
||
292 | public function wait() |
||
304 | |||
305 | /** |
||
306 | * wait child |
||
307 | * |
||
308 | * @return \Ackintosh\Snidel\Result\Result |
||
309 | */ |
||
310 | public function waitForChild() |
||
330 | |||
331 | /** |
||
332 | * @return array |
||
333 | */ |
||
334 | public function getChildPids() |
||
338 | |||
339 | /** |
||
340 | * return fork |
||
341 | * |
||
342 | * @param int $pid |
||
343 | * @return \Ackintosh\Snidel\Fork\Fork |
||
344 | */ |
||
345 | public function get($pid) |
||
349 | |||
350 | public function getCollection($tag = null) |
||
361 | |||
362 | /** |
||
363 | * return results |
||
364 | * |
||
365 | * @param string $tag |
||
366 | * @return \Ackintosh\Snidel\Result\Collection |
||
367 | */ |
||
368 | private function getCollectionWithTag($tag) |
||
382 | |||
383 | /** |
||
384 | * @return bool |
||
385 | */ |
||
386 | public function hasError() |
||
390 | |||
391 | /** |
||
392 | * @return \Ackintosh\Sniden\Error |
||
393 | */ |
||
394 | public function getError() |
||
398 | |||
399 | public function __destruct() |
||
404 | } |
||
405 |
This check marks private properties in classes that are never used. Those properties can be removed.