Complex classes like ForkContainer 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 ForkContainer, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class ForkContainer |
||
15 | { |
||
16 | /** @var int */ |
||
17 | private $ownerPid; |
||
18 | |||
19 | /** @var int */ |
||
20 | private $masterPid; |
||
21 | |||
22 | /** @var \Ackintosh\Snidel\Fork[] */ |
||
23 | private $forks = array(); |
||
24 | |||
25 | /** @var \Ackintosh\Snidel\Pcntl */ |
||
26 | private $pcntl; |
||
27 | |||
28 | /** @var \Ackintosh\Snidel\DataRepository */ |
||
29 | private $dataRepository; |
||
30 | |||
31 | /** @var \Ackintosh\Snidel\Error */ |
||
32 | private $error; |
||
33 | |||
34 | /** @var \Ackintosh\Snidel\TaskQueue */ |
||
35 | private $taskQueue; |
||
36 | |||
37 | /** @var \Ackintosh\Snidel\ResultQueue */ |
||
38 | private $resultQueue; |
||
39 | |||
40 | /** @var \Ackintosh\Snidel\Log */ |
||
41 | private $log; |
||
42 | |||
43 | /** @var array */ |
||
44 | private $signals = array( |
||
45 | SIGTERM, |
||
46 | SIGINT, |
||
47 | ); |
||
48 | |||
49 | /** |
||
50 | * @param int $ownerPid |
||
51 | */ |
||
52 | public function __construct($ownerPid, $log, $concurrency = 5) |
||
63 | |||
64 | /** |
||
65 | * @param \Ackintosh\Snidel\Task |
||
66 | * @return void |
||
67 | */ |
||
68 | public function enqueue($task) |
||
72 | |||
73 | /** |
||
74 | * @return int |
||
75 | */ |
||
76 | public function queuedCount() |
||
80 | |||
81 | /** |
||
82 | * @return \Ackintosh\Snidel\Fork |
||
83 | */ |
||
84 | private function dequeue() |
||
88 | |||
89 | /** |
||
90 | * @return int |
||
91 | */ |
||
92 | public function dequeuedCount() |
||
96 | |||
97 | /** |
||
98 | * fork process |
||
99 | * |
||
100 | * @param \Ackintosh\Snidel\Task |
||
101 | * @return \Ackintosh\Snidel\Fork |
||
102 | * @throws \RuntimeException |
||
103 | */ |
||
104 | public function fork($task) |
||
118 | |||
119 | /** |
||
120 | * fork master process |
||
121 | * |
||
122 | * @return int $masterPid |
||
123 | */ |
||
124 | public function forkMaster() |
||
155 | |||
156 | /** |
||
157 | * fork worker process |
||
158 | * |
||
159 | * @param \Ackintosh\Snidel\Task |
||
160 | * @return void |
||
161 | * @throws \RuntimeException |
||
162 | */ |
||
163 | private function forkWorker($task) |
||
208 | |||
209 | /** |
||
210 | * @return bool |
||
211 | */ |
||
212 | public function existsMaster() |
||
216 | |||
217 | /** |
||
218 | * kill master process |
||
219 | * |
||
220 | * @return void |
||
221 | */ |
||
222 | public function killMaster() |
||
226 | |||
227 | /** |
||
228 | * |
||
229 | * @param string $tag |
||
230 | * @return bool |
||
231 | */ |
||
232 | public function hasTag($tag) |
||
242 | |||
243 | /** |
||
244 | * @return void |
||
245 | */ |
||
246 | public function wait() |
||
257 | |||
258 | /** |
||
259 | * wait child |
||
260 | * |
||
261 | * @return \Ackintosh\Snidel\Fork |
||
262 | */ |
||
263 | public function waitSimply() |
||
281 | |||
282 | /** |
||
283 | * @return array |
||
284 | */ |
||
285 | public function getChildPids() |
||
289 | |||
290 | /** |
||
291 | * return fork |
||
292 | * |
||
293 | * @param int $pid |
||
294 | * @return \Ackintosh\Snidel\Fork |
||
295 | */ |
||
296 | public function get($pid) |
||
300 | |||
301 | public function getCollection($tag = null) |
||
309 | |||
310 | /** |
||
311 | * return forks |
||
312 | * |
||
313 | * @param string $tag |
||
314 | * @return \Ackintosh\Snidel\Fork[] |
||
315 | */ |
||
316 | private function getCollectionWithTag($tag) |
||
324 | |||
325 | /** |
||
326 | * @return bool |
||
327 | */ |
||
328 | public function hasError() |
||
332 | |||
333 | /** |
||
334 | * @return \Ackintosh\Sniden\Error |
||
335 | */ |
||
336 | public function getError() |
||
340 | |||
341 | public function __destruct() |
||
346 | } |
||
347 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: