Complex classes like Snidel 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 Snidel, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class Snidel |
||
17 | { |
||
18 | /** @var string */ |
||
19 | const VERSION = '0.6.0'; |
||
20 | |||
21 | /** @var \Ackintosh\Snidel\ForkContainer */ |
||
22 | private $forkContainer; |
||
23 | |||
24 | /** @var \Ackintosh\Snidel\Pcntl */ |
||
25 | private $pcntl; |
||
26 | |||
27 | /** @var int */ |
||
28 | private $concurrency; |
||
29 | |||
30 | /** @var \Ackintosh\Snidel\Log */ |
||
31 | private $log; |
||
32 | |||
33 | /** @var bool */ |
||
34 | private $joined = false; |
||
35 | |||
36 | /** @var int */ |
||
37 | private $ownerPid; |
||
38 | |||
39 | /** @var array */ |
||
40 | private $signals = array( |
||
41 | SIGTERM, |
||
42 | SIGINT, |
||
43 | ); |
||
44 | |||
45 | /** @var int */ |
||
46 | private $receivedSignal; |
||
47 | |||
48 | /** @var \Ackintosh\Snidel\Token */ |
||
49 | private $processToken; |
||
|
|||
50 | |||
51 | /** @var bool */ |
||
52 | private $exceptionHasOccured = false; |
||
53 | |||
54 | public function __construct($concurrency = 5) |
||
82 | |||
83 | /** |
||
84 | * sets the resource for the log. |
||
85 | * |
||
86 | * @param resource $resource |
||
87 | * @return void |
||
88 | * @codeCoverageIgnore |
||
89 | */ |
||
90 | public function setLoggingDestination($resource) |
||
94 | |||
95 | /** |
||
96 | * this method uses master / worker model. |
||
97 | * |
||
98 | * @param callable $callable |
||
99 | * @param mixed $args |
||
100 | * @param string $tag |
||
101 | * @return void |
||
102 | * @throws \RuntimeException |
||
103 | */ |
||
104 | public function fork($callable, $args = array(), $tag = null) |
||
118 | |||
119 | /** |
||
120 | * fork process |
||
121 | * the processes which forked are wait for token. |
||
122 | * |
||
123 | * @param callable $callable |
||
124 | * @param mixed $args |
||
125 | * @param string $tag |
||
126 | * @param \Ackintosh\Snidel\Token $token |
||
127 | * @return void |
||
128 | * @throws \RuntimeException |
||
129 | */ |
||
130 | private function forkChild(Token $token, $callable, $args = array(), $tag = null) |
||
131 | { |
||
132 | $task = new Task($callable, $args, $tag); |
||
133 | |||
134 | try { |
||
135 | $fork = $this->forkContainer->fork($task); |
||
136 | } catch (\RuntimeException $e) { |
||
137 | $this->log->error($e->getMessage()); |
||
138 | throw $e; |
||
139 | } |
||
140 | |||
141 | if (getmypid() === $this->ownerPid) { |
||
142 | // parent |
||
143 | $this->log->info('created child process. pid: ' . $fork->getPid()); |
||
144 | } else { |
||
145 | // @codeCoverageIgnoreStart |
||
146 | // child |
||
147 | foreach ($this->signals as $sig) { |
||
148 | $this->pcntl->signal($sig, SIG_DFL, true); |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | * in php5.3, we can not use $this in anonymous functions |
||
153 | */ |
||
154 | $log = $this->log; |
||
155 | register_shutdown_function(function () use ($fork, $log, $token) { |
||
156 | $dataRepository = new DataRepository(); |
||
157 | $data = $dataRepository->load(getmypid()); |
||
158 | try { |
||
159 | $data->write($fork); |
||
160 | } catch (SharedMemoryControlException $e) { |
||
161 | throw $e; |
||
162 | } |
||
163 | $log->info('<-- return token.'); |
||
164 | $token->back(); |
||
165 | }); |
||
166 | |||
167 | $log->info('--> waiting for the token come around.'); |
||
168 | if ($token->accept()) { |
||
169 | $log->info('----> started the function.'); |
||
170 | $fork->executeTask(); |
||
171 | $log->info('<---- completed the function.'); |
||
172 | } |
||
173 | |||
174 | $this->_exit(); |
||
175 | // @codeCoverageIgnoreEnd |
||
176 | } |
||
177 | |||
178 | return $fork->getPid(); |
||
179 | } |
||
180 | |||
181 | /** |
||
182 | * waits until all tasks that queued by Snidel::fork() are completed |
||
183 | * |
||
184 | * @return void |
||
185 | */ |
||
186 | public function wait() |
||
191 | |||
192 | /** |
||
193 | * @return bool |
||
194 | */ |
||
195 | public function hasError() |
||
199 | |||
200 | /** |
||
201 | * @return \Ackintosh\Snidel\Error |
||
202 | */ |
||
203 | public function getError() |
||
207 | |||
208 | /** |
||
209 | * gets results |
||
210 | * |
||
211 | * @param string $tag |
||
212 | * @return \Ackintosh\Snidel\ForkCollection |
||
213 | * @throws \InvalidArgumentException |
||
214 | */ |
||
215 | public function get($tag = null) |
||
226 | |||
227 | /** |
||
228 | * sends signal to child |
||
229 | * |
||
230 | * @param int $sig |
||
231 | * @return void |
||
232 | */ |
||
233 | public function sendSignalToChildren($sig) |
||
240 | |||
241 | public function setReceivedSignal($sig) |
||
245 | |||
246 | /** |
||
247 | * delete shared memory |
||
248 | * |
||
249 | * @return void |
||
250 | * @throws \Ackintosh\Snidel\Exception\SharedMemoryControlException |
||
251 | */ |
||
252 | private function deleteAllData() |
||
261 | |||
262 | /** |
||
263 | * create map object |
||
264 | * |
||
265 | * @param array $args |
||
266 | * @param callable $callable |
||
267 | * @return \Ackintosh\Snidel\MapContainer |
||
268 | */ |
||
269 | public function map(Array $args, $callable) |
||
273 | |||
274 | /** |
||
275 | * run map object |
||
276 | * |
||
277 | * @param \Ackintosh\Snidel\MapContainer |
||
278 | * @return array |
||
279 | * @throws \RuntimeException |
||
280 | */ |
||
281 | public function run(MapContainer $mapContainer) |
||
294 | |||
295 | /** |
||
296 | * fork the first processing of the map container |
||
297 | * |
||
298 | * @param \Ackintosh\Snidel\MapContainer |
||
299 | * @return void |
||
300 | * @throws \RuntimeException |
||
301 | */ |
||
302 | private function forkTheFirstProcessing(MapContainer $mapContainer, $token) |
||
314 | |||
315 | /** |
||
316 | * waits and connects the process of map container |
||
317 | * |
||
318 | * @param \Ackintosh\Snidel\MapContainer |
||
319 | * @return void |
||
320 | * @throws \RuntimeException |
||
321 | */ |
||
322 | private function waitsAndConnectsProcess(MapContainer $mapContainer) |
||
323 | { |
||
324 | if ($this->joined) { |
||
325 | return; |
||
326 | } |
||
327 | |||
328 | while ($mapContainer->isProcessing()) { |
||
329 | try { |
||
330 | $fork = $this->forkContainer->waitForChild(); |
||
331 | } catch (SharedMemoryControlException $e) { |
||
332 | throw $e; |
||
333 | } |
||
334 | |||
335 | $childPid = $fork->getPid(); |
||
336 | if ($fork->hasNotFinishedSuccessfully()) { |
||
337 | $message = 'an error has occurred in child process. pid: ' . $childPid; |
||
338 | $this->log->error($message); |
||
339 | throw new \RuntimeException($message); |
||
340 | } |
||
341 | |||
342 | if ($nextMap = $mapContainer->nextMap($childPid)) { |
||
343 | try { |
||
344 | $nextMapPid = $this->forkChild( |
||
345 | $nextMap->getToken(), |
||
346 | $nextMap->getCallable(), |
||
347 | $fork |
||
348 | ); |
||
349 | } catch (\RuntimeException $e) { |
||
350 | throw $e; |
||
351 | } |
||
352 | $message = sprintf('processing is connected from [%d] to [%d]', $childPid, $nextMapPid); |
||
353 | $this->log->info($message); |
||
354 | $nextMap->countTheForked(); |
||
355 | $nextMap->addChildPid($nextMapPid); |
||
356 | } |
||
357 | $mapContainer->countTheCompleted($childPid); |
||
358 | } |
||
359 | |||
360 | $this->joined = true; |
||
361 | } |
||
362 | |||
363 | /** |
||
364 | * gets results of map container |
||
365 | * |
||
366 | * @param \Ackintosh\Snidel\MapContainer |
||
367 | * @return array |
||
368 | */ |
||
369 | private function getResultsOf(MapContainer $mapContainer) |
||
378 | |||
379 | private function _exit($status = 0) |
||
383 | |||
384 | public function __destruct() |
||
415 | } |
||
416 |
This check marks private properties in classes that are never used. Those properties can be removed.