Complex classes like Generic 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 Generic, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | abstract class Generic |
||
16 | { |
||
17 | use \PHPDaemon\Traits\ClassWatchdog; |
||
18 | use \PHPDaemon\Traits\StaticObjectWatchdog; |
||
19 | |||
20 | /** |
||
21 | * Process identificator |
||
22 | * @var int |
||
23 | */ |
||
24 | public $id; |
||
25 | |||
26 | /** |
||
27 | * PID |
||
28 | * @var int |
||
29 | */ |
||
30 | protected $pid; |
||
31 | |||
32 | /** |
||
33 | * Is this thread shutdown? |
||
34 | * @var boolean |
||
35 | */ |
||
36 | protected $shutdown = false; |
||
37 | |||
38 | /** |
||
39 | * Is this thread terminated? |
||
40 | * @var boolean |
||
41 | */ |
||
42 | protected $terminated = false; |
||
43 | |||
44 | /** |
||
45 | * Collections of childrens |
||
46 | * @var array|Collection[] |
||
47 | */ |
||
48 | protected $collections = []; |
||
49 | |||
50 | /** |
||
51 | * Storage of signal handler events |
||
52 | * @var array |
||
53 | */ |
||
54 | protected $sigEvents = []; |
||
55 | |||
56 | /** |
||
57 | * Hash of known signal [no => name, ...] |
||
58 | * @var array |
||
59 | */ |
||
60 | public static $signals = [ |
||
61 | SIGHUP => 'SIGHUP', |
||
62 | SIGSYS => 'SIGSYS', |
||
63 | SIGPIPE => 'SIGPIPE', |
||
64 | SIGALRM => 'SIGALRM', |
||
65 | SIGTERM => 'SIGTERM', |
||
66 | SIGSTOP => 'SIGSTOP', |
||
67 | SIGINT => 'SIGINT', |
||
68 | SIGCHLD => 'SIGCHLD', |
||
69 | SIGTTIN => 'SIGTTIN', |
||
70 | SIGTTOU => 'SIGTTOU', |
||
71 | SIGIO => 'SIGIO', |
||
72 | SIGXCPU => 'SIGXCPU', |
||
73 | SIGXFSZ => 'SIGXFSZ', |
||
74 | SIGVTALRM => 'SIGVTALRM', |
||
75 | SIGPROF => 'SIGPROF', |
||
76 | SIGWINCH => 'SIGWINCH', |
||
77 | SIGUSR1 => 'SIGUSR1', |
||
78 | SIGUSR2 => 'SIGUSR2', |
||
79 | SIGTSTP => 'SIGTSTP', |
||
80 | ]; |
||
81 | |||
82 | /** |
||
83 | * Get PID of this Thread |
||
84 | * @return integer |
||
85 | */ |
||
86 | public function getPid() |
||
90 | |||
91 | /** |
||
92 | * Set ID of this Thread |
||
93 | * @param integer Id |
||
94 | * @return void |
||
95 | */ |
||
96 | public function setId($id) |
||
100 | |||
101 | /** |
||
102 | * Get ID of this Thread |
||
103 | * @return integer |
||
104 | */ |
||
105 | public function getId() |
||
109 | |||
110 | /** |
||
111 | * Is this thread terminated? |
||
112 | * @return boolean |
||
113 | */ |
||
114 | public function isTerminated() |
||
118 | |||
119 | protected function onTerminated() |
||
122 | |||
123 | public function setTerminated() |
||
128 | |||
129 | /** |
||
130 | * Invoke magic method |
||
131 | * @return void |
||
132 | */ |
||
133 | |||
134 | public function __invoke() |
||
139 | |||
140 | /** |
||
141 | * Register signals. |
||
142 | * @return void |
||
143 | */ |
||
144 | protected function registerEventSignals() |
||
162 | |||
163 | /** |
||
164 | * Unregister signals. |
||
165 | * @return void |
||
166 | */ |
||
167 | protected function unregisterSignals() |
||
174 | |||
175 | /** |
||
176 | * Called when a signal caught through libevent. |
||
177 | * @param integer Signal's number. |
||
178 | * @param integer Events. |
||
179 | * @param mixed Argument. |
||
180 | * @return void |
||
181 | */ |
||
182 | public function eventSighandler($fd, $arg) |
||
186 | |||
187 | /** |
||
188 | * Run thread process |
||
189 | * @return void |
||
190 | */ |
||
191 | protected function run() |
||
194 | |||
195 | /** |
||
196 | * If true, we do not register signals automatically at start |
||
197 | * @var boolean |
||
198 | */ |
||
199 | protected $delayedSigReg = false; |
||
200 | |||
201 | /** |
||
202 | * Registers signals |
||
203 | * @return void |
||
204 | */ |
||
205 | protected function registerSignals() |
||
217 | |||
218 | /** |
||
219 | * Starts the process |
||
220 | * @return void |
||
221 | */ |
||
222 | public function start($clearstack = true) |
||
244 | |||
245 | /** |
||
246 | * Called when the signal is caught |
||
247 | * @param integer Signal's number |
||
248 | * @return void |
||
249 | */ |
||
250 | public function sighandler($signo) |
||
262 | |||
263 | /** |
||
264 | * Shutdowns the current process properly |
||
265 | * @return void |
||
266 | */ |
||
267 | protected function shutdown() |
||
272 | |||
273 | /** |
||
274 | * Sends the signal to parent process |
||
275 | * @param integer Signal's number |
||
276 | * @return boolean Success |
||
277 | */ |
||
278 | protected function backsig($sig) |
||
282 | |||
283 | /** |
||
284 | * Delays the process execution for the given number of seconds |
||
285 | * @param integer Sleep time in seconds |
||
286 | * @return boolean Success |
||
287 | */ |
||
288 | public function sleep($s) |
||
303 | |||
304 | /** |
||
305 | * Called when the signal SIGCHLD caught |
||
306 | * @return void |
||
307 | */ |
||
308 | protected function sigchld() |
||
312 | |||
313 | /** |
||
314 | * Called when the signal SIGTERM caught |
||
315 | * @return void |
||
316 | */ |
||
317 | protected function sigterm() |
||
321 | |||
322 | /** |
||
323 | * Called when the signal SIGINT caught |
||
324 | * @return void |
||
325 | */ |
||
326 | protected function sigint() |
||
330 | |||
331 | /** |
||
332 | * Called when the signal SIGTERM caught |
||
333 | * @return void |
||
334 | */ |
||
335 | protected function sigquit() |
||
339 | |||
340 | /** |
||
341 | * Called when the signal SIGKILL caught |
||
342 | * @return void |
||
343 | */ |
||
344 | protected function sigkill() |
||
348 | |||
349 | /** |
||
350 | * Terminates the process |
||
351 | * @param boolean Kill? |
||
352 | * @return void |
||
353 | */ |
||
354 | public function stop($kill = false) |
||
359 | |||
360 | /** |
||
361 | * Checks for SIGCHLD |
||
362 | * @return boolean Success |
||
363 | */ |
||
364 | protected function waitPid() |
||
382 | |||
383 | /** |
||
384 | * Sends arbitrary signal to the process |
||
385 | * @param integer Signal's number |
||
386 | * @return boolean Success |
||
387 | */ |
||
388 | public function signal($sig) |
||
392 | |||
393 | /** |
||
394 | * Checks if this process does exist |
||
395 | * @return boolean Success |
||
396 | */ |
||
397 | public function ifExists() |
||
401 | |||
402 | /** |
||
403 | * Checks if given process ID does exist |
||
404 | * @static |
||
405 | * @param integer PID |
||
406 | * @param integer $pid |
||
407 | * @return boolean Success |
||
408 | */ |
||
409 | public static function ifExistsByPid($pid) |
||
413 | |||
414 | /** |
||
415 | * Waits until children is alive |
||
416 | * @param boolean $check |
||
417 | * @return void |
||
418 | */ |
||
419 | protected function waitAll($check) |
||
432 | |||
433 | /** |
||
434 | * Sets a title of the current process |
||
435 | * |
||
436 | * @param string $title |
||
437 | * @return boolean Success |
||
438 | */ |
||
439 | protected function setTitle($title) |
||
443 | |||
444 | /** |
||
445 | * Returns a title of the current process |
||
446 | * |
||
447 | * @return string |
||
448 | */ |
||
449 | protected function getTitle() |
||
453 | |||
454 | /** |
||
455 | * Waits for signals, with a timeout |
||
456 | * @param int Seconds |
||
457 | * @param int Nanoseconds |
||
458 | * @return boolean Success |
||
459 | */ |
||
460 | protected function sigwait($sec = 0, $nano = 0.3e9) |
||
482 | |||
483 | /** |
||
484 | * Implementation of pcntl_sigtimedwait for Mac. |
||
485 | * |
||
486 | * @param array Signal |
||
487 | * @param null|array SigInfo |
||
488 | * @param int Seconds |
||
489 | * @param int Nanoseconds |
||
490 | * @param integer $sec |
||
491 | * @param double $nano |
||
492 | * @return boolean Success |
||
493 | */ |
||
494 | protected function sigtimedwait($signals, $siginfo, $sec, $nano) |
||
503 | } |
||
504 |
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: