Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like IPC 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 IPC, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class IPC extends Generic |
||
| 19 | { |
||
| 20 | /** |
||
| 21 | * Event base |
||
| 22 | * @var EventLoop |
||
| 23 | */ |
||
| 24 | public $loop; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Break main loop? |
||
| 28 | * @var boolean |
||
| 29 | */ |
||
| 30 | protected $breakMainLoop = false; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Reload ready? |
||
| 34 | * @var boolean |
||
| 35 | */ |
||
| 36 | protected $reloadReady = false; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Update? |
||
| 40 | * @var boolean |
||
| 41 | */ |
||
| 42 | public $update = false; |
||
| 43 | |||
| 44 | |||
| 45 | /** |
||
| 46 | * If true, we do not register signals automatically at start |
||
| 47 | * @var boolean |
||
| 48 | */ |
||
| 49 | protected $delayedSigReg = true; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Instances count |
||
| 53 | * @var array |
||
| 54 | */ |
||
| 55 | public $instancesCount = []; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * File watcher |
||
| 59 | * @var FileWatcher |
||
| 60 | */ |
||
| 61 | public $fileWatcher; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * If true, we do not register signals automatically at start |
||
| 65 | * @var boolean |
||
| 66 | */ |
||
| 67 | public $reload = false; |
||
| 68 | |||
| 69 | /** @var */ |
||
| 70 | public $IPCManager; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Runtime of Worker process. |
||
| 74 | * @return void |
||
| 75 | */ |
||
| 76 | protected function run() |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Setup settings on start. |
||
| 111 | * @return void |
||
| 112 | */ |
||
| 113 | protected function prepareSystemEnv() |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Log something |
||
| 174 | * @param string - Message. |
||
| 175 | * @param string $message |
||
| 176 | * @return void |
||
| 177 | */ |
||
| 178 | public function log($message) |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Reloads additional config-files on-the-fly. |
||
| 185 | * @return void |
||
| 186 | */ |
||
| 187 | protected function update() |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Shutdown thread |
||
| 199 | * @param boolean - Hard? If hard, we shouldn't wait for graceful shutdown of the running applications. |
||
| 200 | * @return boolean|null - Ready? |
||
| 201 | */ |
||
| 202 | public function shutdown($hard = false) |
||
| 203 | { |
||
| 204 | $error = error_get_last(); |
||
| 205 | View Code Duplication | if ($error) { |
|
| 206 | if ($error['type'] === E_ERROR) { |
||
| 207 | $this->log('crashed by error \'' . $error['message'] . '\' at ' . $error['file'] . ':' . $error['line']); |
||
| 208 | } |
||
| 209 | } |
||
| 210 | |||
| 211 | if (Daemon::$config->throwexceptiononshutdown->value) { |
||
| 212 | throw new \Exception('event shutdown'); |
||
| 213 | } |
||
| 214 | |||
| 215 | @ob_flush(); |
||
| 216 | |||
| 217 | View Code Duplication | if ($this->terminated === true) { |
|
| 218 | if ($hard) { |
||
| 219 | posix_kill(getmypid(), SIGKILL); |
||
| 220 | exit(0); |
||
| 221 | } |
||
| 222 | |||
| 223 | return; |
||
| 224 | } |
||
| 225 | |||
| 226 | $this->terminated = true; |
||
| 227 | if ($hard) { |
||
| 228 | posix_kill(getmypid(), SIGKILL); |
||
| 229 | exit(0); |
||
| 230 | } |
||
| 231 | FileSystem::waitAllEvents(); // ensure that all I/O events completed before suicide |
||
| 232 | posix_kill(posix_getppid(), SIGCHLD); // praying to Master |
||
| 233 | posix_kill(getmypid(), SIGKILL); |
||
| 234 | exit(0); // R.I.P. |
||
| 235 | } |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Handler of the SIGINT (hard shutdown) signal in worker process. |
||
| 239 | * @return void |
||
| 240 | */ |
||
| 241 | protected function sigint() |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Handler of the SIGTERM (graceful shutdown) signal in worker process. |
||
| 252 | * @return void |
||
| 253 | */ |
||
| 254 | protected function sigterm() |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Handler of the SIGQUIT (graceful shutdown) signal in worker process. |
||
| 265 | * @return void |
||
| 266 | */ |
||
| 267 | public function sigquit() |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Handler of the SIGTSTP (graceful stop) signal in worker process. |
||
| 278 | * @return void |
||
| 279 | */ |
||
| 280 | protected function sigtstp() |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Handler of the SIGHUP (reload config) signal in worker process. |
||
| 291 | * @return void |
||
| 292 | */ |
||
| 293 | View Code Duplication | public function sighup() |
|
| 305 | |||
| 306 | /** |
||
| 307 | * Handler of the SIGUSR1 (re-open log-file) signal in worker process. |
||
| 308 | * @return void |
||
| 309 | */ |
||
| 310 | public function sigusr1() |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Handler of the SIGUSR2 (graceful shutdown for update) signal in worker process. |
||
| 321 | * @return void |
||
| 322 | */ |
||
| 323 | public function sigusr2() |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Handler of the SIGTTIN signal in worker process. |
||
| 332 | * @return void |
||
| 333 | */ |
||
| 334 | public function sigttin() |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Handler of the SIGXSFZ signal in worker process. |
||
| 340 | * @return void |
||
| 341 | */ |
||
| 342 | public function sigxfsz() |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Handler of non-known signals. |
||
| 349 | * @return void |
||
| 350 | */ |
||
| 351 | View Code Duplication | public function sigunknown($signo) |
|
| 361 | } |
||
| 362 |
This check looks for access to methods that are not accessible from the current context.
If you need to make a method accessible to another context you can raise its visibility level in the defining class.