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\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 |
||
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 |
||
112 | * @throws \RuntimeException |
||
113 | */ |
||
114 | public function fork() |
||
115 | { |
||
116 | $pid = $this->pcntl->fork(); |
||
117 | if ($pid === -1) { |
||
118 | throw new \RuntimeException('could not fork a new process'); |
||
119 | } |
||
120 | |||
121 | $pid = ($pid === 0) ? getmypid() : $pid; |
||
122 | |||
123 | $fork = new Fork($pid); |
||
124 | $this->forks[$pid] = $fork; |
||
125 | |||
126 | return $fork; |
||
127 | } |
||
128 | |||
129 | /** |
||
130 | * fork master process |
||
131 | * |
||
132 | * @return int $masterPid |
||
133 | */ |
||
134 | public function forkMaster() |
||
135 | { |
||
136 | $pid = $this->pcntl->fork(); |
||
137 | $this->masterPid = ($pid === 0) ? getmypid() : $pid; |
||
138 | $this->log->setMasterPid($this->masterPid); |
||
139 | |||
140 | if ($pid) { |
||
141 | // owner |
||
142 | $this->log->info('pid: ' . getmypid()); |
||
143 | |||
144 | return $this->masterPid; |
||
145 | } elseif ($pid === -1) { |
||
|
|||
146 | // error |
||
147 | } else { |
||
148 | // master |
||
149 | $taskQueue = new TaskQueue($this->ownerPid); |
||
150 | $this->log->info('pid: ' . $this->masterPid); |
||
151 | |||
152 | foreach ($this->signals as $sig) { |
||
153 | $this->pcntl->signal($sig, SIG_DFL, true); |
||
154 | } |
||
155 | $workerCount = 0; |
||
156 | |||
157 | while ($task = $taskQueue->dequeue()) { |
||
158 | $this->log->info('dequeued task #' . $taskQueue->dequeuedCount()); |
||
159 | if ($workerCount >= $this->concurrency) { |
||
160 | $status = null; |
||
161 | $this->pcntl->waitpid(-1, $status); |
||
162 | $workerCount--; |
||
163 | } |
||
164 | $this->forkWorker($task); |
||
165 | $workerCount++; |
||
166 | } |
||
167 | exit; |
||
168 | } |
||
169 | } |
||
170 | |||
171 | /** |
||
172 | * fork worker process |
||
173 | * |
||
174 | * @param \Ackintosh\Snidel\Task |
||
175 | * @return void |
||
176 | * @throws \RuntimeException |
||
177 | */ |
||
178 | private function forkWorker($task) |
||
179 | { |
||
180 | try { |
||
181 | $fork = $this->fork(); |
||
182 | } catch (\RuntimeException $e) { |
||
183 | $this->log->error($e->getMessage()); |
||
184 | throw $e; |
||
185 | } |
||
186 | |||
187 | if (getmypid() === $this->masterPid) { |
||
188 | // master |
||
189 | $this->log->info('forked worker. pid: ' . $fork->getPid()); |
||
190 | } else { |
||
191 | // worker |
||
192 | $this->log->info('has forked. pid: ' . getmypid()); |
||
193 | // @codeCoverageIgnoreStart |
||
194 | |||
195 | foreach ($this->signals as $sig) { |
||
196 | $this->pcntl->signal($sig, SIG_DFL, true); |
||
197 | } |
||
198 | |||
199 | $resultQueue = new ResultQueue($this->ownerPid); |
||
200 | $resultHasQueued = false; |
||
201 | register_shutdown_function(function () use (&$resultHasQueued, $fork, $task, $resultQueue) { |
||
202 | if (!$resultHasQueued) { |
||
203 | $result = new Result(); |
||
204 | $result->setFailure(); |
||
205 | $result->setTask($task); |
||
206 | $result->setFork($fork); |
||
207 | $resultQueue->enqueue($result); |
||
208 | } |
||
209 | }); |
||
210 | |||
211 | $this->log->info('----> started the function.'); |
||
212 | $result = $task->execute(); |
||
213 | $result->setFork($fork); |
||
214 | $this->log->info('<---- completed the function.'); |
||
215 | |||
216 | try { |
||
217 | $resultQueue->enqueue($result); |
||
218 | } catch (\RuntimeException $e) { |
||
219 | $this->log->error($e->getMessage()); |
||
220 | $result->setFailure(); |
||
221 | $resultQueue->enqueue($result); |
||
222 | } |
||
223 | $resultHasQueued = true; |
||
224 | $this->log->info('queued the result and exit.'); |
||
225 | exit; |
||
226 | // @codeCoverageIgnoreEnd |
||
227 | } |
||
228 | } |
||
229 | |||
230 | /** |
||
231 | * @return bool |
||
232 | */ |
||
233 | public function existsMaster() |
||
237 | |||
238 | /** |
||
239 | * kill master process |
||
240 | * |
||
241 | * @return void |
||
242 | */ |
||
243 | public function killMaster() |
||
247 | |||
248 | /** |
||
249 | * |
||
250 | * @param string $tag |
||
251 | * @return bool |
||
252 | */ |
||
253 | public function hasTag($tag) |
||
263 | |||
264 | /** |
||
265 | * @return void |
||
266 | */ |
||
267 | public function wait() |
||
279 | |||
280 | /** |
||
281 | * wait child |
||
282 | * |
||
283 | * @return \Ackintosh\Snidel\Result\Result |
||
284 | */ |
||
285 | public function waitForChild() |
||
286 | { |
||
287 | $status = null; |
||
288 | $childPid = $this->pcntl->waitpid(-1, $status); |
||
289 | try { |
||
290 | $result = $this->dataRepository->load($childPid)->readAndDelete(); |
||
291 | } catch (SharedMemoryControlException $e) { |
||
292 | throw $e; |
||
293 | } |
||
294 | $fork = $result->getFork(); |
||
295 | $fork->setStatus($status); |
||
296 | $result->setFork($fork); |
||
297 | |||
298 | if ($result->isFailure() || !$this->pcntl->wifexited($status) || $this->pcntl->wexitstatus($status) !== 0) { |
||
299 | $this->error[$childPid] = $fork; |
||
300 | } |
||
301 | $this->results[$childPid] = $result; |
||
302 | |||
303 | return $result; |
||
304 | } |
||
305 | |||
306 | /** |
||
307 | * @return array |
||
308 | */ |
||
309 | public function getChildPids() |
||
313 | |||
314 | /** |
||
315 | * return fork |
||
316 | * |
||
317 | * @param int $pid |
||
318 | * @return \Ackintosh\Snidel\Fork |
||
319 | */ |
||
320 | public function get($pid) |
||
324 | |||
325 | public function getCollection($tag = null) |
||
336 | |||
337 | /** |
||
338 | * return results |
||
339 | * |
||
340 | * @param string $tag |
||
341 | * @return \Ackintosh\Snidel\Result\Collection |
||
342 | */ |
||
343 | private function getCollectionWithTag($tag) |
||
357 | |||
358 | /** |
||
359 | * @return bool |
||
360 | */ |
||
361 | public function hasError() |
||
365 | |||
366 | /** |
||
367 | * @return \Ackintosh\Sniden\Error |
||
368 | */ |
||
369 | public function getError() |
||
373 | |||
374 | public function __destruct() |
||
379 | } |
||
380 |
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.