Complex classes like Threads 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 Threads, and based on these observations, apply Extract Interface, too.
| 1 | <?php /** MicroThreads */ |
||
| 19 | abstract class Threads |
||
| 20 | { |
||
| 21 | /** @var string $name thread name */ |
||
| 22 | private $name; |
||
| 23 | /** @var integer $pid process ID */ |
||
| 24 | private $pid; |
||
| 25 | /** @var integer $puid process UID */ |
||
| 26 | private $puid; |
||
| 27 | /** @var integer $guid process GUID */ |
||
| 28 | private $guid; |
||
| 29 | /** @var bool $isChild is child */ |
||
| 30 | private $isChild = false; |
||
| 31 | /** @var array $internalIPCArray Internal IPC array */ |
||
| 32 | private $internalIPCArray = []; |
||
| 33 | /** @var integer $internalIPCKey Internal IPC key */ |
||
| 34 | private $internalIPCKey; |
||
| 35 | /** @var integer $internalSemaphoreKey Internal semaphore key */ |
||
| 36 | private $internalSemaphoreKey; |
||
| 37 | /** @var bool $isIPC is IPC */ |
||
| 38 | private $isIPC = false; |
||
| 39 | /** @var bool $running is running */ |
||
| 40 | private $running = false; |
||
| 41 | /** @var string $fileIPC1 file IPC1 */ |
||
| 42 | private $fileIPC1; |
||
| 43 | /** @var string $fileIPC2 file IPC2 */ |
||
| 44 | private $fileIPC2; |
||
| 45 | |||
| 46 | |||
| 47 | /** |
||
| 48 | * Constructor thread |
||
| 49 | * |
||
| 50 | * @access public |
||
| 51 | * |
||
| 52 | * @param string $name |
||
| 53 | * @param int $puid |
||
| 54 | * @param int $guid |
||
| 55 | * @param int $umask |
||
| 56 | * |
||
| 57 | * @result void |
||
| 58 | * @throws Exception |
||
| 59 | */ |
||
| 60 | public function __construct($name, $puid = 0, $guid = 0, $umask = -1) |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Create IPC segment |
||
| 85 | * |
||
| 86 | * @access protected |
||
| 87 | * |
||
| 88 | * @return bool |
||
| 89 | * @throws Exception |
||
| 90 | */ |
||
| 91 | protected function createIPCSegment() |
||
| 109 | |||
| 110 | /** |
||
| 111 | * get thread name |
||
| 112 | * |
||
| 113 | * @access public |
||
| 114 | * @return string |
||
| 115 | */ |
||
| 116 | public function getName() |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Set thread name |
||
| 123 | * |
||
| 124 | * @access public |
||
| 125 | * |
||
| 126 | * @param string $name |
||
| 127 | * |
||
| 128 | * @return void |
||
| 129 | */ |
||
| 130 | public function setName($name) |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Create IPC semaphore |
||
| 137 | * |
||
| 138 | * @access protected |
||
| 139 | * |
||
| 140 | * @return bool |
||
| 141 | * @throws Exception |
||
| 142 | */ |
||
| 143 | protected function createIPCSemaphore() |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Is running thread |
||
| 164 | * |
||
| 165 | * @access public |
||
| 166 | * @return bool |
||
| 167 | */ |
||
| 168 | public function isRunning() |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Set alive |
||
| 175 | * |
||
| 176 | * @access public |
||
| 177 | * |
||
| 178 | * @return void |
||
| 179 | * @throws \Micro\Base\Exception |
||
| 180 | */ |
||
| 181 | public function setAlive() |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Set variable in shared memory |
||
| 188 | * |
||
| 189 | * @access public |
||
| 190 | * |
||
| 191 | * @param string $name |
||
| 192 | * @param integer $value |
||
| 193 | * |
||
| 194 | * @return void |
||
| 195 | * @throws \Micro\Base\Exception |
||
| 196 | */ |
||
| 197 | public function setVariable($name, $value) |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Write to IPC segment |
||
| 205 | * |
||
| 206 | * @access protected |
||
| 207 | * @return void |
||
| 208 | * @throws Exception |
||
| 209 | */ |
||
| 210 | protected function writeToIPCSegment() |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Get last alive |
||
| 229 | * |
||
| 230 | * @access public |
||
| 231 | * @return int |
||
| 232 | * @throws \Micro\Base\Exception |
||
| 233 | */ |
||
| 234 | public function getLastAlive() |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Get variable from shared memory |
||
| 246 | * |
||
| 247 | * @access public |
||
| 248 | * |
||
| 249 | * @param string $name |
||
| 250 | * |
||
| 251 | * @return mixed |
||
| 252 | * @throws \Micro\Base\Exception |
||
| 253 | */ |
||
| 254 | public function getVariable($name) |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Read from IPC segment |
||
| 263 | * |
||
| 264 | * @access public |
||
| 265 | * @return void |
||
| 266 | * @throws Exception |
||
| 267 | */ |
||
| 268 | protected function readFromIPCSegment() |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Get thread process ID |
||
| 283 | * |
||
| 284 | * @access public |
||
| 285 | * @return int |
||
| 286 | */ |
||
| 287 | public function getPid() |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Register callback func into shared memory |
||
| 294 | * |
||
| 295 | * @access public |
||
| 296 | * |
||
| 297 | * @param mixed $argList |
||
| 298 | * @param string $methodName |
||
| 299 | * |
||
| 300 | * @return mixed|void |
||
| 301 | * @throws \Micro\Base\Exception |
||
| 302 | */ |
||
| 303 | public function register_callback_func($argList, $methodName) |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Send signal USR1 |
||
| 342 | * |
||
| 343 | * @access protected |
||
| 344 | * @return void |
||
| 345 | */ |
||
| 346 | protected function sendSigUsr1() |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Wait IPC semaphore |
||
| 355 | * |
||
| 356 | * @access protected |
||
| 357 | * @return void |
||
| 358 | */ |
||
| 359 | protected function waitIPCSemaphore() |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Start |
||
| 374 | * |
||
| 375 | * @access public |
||
| 376 | * |
||
| 377 | * @return void |
||
| 378 | * @throws Exception |
||
| 379 | */ |
||
| 380 | public function start() |
||
| 410 | |||
| 411 | /** |
||
| 412 | * Running thread |
||
| 413 | * |
||
| 414 | * @access public |
||
| 415 | * @return void |
||
| 416 | */ |
||
| 417 | abstract public function run(); |
||
| 418 | |||
| 419 | /** |
||
| 420 | * Stop thread |
||
| 421 | * |
||
| 422 | * @access public |
||
| 423 | * @return bool |
||
| 424 | */ |
||
| 425 | public function stop() |
||
| 440 | |||
| 441 | /** |
||
| 442 | * Clean thread context |
||
| 443 | * |
||
| 444 | * @access protected |
||
| 445 | * @return void |
||
| 446 | */ |
||
| 447 | protected function cleanThreadContext() |
||
| 461 | |||
| 462 | /** |
||
| 463 | * Signal handler |
||
| 464 | * |
||
| 465 | * @access protected |
||
| 466 | * |
||
| 467 | * @param int $sigNo |
||
| 468 | * |
||
| 469 | * @return void |
||
| 470 | * @throws \Micro\Base\Exception |
||
| 471 | */ |
||
| 472 | protected function sigHandler($sigNo) |
||
| 508 | } |
||
| 509 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: