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) |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Sets an array of arguments |
||
| 240 | * @param array Arguments |
||
| 241 | * @return this |
||
| 242 | */ |
||
| 243 | public function setArgs($args = null) |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Set a hash of environment's variables |
||
| 252 | * @param array Hash of environment's variables |
||
| 253 | * @return this |
||
| 254 | */ |
||
| 255 | public function setEnv($env = null) |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Called when got EOF |
||
| 264 | * @return void |
||
| 265 | */ |
||
| 266 | public function onEofEvent() |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Set priority |
||
| 278 | * @param integer $nice Priority |
||
| 279 | * @return this |
||
| 280 | */ |
||
| 281 | public function nice($nice = null) |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Called when new data received |
||
| 290 | * @return this|null |
||
| 291 | */ |
||
| 292 | protected function onRead() |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Build arguments string from associative/enumerated array (may be mixed) |
||
| 303 | * @param array $args |
||
| 304 | * @return string |
||
| 305 | */ |
||
| 306 | public static function buildArgs($args) |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Execute |
||
| 331 | * @param string $binPath Optional. Binpath |
||
| 332 | * @param array $args Optional. Arguments |
||
| 333 | * @param array $env Optional. Hash of environment's variables |
||
| 334 | * @return this |
||
| 335 | */ |
||
| 336 | public function execute($binPath = null, $args = null, $env = null) |
||
| 383 | |||
| 384 | /** |
||
| 385 | * Finish write stream |
||
| 386 | * @return boolean |
||
| 387 | */ |
||
| 388 | public function finishWrite() |
||
| 398 | |||
| 399 | /** |
||
| 400 | * Close the process |
||
| 401 | * @return void |
||
| 402 | */ |
||
| 403 | public function close() |
||
| 411 | |||
| 412 | /** |
||
| 413 | * Called when stream is finished |
||
| 414 | */ |
||
| 415 | public function onFinish() |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Close write stream |
||
| 422 | * @return this |
||
| 423 | */ |
||
| 424 | public function closeWrite() |
||
| 440 | |||
| 441 | /** |
||
| 442 | * Got EOF? |
||
| 443 | * @return boolean |
||
| 444 | */ |
||
| 445 | public function eof() |
||
| 449 | |||
| 450 | /** |
||
| 451 | * Send data to the connection. Note that it just writes to buffer that flushes at every baseloop |
||
| 452 | * @param string $data Data to send |
||
| 453 | * @return boolean Success |
||
| 454 | */ |
||
| 455 | View Code Duplication | public function write($data) |
|
| 475 | |||
| 476 | /** |
||
| 477 | * Send data and appending \n to connection. Note that it just writes to buffer flushed at every baseloop |
||
| 478 | * @param string Data to send |
||
| 479 | * @return boolean Success |
||
| 480 | */ |
||
| 481 | View Code Duplication | public function writeln($data) |
|
| 498 | |||
| 499 | /** |
||
| 500 | * Sets callback which will be called once when got EOF |
||
| 501 | * @param callable $cb |
||
| 502 | * @return this |
||
| 503 | */ |
||
| 504 | public function onEOF($cb = null) |
||
| 508 | |||
| 509 | public function getStatus() |
||
| 513 | } |
||
| 514 |
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.