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 | protected $finishWrite; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @var string Command string |
||
| 20 | */ |
||
| 21 | protected $cmd; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var string Executable path |
||
| 25 | */ |
||
| 26 | public $binPath; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var array Opened pipes |
||
| 30 | */ |
||
| 31 | protected $pipes; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var resource Process descriptor |
||
| 35 | */ |
||
| 36 | protected $pd; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var resource FD write |
||
| 40 | */ |
||
| 41 | protected $fdWrite; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var boolean Output errors? |
||
| 45 | */ |
||
| 46 | protected $outputErrors = true; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var string SUID |
||
| 50 | */ |
||
| 51 | public $setUser; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var string SGID |
||
| 55 | */ |
||
| 56 | public $setGroup; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var string Chroot |
||
| 60 | */ |
||
| 61 | public $chroot = '/'; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var array Hash of environment's variables |
||
| 65 | */ |
||
| 66 | protected $env = []; // |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var string Chdir |
||
| 70 | */ |
||
| 71 | public $cwd; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var string Path to error logfile |
||
| 75 | */ |
||
| 76 | protected $errlogfile = null; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @var array Array of arguments |
||
| 80 | */ |
||
| 81 | protected $args; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @var integer Process priority |
||
| 85 | */ |
||
| 86 | protected $nice; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @var \EventBufferEvent |
||
| 90 | */ |
||
| 91 | protected $bevWrite; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @var \EventBufferEvent |
||
| 95 | */ |
||
| 96 | protected $bevErr; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @var boolean Got EOF? |
||
| 100 | */ |
||
| 101 | protected $EOF = false; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Get command string |
||
| 105 | * @return string |
||
| 106 | */ |
||
| 107 | public function getCmd() { |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Set group |
||
| 113 | * @return this |
||
|
|
|||
| 114 | */ |
||
| 115 | public function setGroup($val) { |
||
| 116 | $this->setGroup = $val; |
||
| 117 | return $this; |
||
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Set cwd |
||
| 122 | * @param string $dir |
||
| 123 | * @return this |
||
| 124 | */ |
||
| 125 | public function setCwd($dir) { |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Set group |
||
| 132 | * @param string $val |
||
| 133 | * @return this |
||
| 134 | */ |
||
| 135 | public function setUser($val) { |
||
| 136 | $this->setUser = $val; |
||
| 137 | return $this; |
||
| 138 | } |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Set chroot |
||
| 142 | * @param string $dir |
||
| 143 | * @return this |
||
| 144 | */ |
||
| 145 | public function setChroot($dir) { |
||
| 146 | $this->chroot = $dir; |
||
| 147 | return $this; |
||
| 148 | } |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Execute |
||
| 152 | * @param string $binPath Binpath |
||
| 153 | * @param callable $cb Callback |
||
| 154 | * @param array $args Optional. Arguments |
||
| 155 | * @param array $env Optional. Hash of environment's variables |
||
| 156 | */ |
||
| 157 | public static function exec($binPath = null, $cb = null, $args = null, $env = null) { |
||
| 158 | $o = new static; |
||
| 159 | $data = ''; |
||
| 160 | $o->bind('read', function($o) use (&$data, $o) { |
||
| 161 | $data .= $o->readUnlimited(); |
||
| 162 | }); |
||
| 163 | $o->bind('eof', function($o) use (&$data, $cb) { |
||
| 164 | call_user_func($cb, $o, $data); |
||
| 165 | $o->close(); |
||
| 166 | }); |
||
| 167 | $o->execute($binPath, $args, $env); |
||
| 168 | } |
||
| 169 | |||
| 170 | |||
| 171 | /** |
||
| 172 | * Sets fd |
||
| 173 | * @param resource $fd File descriptor |
||
| 174 | * @param \EventBufferEvent $bev |
||
| 175 | * @return void |
||
| 176 | */ |
||
| 177 | public function setFd($fd, $bev = null) { |
||
| 178 | $this->fd = $fd; |
||
| 179 | if ($fd === false) { |
||
| 180 | $this->finish(); |
||
| 181 | return; |
||
| 182 | } |
||
| 183 | $this->fdWrite = $this->pipes[0]; |
||
| 184 | $flags = !is_resource($this->fd) ? \EventBufferEvent::OPT_CLOSE_ON_FREE : 0; |
||
| 185 | $flags |= \EventBufferEvent::OPT_DEFER_CALLBACKS; /* buggy option */ |
||
| 186 | $this->bev = new \EventBufferEvent(Daemon::$process->eventBase, $this->fd, 0, [$this, 'onReadEv'], null, [$this, 'onStateEv']); |
||
| 187 | $this->bevWrite = new \EventBufferEvent(Daemon::$process->eventBase, $this->fdWrite, 0, null, [$this, 'onWriteEv'], null); |
||
| 188 | if (!$this->bev || !$this->bevWrite) { |
||
| 189 | $this->finish(); |
||
| 190 | return; |
||
| 191 | } |
||
| 192 | if ($this->priority !== null) { |
||
| 193 | $this->bev->priority = $this->priority; |
||
| 194 | } |
||
| 195 | if ($this->timeout !== null) { |
||
| 196 | $this->setTimeout($this->timeout); |
||
| 197 | } |
||
| 198 | View Code Duplication | if (!$this->bev->enable(\Event::READ | \Event::TIMEOUT | \Event::PERSIST)) { |
|
| 199 | $this->finish(); |
||
| 200 | return; |
||
| 201 | } |
||
| 202 | View Code Duplication | if (!$this->bevWrite->enable(\Event::WRITE | \Event::TIMEOUT | \Event::PERSIST)) { |
|
| 203 | $this->finish(); |
||
| 204 | return; |
||
| 205 | } |
||
| 206 | $this->bev->setWatermark(\Event::READ, $this->lowMark, $this->highMark); |
||
| 207 | |||
| 208 | init: |
||
| 209 | if (!$this->inited) { |
||
| 210 | $this->inited = true; |
||
| 211 | $this->init(); |
||
| 212 | } |
||
| 213 | } |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Sets an array of arguments |
||
| 217 | * @param array Arguments |
||
| 218 | * @return this |
||
| 219 | */ |
||
| 220 | public function setArgs($args = NULL) { |
||
| 221 | $this->args = $args; |
||
| 222 | |||
| 223 | return $this; |
||
| 224 | } |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Set a hash of environment's variables |
||
| 228 | * @param array Hash of environment's variables |
||
| 229 | * @return this |
||
| 230 | */ |
||
| 231 | public function setEnv($env = NULL) { |
||
| 232 | $this->env = $env; |
||
| 233 | |||
| 234 | return $this; |
||
| 235 | } |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Called when got EOF |
||
| 239 | * @return void |
||
| 240 | */ |
||
| 241 | public function onEofEvent() { |
||
| 242 | if ($this->EOF) { |
||
| 243 | return; |
||
| 244 | } |
||
| 245 | $this->EOF = true; |
||
| 246 | |||
| 247 | $this->event('eof'); |
||
| 248 | } |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Set priority |
||
| 252 | * @param integer $nice Priority |
||
| 253 | * @return this |
||
| 254 | */ |
||
| 255 | public function nice($nice = NULL) { |
||
| 256 | $this->nice = $nice; |
||
| 257 | |||
| 258 | return $this; |
||
| 259 | } |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Called when new data received |
||
| 263 | * @return this|null |
||
| 264 | */ |
||
| 265 | protected function onRead() { |
||
| 266 | if (func_num_args() === 1) { |
||
| 267 | $this->onRead = func_get_arg(0); |
||
| 268 | return $this; |
||
| 269 | } |
||
| 270 | $this->event('read'); |
||
| 271 | } |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Build arguments string from associative/enumerated array (may be mixed) |
||
| 275 | * @param array $args |
||
| 276 | * @return string |
||
| 277 | */ |
||
| 278 | public static function buildArgs($args) { |
||
| 279 | if (!is_array($args)) { |
||
| 280 | return ''; |
||
| 281 | } |
||
| 282 | $ret = ''; |
||
| 283 | foreach ($args as $k => $v) { |
||
| 284 | if (!is_int($v) && ($v !== null)) { |
||
| 285 | $v = escapeshellarg($v); |
||
| 286 | } |
||
| 287 | if (is_int($k)) { |
||
| 288 | $ret .= ' ' . $v; |
||
| 289 | } else { |
||
| 290 | if ($k{0} !== '-') { |
||
| 291 | $ret .= ' --' . $k . ($v !== null ? '=' . $v : ''); |
||
| 292 | } else { |
||
| 293 | $ret .= ' ' . $k . ($v !== null ? '=' . $v : ''); |
||
| 294 | } |
||
| 295 | } |
||
| 296 | } |
||
| 297 | return $ret; |
||
| 298 | } |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Execute |
||
| 302 | * @param string $binPath Optional. Binpath |
||
| 303 | * @param array $args Optional. Arguments |
||
| 304 | * @param array $env Optional. Hash of environment's variables |
||
| 305 | * @return this |
||
| 306 | */ |
||
| 307 | public function execute($binPath = NULL, $args = NULL, $env = NULL) { |
||
| 308 | if ($binPath !== NULL) { |
||
| 309 | $this->binPath = $binPath; |
||
| 310 | } |
||
| 311 | |||
| 312 | if ($env !== NULL) { |
||
| 313 | $this->env = $env; |
||
| 314 | } |
||
| 315 | |||
| 316 | if ($args !== NULL) { |
||
| 317 | $this->args = $args; |
||
| 318 | } |
||
| 319 | $this->cmd = $this->binPath . static::buildArgs($this->args) . ($this->outputErrors ? ' 2>&1' : ''); |
||
| 320 | |||
| 321 | if ( |
||
| 322 | isset($this->setUser) |
||
| 323 | || isset($this->setGroup) |
||
| 324 | ) { |
||
| 325 | if ( |
||
| 326 | isset($this->setUser) |
||
| 327 | && isset($this->setGroup) |
||
| 328 | && ($this->setUser !== $this->setGroup) |
||
| 329 | ) { |
||
| 330 | $this->cmd = 'sudo -g ' . escapeshellarg($this->setGroup) . ' -u ' . escapeshellarg($this->setUser) . ' ' . $this->cmd; |
||
| 331 | } |
||
| 332 | else { |
||
| 333 | $this->cmd = 'su ' . escapeshellarg($this->setGroup) . ' -c ' . escapeshellarg($this->cmd); |
||
| 334 | } |
||
| 335 | } |
||
| 336 | |||
| 337 | if ($this->chroot !== '/') { |
||
| 338 | $this->cmd = 'chroot ' . escapeshellarg($this->chroot) . ' ' . $this->cmd; |
||
| 339 | } |
||
| 340 | |||
| 341 | if ($this->nice !== NULL) { |
||
| 342 | $this->cmd = 'nice -n ' . ((int)$this->nice) . ' ' . $this->cmd; |
||
| 343 | } |
||
| 344 | |||
| 345 | $pipesDescr = [ |
||
| 346 | 0 => ['pipe', 'r'], // stdin is a pipe that the child will read from |
||
| 347 | 1 => ['pipe', 'w'] // stdout is a pipe that the child will write to |
||
| 348 | ]; |
||
| 349 | |||
| 350 | if ( |
||
| 351 | ($this->errlogfile !== NULL) |
||
| 352 | && !$this->outputErrors |
||
| 353 | ) { |
||
| 354 | $pipesDescr[2] = ['file', $this->errlogfile, 'a']; // @TODO: refactoring |
||
| 355 | } |
||
| 356 | |||
| 357 | $this->pd = proc_open($this->cmd, $pipesDescr, $this->pipes, $this->cwd, $this->env); |
||
| 358 | if ($this->pd) { |
||
| 359 | $this->setFd($this->pipes[1]); |
||
| 360 | } |
||
| 361 | |||
| 362 | return $this; |
||
| 363 | } |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Finish write stream |
||
| 367 | * @return boolean |
||
| 368 | */ |
||
| 369 | public function finishWrite() { |
||
| 370 | if (!$this->writing) { |
||
| 371 | $this->closeWrite(); |
||
| 372 | } |
||
| 373 | |||
| 374 | $this->finishWrite = true; |
||
| 375 | |||
| 376 | return true; |
||
| 377 | } |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Close the process |
||
| 381 | * @return void |
||
| 382 | */ |
||
| 383 | public function close() { |
||
| 384 | parent::close(); |
||
| 385 | $this->closeWrite(); |
||
| 386 | if (is_resource($this->pd)) { |
||
| 387 | proc_close($this->pd); |
||
| 388 | } |
||
| 389 | } |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Called when stream is finished |
||
| 393 | */ |
||
| 394 | public function onFinish() { |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Close write stream |
||
| 400 | * @return this |
||
| 401 | */ |
||
| 402 | public function closeWrite() { |
||
| 417 | |||
| 418 | /** |
||
| 419 | * Got EOF? |
||
| 420 | * @return boolean |
||
| 421 | */ |
||
| 422 | public function eof() { |
||
| 426 | |||
| 427 | /** |
||
| 428 | * Send data to the connection. Note that it just writes to buffer that flushes at every baseloop |
||
| 429 | * @param string $data Data to send |
||
| 430 | * @return boolean Success |
||
| 431 | */ |
||
| 432 | View Code Duplication | public function write($data) { |
|
| 433 | if (!$this->alive) { |
||
| 434 | Daemon::log('Attempt to write to dead IOStream (' . get_class($this) . ')'); |
||
| 435 | return false; |
||
| 436 | } |
||
| 437 | if (!isset($this->bevWrite)) { |
||
| 438 | return false; |
||
| 439 | } |
||
| 440 | if (!strlen($data)) { |
||
| 441 | return true; |
||
| 442 | } |
||
| 443 | $this->writing = true; |
||
| 444 | Daemon::$noError = true; |
||
| 445 | if (!$this->bevWrite->write($data) || !Daemon::$noError) { |
||
| 446 | $this->close(); |
||
| 447 | return false; |
||
| 448 | } |
||
| 449 | return true; |
||
| 450 | } |
||
| 451 | |||
| 452 | /** |
||
| 453 | * Send data and appending \n to connection. Note that it just writes to buffer flushed at every baseloop |
||
| 454 | * @param string Data to send |
||
| 455 | * @return boolean Success |
||
| 456 | */ |
||
| 457 | View Code Duplication | public function writeln($data) { |
|
| 458 | if (!$this->alive) { |
||
| 459 | Daemon::log('Attempt to write to dead IOStream (' . get_class($this) . ')'); |
||
| 460 | return false; |
||
| 461 | } |
||
| 462 | if (!isset($this->bevWrite)) { |
||
| 463 | return false; |
||
| 464 | } |
||
| 465 | if (!strlen($data) && !strlen($this->EOL)) { |
||
| 466 | return true; |
||
| 467 | } |
||
| 468 | $this->writing = true; |
||
| 469 | $this->bevWrite->write($data); |
||
| 470 | $this->bevWrite->write($this->EOL); |
||
| 471 | return true; |
||
| 472 | } |
||
| 473 | |||
| 474 | /** |
||
| 475 | * Sets callback which will be called once when got EOF |
||
| 476 | * @param callable $cb |
||
| 477 | * @return this |
||
| 478 | */ |
||
| 479 | public function onEOF($cb = NULL) { |
||
| 483 | |||
| 484 | public function getStatus() |
||
| 485 | { |
||
| 486 | return !empty($this->pd) ? proc_get_status($this->pd) : null; |
||
| 487 | } |
||
| 488 | } |
||
| 489 |
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.