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 | 61 | public function __construct($execution = null, $name = null) |
|
| 96 | |||
| 97 | /** |
||
| 98 | * init process status |
||
| 99 | */ |
||
| 100 | 58 | 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() |
|
| 144 | |||
| 145 | /** |
||
| 146 | * if the process is running |
||
| 147 | * |
||
| 148 | * @return bool |
||
| 149 | */ |
||
| 150 | 55 | public function isRunning() |
|
| 155 | |||
| 156 | /** |
||
| 157 | * update the process status |
||
| 158 | * |
||
| 159 | * @param bool $block |
||
| 160 | */ |
||
| 161 | 55 | protected function updateStatus($block = false) |
|
| 201 | |||
| 202 | /** |
||
| 203 | * if the process is started |
||
| 204 | * |
||
| 205 | * @return bool |
||
| 206 | */ |
||
| 207 | 15 | public function isStarted() |
|
| 211 | |||
| 212 | /** |
||
| 213 | * get pcntl errno |
||
| 214 | * |
||
| 215 | * @return int |
||
| 216 | */ |
||
| 217 | 9 | public function errno() |
|
| 221 | |||
| 222 | /** |
||
| 223 | * get pcntl errmsg |
||
| 224 | * |
||
| 225 | * @return string |
||
| 226 | */ |
||
| 227 | 6 | public function errmsg() |
|
| 231 | |||
| 232 | 3 | public function ifSignal() |
|
| 236 | |||
| 237 | /** |
||
| 238 | * start the sub process |
||
| 239 | * and run the callback |
||
| 240 | * |
||
| 241 | * @return string pid |
||
| 242 | */ |
||
| 243 | 55 | public function start() |
|
| 268 | |||
| 269 | /** |
||
| 270 | * get sub process callback |
||
| 271 | * |
||
| 272 | * @return array|callable|null |
||
| 273 | */ |
||
| 274 | 55 | 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 | 34 | 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.