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 |
||
| 14 | class ShellCommand extends IOStream |
||
| 15 | { |
||
| 16 | |||
| 17 | protected $finishWrite; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @var string Command string |
||
| 21 | */ |
||
| 22 | protected $cmd; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var string Executable path |
||
| 26 | */ |
||
| 27 | public $binPath; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var array Opened pipes |
||
| 31 | */ |
||
| 32 | protected $pipes; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var resource Process descriptor |
||
| 36 | */ |
||
| 37 | protected $pd; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var resource FD write |
||
| 41 | */ |
||
| 42 | protected $fdWrite; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var boolean Output errors? |
||
| 46 | */ |
||
| 47 | protected $outputErrors = true; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var string SUID |
||
| 51 | */ |
||
| 52 | public $setUser; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var string SGID |
||
| 56 | */ |
||
| 57 | public $setGroup; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var string Chroot |
||
| 61 | */ |
||
| 62 | public $chroot = '/'; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var array Hash of environment's variables |
||
| 66 | */ |
||
| 67 | protected $env = []; // |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var string Chdir |
||
| 71 | */ |
||
| 72 | public $cwd; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var string Path to error logfile |
||
| 76 | */ |
||
| 77 | protected $errlogfile = null; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var array Array of arguments |
||
| 81 | */ |
||
| 82 | protected $args; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @var integer Process priority |
||
| 86 | */ |
||
| 87 | protected $nice; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @var \EventBufferEvent |
||
| 91 | */ |
||
| 92 | protected $bevWrite; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @var \EventBufferEvent |
||
| 96 | */ |
||
| 97 | protected $bevErr; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @var boolean Got EOF? |
||
| 101 | */ |
||
| 102 | protected $EOF = false; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Get command string |
||
| 106 | * @return string |
||
| 107 | */ |
||
| 108 | public function getCmd() |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Set group |
||
| 115 | * @return this |
||
|
|
|||
| 116 | */ |
||
| 117 | public function setGroup($val) |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Set cwd |
||
| 125 | * @param string $dir |
||
| 126 | * @return this |
||
| 127 | */ |
||
| 128 | public function setCwd($dir) |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Set group |
||
| 136 | * @param string $val |
||
| 137 | * @return this |
||
| 138 | */ |
||
| 139 | public function setUser($val) |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Set chroot |
||
| 147 | * @param string $dir |
||
| 148 | * @return this |
||
| 149 | */ |
||
| 150 | public function setChroot($dir) |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Execute |
||
| 158 | * @param string $binPath Binpath |
||
| 159 | * @param callable $cb Callback |
||
| 160 | * @param array $args Optional. Arguments |
||
| 161 | * @param array $env Optional. Hash of environment's variables |
||
| 162 | */ |
||
| 163 | public static function exec($binPath = null, $cb = null, $args = null, $env = null) |
||
| 176 | |||
| 177 | |||
| 178 | /** |
||
| 179 | * Sets fd |
||
| 180 | * @param resource $fd File descriptor |
||
| 181 | * @param \EventBufferEvent $bev |
||
| 182 | * @return void |
||
| 183 | */ |
||
| 184 | public function setFd($fd, $bev = null) |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Sets an array of arguments |
||
| 225 | * @param array Arguments |
||
| 226 | * @return this |
||
| 227 | */ |
||
| 228 | public function setArgs($args = null) |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Set a hash of environment's variables |
||
| 237 | * @param array Hash of environment's variables |
||
| 238 | * @return this |
||
| 239 | */ |
||
| 240 | public function setEnv($env = null) |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Called when got EOF |
||
| 249 | * @return void |
||
| 250 | */ |
||
| 251 | public function onEofEvent() |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Set priority |
||
| 263 | * @param integer $nice Priority |
||
| 264 | * @return this |
||
| 265 | */ |
||
| 266 | public function nice($nice = null) |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Called when new data received |
||
| 275 | * @return this|null |
||
| 276 | */ |
||
| 277 | protected function onRead() |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Build arguments string from associative/enumerated array (may be mixed) |
||
| 288 | * @param array $args |
||
| 289 | * @return string |
||
| 290 | */ |
||
| 291 | public static function buildArgs($args) |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Execute |
||
| 316 | * @param string $binPath Optional. Binpath |
||
| 317 | * @param array $args Optional. Arguments |
||
| 318 | * @param array $env Optional. Hash of environment's variables |
||
| 319 | * @return this |
||
| 320 | */ |
||
| 321 | public function execute($binPath = null, $args = null, $env = null) |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Finish write stream |
||
| 381 | * @return boolean |
||
| 382 | */ |
||
| 383 | public function finishWrite() |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Close the process |
||
| 396 | * @return void |
||
| 397 | */ |
||
| 398 | public function close() |
||
| 406 | |||
| 407 | /** |
||
| 408 | * Called when stream is finished |
||
| 409 | */ |
||
| 410 | public function onFinish() |
||
| 414 | |||
| 415 | /** |
||
| 416 | * Close write stream |
||
| 417 | * @return this |
||
| 418 | */ |
||
| 419 | public function closeWrite() |
||
| 435 | |||
| 436 | /** |
||
| 437 | * Got EOF? |
||
| 438 | * @return boolean |
||
| 439 | */ |
||
| 440 | public function eof() |
||
| 444 | |||
| 445 | /** |
||
| 446 | * Send data to the connection. Note that it just writes to buffer that flushes at every baseloop |
||
| 447 | * @param string $data Data to send |
||
| 448 | * @return boolean Success |
||
| 449 | */ |
||
| 450 | View Code Duplication | public function write($data) |
|
| 470 | |||
| 471 | /** |
||
| 472 | * Send data and appending \n to connection. Note that it just writes to buffer flushed at every baseloop |
||
| 473 | * @param string Data to send |
||
| 474 | * @return boolean Success |
||
| 475 | */ |
||
| 476 | View Code Duplication | public function writeln($data) |
|
| 493 | |||
| 494 | /** |
||
| 495 | * Sets callback which will be called once when got EOF |
||
| 496 | * @param callable $cb |
||
| 497 | * @return this |
||
| 498 | */ |
||
| 499 | public function onEOF($cb = null) |
||
| 504 | |||
| 505 | public function getStatus() |
||
| 509 | } |
||
| 510 |
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.