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 | /** |
||
| 75 | * @param string $execution it can be a Runnable object, callback function or null |
||
| 76 | * @param null $name process name,you can manager the process by it's name. |
||
| 77 | */ |
||
| 78 | 60 | public function __construct($execution = null, $name = null) |
|
| 96 | |||
| 97 | /** |
||
| 98 | * init process status |
||
| 99 | */ |
||
| 100 | 57 | protected function initStatus() |
|
| 109 | |||
| 110 | /** |
||
| 111 | * get pid |
||
| 112 | * |
||
| 113 | * @return int |
||
| 114 | */ |
||
| 115 | 9 | public function getPid() |
|
| 119 | |||
| 120 | /** |
||
| 121 | * get or set name |
||
| 122 | * |
||
| 123 | * @param string|null $name |
||
| 124 | * @return mixed |
||
| 125 | */ |
||
| 126 | 3 | public function name($name = null) |
|
| 134 | |||
| 135 | /** |
||
| 136 | * if the process is stopped |
||
| 137 | * |
||
| 138 | * @return bool |
||
| 139 | */ |
||
| 140 | 3 | public function isStopped() |
|
| 141 | { |
||
| 142 | 3 | return !$this->isRunning(); |
|
| 143 | } |
||
| 144 | |||
| 145 | /** |
||
| 146 | * if the process is running |
||
| 147 | * |
||
| 148 | * @return bool |
||
| 149 | */ |
||
| 150 | 54 | public function isRunning() |
|
| 151 | { |
||
| 152 | 54 | $this->updateStatus(); |
|
| 153 | 54 | return $this->running; |
|
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * update the process status |
||
| 158 | * |
||
| 159 | * @param bool $block |
||
| 160 | */ |
||
| 161 | 54 | protected function updateStatus($block = false) |
|
| 162 | { |
||
| 163 | 54 | if ($this->running !== true) { |
|
| 164 | 24 | return; |
|
| 165 | } |
||
| 166 | |||
| 167 | 54 | if ($block) { |
|
| 168 | 12 | $res = pcntl_waitpid($this->pid, $status); |
|
|
|
|||
| 169 | 12 | } else { |
|
| 170 | 54 | $res = pcntl_waitpid($this->pid, $status, WNOHANG | WUNTRACED); |
|
| 171 | } |
||
| 172 | |||
| 173 | 54 | if ($res === -1) { |
|
| 174 | $message = "pcntl_waitpid failed. the process maybe available"; |
||
| 175 | throw new \RuntimeException($message); |
||
| 176 | 54 | } elseif ($res === 0) { |
|
| 177 | 54 | $this->running = true; |
|
| 178 | 54 | } else { |
|
| 179 | 51 | if (pcntl_wifsignaled($status)) { |
|
| 180 | 6 | $this->term_signal = pcntl_wtermsig($status); |
|
| 181 | 6 | } |
|
| 182 | 51 | if (pcntl_wifstopped($status)) { |
|
| 183 | $this->stop_signal = pcntl_wstopsig($status); |
||
| 184 | } |
||
| 185 | 51 | if (pcntl_wifexited($status)) { |
|
| 186 | 45 | $this->errno = pcntl_wexitstatus($status); |
|
| 187 | 45 | $this->errmsg = pcntl_strerror($this->errno); |
|
| 188 | 45 | } else { |
|
| 189 | 6 | $this->errno = pcntl_get_last_error(); |
|
| 190 | 6 | $this->errmsg = pcntl_strerror($this->errno); |
|
| 191 | } |
||
| 192 | 51 | if (pcntl_wifsignaled($status)) { |
|
| 193 | 6 | $this->if_signal = true; |
|
| 194 | 6 | } else { |
|
| 195 | 45 | $this->if_signal = false; |
|
| 196 | } |
||
| 197 | |||
| 198 | 51 | $this->running = false; |
|
| 199 | } |
||
| 200 | 54 | } |
|
| 201 | |||
| 202 | /** |
||
| 203 | * if the process is started |
||
| 204 | * |
||
| 205 | * @return bool |
||
| 206 | */ |
||
| 207 | 15 | public function isStarted() |
|
| 208 | { |
||
| 209 | 15 | return $this->started; |
|
| 210 | } |
||
| 211 | |||
| 212 | /** |
||
| 213 | * get pcntl errno |
||
| 214 | * |
||
| 215 | * @return int |
||
| 216 | */ |
||
| 217 | 9 | public function errno() |
|
| 218 | { |
||
| 219 | 9 | return $this->errno; |
|
| 220 | } |
||
| 221 | |||
| 222 | /** |
||
| 223 | * get pcntl errmsg |
||
| 224 | * |
||
| 225 | * @return string |
||
| 226 | */ |
||
| 227 | 6 | public function errmsg() |
|
| 228 | { |
||
| 229 | 6 | return $this->errmsg; |
|
| 230 | } |
||
| 231 | |||
| 232 | 3 | public function ifSignal() |
|
| 233 | { |
||
| 234 | 3 | return $this->if_signal; |
|
| 235 | } |
||
| 236 | |||
| 237 | /** |
||
| 238 | * start the sub process |
||
| 239 | * and run the callback |
||
| 240 | * |
||
| 241 | * @return string pid |
||
| 242 | */ |
||
| 243 | 54 | public function start() |
|
| 244 | { |
||
| 245 | 54 | if (!empty($this->pid) && $this->isRunning()) { |
|
| 246 | throw new \LogicException("the process is already running"); |
||
| 247 | } |
||
| 248 | |||
| 249 | 54 | $callback = $this->getCallable(); |
|
| 250 | |||
| 251 | 54 | $pid = pcntl_fork(); |
|
| 252 | 54 | if ($pid < 0) { |
|
| 253 | throw new \RuntimeException("fork error"); |
||
| 254 | 54 | } elseif ($pid > 0) { |
|
| 255 | 54 | $this->pid = $pid; |
|
| 256 | 54 | $this->running = true; |
|
| 257 | 54 | $this->started = true; |
|
| 258 | 54 | } else { |
|
| 259 | $this->pid = getmypid(); |
||
| 260 | $this->signal(); |
||
| 261 | foreach ($this->signal_handlers as $signal => $handler) { |
||
| 262 | pcntl_signal($signal, $handler); |
||
| 263 | } |
||
| 264 | call_user_func($callback); |
||
| 265 | exit(0); |
||
| 266 | } |
||
| 267 | 54 | } |
|
| 268 | |||
| 269 | /** |
||
| 270 | * get sub process callback |
||
| 271 | * |
||
| 272 | * @return array|callable|null |
||
| 273 | */ |
||
| 274 | 54 | protected function getCallable() |
|
| 287 | |||
| 288 | /** |
||
| 289 | * register signal SIGTERM handler, |
||
| 290 | * when the parent process call shutdown and use the default signal, |
||
| 291 | * this handler will be triggered |
||
| 292 | */ |
||
| 293 | protected function signal() |
||
| 299 | |||
| 300 | /** |
||
| 301 | * kill self |
||
| 302 | * |
||
| 303 | * @param bool|true $block |
||
| 304 | * @param int $signal |
||
| 305 | */ |
||
| 306 | 12 | public function shutdown($block = true, $signal = SIGTERM) |
|
| 321 | |||
| 322 | /** |
||
| 323 | * waiting for the sub process exit |
||
| 324 | * |
||
| 325 | * @param bool|true $block if block the process |
||
| 326 | * @param int $sleep default 0.1s check sub process status |
||
| 327 | * every $sleep milliseconds. |
||
| 328 | */ |
||
| 329 | 33 | public function wait($block = true, $sleep = 100000) |
|
| 341 | |||
| 342 | /** |
||
| 343 | * register sub process signal handler, |
||
| 344 | * when the sub process start, the handlers will be registered |
||
| 345 | * |
||
| 346 | * @param $signal |
||
| 347 | * @param callable $handler |
||
| 348 | */ |
||
| 349 | public function registerSignalHandler($signal, callable $handler) |
||
| 353 | |||
| 354 | /** |
||
| 355 | * you should overwrite this function |
||
| 356 | * if you do not use the Runnable or callback. |
||
| 357 | */ |
||
| 358 | public function run() |
||
| 361 | } |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.