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 ShellCommand 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 ShellCommand, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class ShellCommand extends IOStream |
||
| 14 | { |
||
| 15 | |||
| 16 | /** |
||
| 17 | * @var string Executable path |
||
| 18 | */ |
||
| 19 | public $binPath; |
||
| 20 | /** |
||
| 21 | * @var string SUID |
||
| 22 | */ |
||
| 23 | public $setUser; |
||
| 24 | /** |
||
| 25 | * @var string SGID |
||
| 26 | */ |
||
| 27 | public $setGroup; |
||
| 28 | /** |
||
| 29 | * @var string Chroot |
||
| 30 | */ |
||
| 31 | public $chroot = '/'; |
||
| 32 | /** |
||
| 33 | * @var string Chdir |
||
| 34 | */ |
||
| 35 | public $cwd; |
||
| 36 | protected $finishWrite; |
||
| 37 | /** |
||
| 38 | * @var string Command string |
||
| 39 | */ |
||
| 40 | protected $cmd; |
||
| 41 | /** |
||
| 42 | * @var array Opened pipes |
||
| 43 | */ |
||
| 44 | protected $pipes; |
||
| 45 | /** |
||
| 46 | * @var resource Process descriptor |
||
| 47 | */ |
||
| 48 | protected $pd; |
||
| 49 | /** |
||
| 50 | * @var resource FD write |
||
| 51 | */ |
||
| 52 | protected $fdWrite; |
||
| 53 | /** |
||
| 54 | * @var boolean Output errors? |
||
| 55 | */ |
||
| 56 | protected $outputErrors = true; |
||
| 57 | /** |
||
| 58 | * @var array Hash of environment's variables |
||
| 59 | */ |
||
| 60 | protected $env = []; |
||
| 61 | /** |
||
| 62 | * @var string Path to error logfile |
||
| 63 | */ |
||
| 64 | protected $errlogfile = null; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var array Array of arguments |
||
| 68 | */ |
||
| 69 | protected $args; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var integer Process priority |
||
| 73 | */ |
||
| 74 | protected $nice; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var \EventBufferEvent |
||
| 78 | */ |
||
| 79 | protected $bevWrite; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @var \EventBufferEvent |
||
| 83 | */ |
||
| 84 | protected $bevErr; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @var boolean Got EOF? |
||
| 88 | */ |
||
| 89 | protected $EOF = false; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @param string $path |
||
| 93 | * |
||
| 94 | * @return $this |
||
| 95 | */ |
||
| 96 | public function setErrorLog (string $path): self |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Execute |
||
| 105 | * |
||
| 106 | * @param string $binPath Binpath |
||
|
|
|||
| 107 | * @param callable $cb Callback |
||
| 108 | * @param array $args Optional. Arguments |
||
| 109 | * @param array $env Optional. Hash of environment's variables |
||
| 110 | */ |
||
| 111 | public static function exec ($binPath = null, $cb = null, $args = null, $env = null) |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @param bool $bool |
||
| 133 | */ |
||
| 134 | public function setOutputErrors (bool $bool): void |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Execute |
||
| 141 | * |
||
| 142 | * @param string $binPath Optional. Binpath |
||
| 143 | * @param array $args Optional. Arguments |
||
| 144 | * @param array $env Optional. Hash of environment's variables |
||
| 145 | * |
||
| 146 | * @return this |
||
| 147 | */ |
||
| 148 | public function execute ($binPath = null, $args = null, $env = null) |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Build arguments string from associative/enumerated array (may be mixed) |
||
| 201 | * |
||
| 202 | * @param array $args |
||
| 203 | * |
||
| 204 | * @return string |
||
| 205 | */ |
||
| 206 | public static function buildArgs ($args) |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Sets fd |
||
| 234 | * |
||
| 235 | * @param resource $fd File descriptor |
||
| 236 | * @param \EventBufferEvent $bev |
||
| 237 | * |
||
| 238 | * @return void |
||
| 239 | */ |
||
| 240 | public function setFd ($fd, $bev = null) |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Get command string |
||
| 302 | * |
||
| 303 | * @return string |
||
| 304 | */ |
||
| 305 | public function getCmd () |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Set group |
||
| 312 | * |
||
| 313 | * @return this |
||
| 314 | */ |
||
| 315 | public function setGroup ($val) |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Set cwd |
||
| 324 | * |
||
| 325 | * @param string $dir |
||
| 326 | * |
||
| 327 | * @return this |
||
| 328 | */ |
||
| 329 | public function setCwd ($dir) |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Set group |
||
| 338 | * |
||
| 339 | * @param string $val |
||
| 340 | * |
||
| 341 | * @return this |
||
| 342 | */ |
||
| 343 | public function setUser ($val) |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Set chroot |
||
| 352 | * |
||
| 353 | * @param string $dir |
||
| 354 | * |
||
| 355 | * @return this |
||
| 356 | */ |
||
| 357 | public function setChroot ($dir) |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Sets an array of arguments |
||
| 366 | * |
||
| 367 | * @param array Arguments |
||
| 368 | * |
||
| 369 | * @return this |
||
| 370 | */ |
||
| 371 | public function setArgs ($args = null) |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Set a hash of environment's variables |
||
| 380 | * |
||
| 381 | * @param array Hash of environment's variables |
||
| 382 | * |
||
| 383 | * @return this |
||
| 384 | */ |
||
| 385 | public function setEnv ($env = null) |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Set priority |
||
| 394 | * |
||
| 395 | * @param integer $nice Priority |
||
| 396 | * |
||
| 397 | * @return this |
||
| 398 | */ |
||
| 399 | public function nice ($nice = null) |
||
| 405 | |||
| 406 | /** |
||
| 407 | * Finish write stream |
||
| 408 | * |
||
| 409 | * @return boolean |
||
| 410 | */ |
||
| 411 | public function finishWrite () |
||
| 421 | |||
| 422 | /** |
||
| 423 | * Close write stream |
||
| 424 | * |
||
| 425 | * @return this |
||
| 426 | */ |
||
| 427 | public function closeWrite () |
||
| 443 | |||
| 444 | /** |
||
| 445 | * Called when stream is finished |
||
| 446 | */ |
||
| 447 | public function onFinish () |
||
| 451 | |||
| 452 | /** |
||
| 453 | * Called when got EOF |
||
| 454 | * |
||
| 455 | * @return void |
||
| 456 | */ |
||
| 457 | public function onEofEvent () |
||
| 466 | |||
| 467 | /** |
||
| 468 | * Got EOF? |
||
| 469 | * |
||
| 470 | * @return boolean |
||
| 471 | */ |
||
| 472 | public function eof () |
||
| 476 | |||
| 477 | /** |
||
| 478 | * Send data to the connection. Note that it just writes to buffer that flushes at every baseloop |
||
| 479 | * |
||
| 480 | * @param string $data Data to send |
||
| 481 | * |
||
| 482 | * @return boolean Success |
||
| 483 | */ |
||
| 484 | View Code Duplication | public function write ($data) |
|
| 507 | |||
| 508 | /** |
||
| 509 | * Close the process |
||
| 510 | * |
||
| 511 | * @return void |
||
| 512 | */ |
||
| 513 | public function close () |
||
| 521 | |||
| 522 | /** |
||
| 523 | * Send data and appending \n to connection. Note that it just writes to buffer flushed at every baseloop |
||
| 524 | * |
||
| 525 | * @param string Data to send |
||
| 526 | * |
||
| 527 | * @return boolean Success |
||
| 528 | */ |
||
| 529 | View Code Duplication | public function writeln ($data) |
|
| 548 | |||
| 549 | /** |
||
| 550 | * Sets callback which will be called once when got EOF |
||
| 551 | * |
||
| 552 | * @param callable $cb |
||
| 553 | * |
||
| 554 | * @return this |
||
| 555 | */ |
||
| 556 | public function onEOF ($cb = null) |
||
| 560 | |||
| 561 | public function getStatus () |
||
| 565 | |||
| 566 | /** |
||
| 567 | * Called when new data received |
||
| 568 | * |
||
| 569 | * @return this|null |
||
| 570 | */ |
||
| 571 | protected function onRead () |
||
| 580 | } |
||
| 581 |
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.