Complex classes like Process 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 Process, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | class Process |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * @var Runnable|callable |
||
| 15 | */ |
||
| 16 | protected $runnable; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @var int |
||
| 20 | */ |
||
| 21 | protected $pid = 0; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var string custom process name |
||
| 25 | */ |
||
| 26 | protected $name = null; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var bool if the process is started |
||
| 30 | */ |
||
| 31 | protected $started = false; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var bool |
||
| 35 | */ |
||
| 36 | protected $running = false; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var int the signal which made the process terminate |
||
| 40 | */ |
||
| 41 | protected $term_signal = null; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var int the signal which made the process stop |
||
| 45 | */ |
||
| 46 | protected $stop_signal = null; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var int error code |
||
| 50 | */ |
||
| 51 | protected $errno = null; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var string error message |
||
| 55 | */ |
||
| 56 | protected $errmsg = null; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var bool |
||
| 60 | */ |
||
| 61 | protected $if_signal = false; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var array |
||
| 65 | */ |
||
| 66 | protected $callbacks = array(); |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var array signal handlers |
||
| 70 | */ |
||
| 71 | protected $signal_handlers = array(); |
||
| 72 | |||
| 73 | /** |
||
| 74 | * event name of before process start |
||
| 75 | */ |
||
| 76 | const BEFORE_START = 'beforeStart'; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * event name of before process exit |
||
| 80 | */ |
||
| 81 | const BEFORE_EXIT = 'beforeExit'; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * event name of after process exit |
||
| 85 | */ |
||
| 86 | const AFTER_FINISHED = 'afterFinished'; |
||
| 87 | |||
| 88 | |||
| 89 | /** |
||
| 90 | * @param string $execution it can be a Runnable object, callback function or null |
||
| 91 | * @param null $name process name,you can manager the process by it's name. |
||
| 92 | */ |
||
| 93 | 52 | public function __construct($execution = null, $name = null) |
|
| 111 | |||
| 112 | /** |
||
| 113 | * get pid |
||
| 114 | * |
||
| 115 | * @return int |
||
| 116 | */ |
||
| 117 | 9 | public function getPid() |
|
| 121 | |||
| 122 | /** |
||
| 123 | * get or set name |
||
| 124 | * |
||
| 125 | * @param string|null $name |
||
| 126 | * @return mixed |
||
| 127 | */ |
||
| 128 | 3 | public function name($name = null) |
|
| 136 | |||
| 137 | /** |
||
| 138 | * if the process is running |
||
| 139 | * |
||
| 140 | * @return bool |
||
| 141 | */ |
||
| 142 | 46 | public function isRunning() |
|
| 147 | |||
| 148 | /** |
||
| 149 | * if the process is stopped |
||
| 150 | * |
||
| 151 | * @return bool |
||
| 152 | */ |
||
| 153 | 3 | public function isStopped() |
|
| 154 | { |
||
| 155 | 3 | if (is_null($this->errno)) { |
|
|
1 ignored issue
–
show
|
|||
| 156 | 3 | return false; |
|
| 157 | } |
||
| 158 | |||
| 159 | 3 | return true; |
|
| 160 | } |
||
| 161 | |||
| 162 | /** |
||
| 163 | * if the process is started |
||
| 164 | * |
||
| 165 | * @return bool |
||
| 166 | */ |
||
| 167 | 3 | public function hasStarted() |
|
| 168 | { |
||
| 169 | 3 | return $this->started; |
|
| 170 | } |
||
| 171 | |||
| 172 | /** |
||
| 173 | * get pcntl errno |
||
| 174 | * |
||
| 175 | * @return int |
||
| 176 | */ |
||
| 177 | 9 | public function errno() |
|
| 181 | |||
| 182 | /** |
||
| 183 | * get pcntl errmsg |
||
| 184 | * |
||
| 185 | * @return string |
||
| 186 | */ |
||
| 187 | 6 | public function errmsg() |
|
| 191 | |||
| 192 | 3 | public function ifSignal() |
|
| 196 | |||
| 197 | /** |
||
| 198 | * start the sub process |
||
| 199 | * and run the callback |
||
| 200 | * |
||
| 201 | * @return string pid |
||
| 202 | */ |
||
| 203 | 49 | public function start() |
|
| 241 | |||
| 242 | /** |
||
| 243 | * kill self |
||
| 244 | * |
||
| 245 | * @param bool|true $block |
||
| 246 | * @param int $signal |
||
| 247 | */ |
||
| 248 | 12 | public function shutdown($block = true, $signal = SIGTERM) |
|
| 263 | |||
| 264 | /** |
||
| 265 | * waiting for the sub process exit |
||
| 266 | * |
||
| 267 | * @param bool|true $block if block the process |
||
| 268 | * @param int $sleep default 0.1s check sub process status |
||
| 269 | * every $sleep milliseconds. |
||
| 270 | */ |
||
| 271 | 25 | public function wait($block = true, $sleep = 100000) |
|
| 283 | |||
| 284 | /** |
||
| 285 | * register callback functions |
||
| 286 | * |
||
| 287 | * @param $event |
||
| 288 | * @param $function |
||
| 289 | */ |
||
| 290 | 3 | public function on($event, $function) |
|
| 298 | |||
| 299 | /** |
||
| 300 | * register sub process signal handler, |
||
| 301 | * when the sub process start, the handlers will be registered |
||
| 302 | * |
||
| 303 | * @param $signal |
||
| 304 | * @param callable $handler |
||
| 305 | */ |
||
| 306 | public function registerSignalHandler($signal, callable $handler) |
||
| 310 | |||
| 311 | /** |
||
| 312 | * you should overwrite this function |
||
| 313 | * if you do not use the Runnable or callback. |
||
| 314 | */ |
||
| 315 | public function run() |
||
| 318 | |||
| 319 | /** |
||
| 320 | * update the process status |
||
| 321 | * |
||
| 322 | * @param bool $block |
||
| 323 | */ |
||
| 324 | 46 | protected function updateStatus($block = false) |
|
| 364 | |||
| 365 | /** |
||
| 366 | * register signal SIGTERM handler, |
||
| 367 | * when the parent process call shutdown and use the default signal, |
||
| 368 | * this handler will be triggered |
||
| 369 | */ |
||
| 370 | protected function signal() |
||
| 383 | |||
| 384 | /** |
||
| 385 | * get sub process callback |
||
| 386 | * |
||
| 387 | * @return array|callable|null |
||
| 388 | */ |
||
| 389 | 49 | protected function getCallable() |
|
| 402 | |||
| 403 | /** |
||
| 404 | * init process status |
||
| 405 | */ |
||
| 406 | 52 | protected function initStatus() |
|
| 415 | } |