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:
| 1 | <?php |
||
| 12 | trait ExecTrait |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * @var bool |
||
| 16 | */ |
||
| 17 | protected $background = false; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @var null|int |
||
| 21 | */ |
||
| 22 | protected $timeout = null; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var null|int |
||
| 26 | */ |
||
| 27 | protected $idleTimeout = null; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var null|array |
||
| 31 | */ |
||
| 32 | protected $env = null; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var Process |
||
| 36 | */ |
||
| 37 | protected $process; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var resource|string |
||
| 41 | */ |
||
| 42 | protected $input; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var boolean |
||
| 46 | */ |
||
| 47 | protected $interactive = false; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var bool |
||
| 51 | */ |
||
| 52 | protected $isPrinted = true; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var bool |
||
| 56 | */ |
||
| 57 | protected $isMetadataPrinted = true; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var string |
||
| 61 | */ |
||
| 62 | protected $workingDirectory; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @return string |
||
| 66 | */ |
||
| 67 | abstract function getCommand(); |
||
| 68 | |||
| 69 | /** Typically provided by Timer trait via ProgressIndicatorAwareTrait. */ |
||
| 70 | abstract function startTimer(); |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Typically provided by TaskIO Trait. |
||
| 76 | */ |
||
| 77 | abstract function hideTaskProgress(); |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @param string $text |
||
| 82 | * @param null|array $context |
||
| 83 | */ |
||
| 84 | abstract function printTaskInfo($text, $context = null); |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Typically provided by VerbosityThresholdTrait. |
||
| 88 | */ |
||
| 89 | abstract function verbosityMeetsThreshold(); |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Sets $this->interactive() based on posix_isatty(). |
||
| 94 | */ |
||
| 95 | public function detectInteractive() |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Executes command in background mode (asynchronously) |
||
| 108 | * |
||
| 109 | * @return $this |
||
| 110 | */ |
||
| 111 | public function background($arg = true) |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Stop command if it runs longer then $timeout in seconds |
||
| 119 | * |
||
| 120 | * @param int $timeout |
||
| 121 | * |
||
| 122 | * @return $this |
||
| 123 | */ |
||
| 124 | public function timeout($timeout) |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Stops command if it does not output something for a while |
||
| 132 | * |
||
| 133 | * @param int $timeout |
||
| 134 | * |
||
| 135 | * @return $this |
||
| 136 | */ |
||
| 137 | public function idleTimeout($timeout) |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Sets the environment variables for the command |
||
| 145 | * |
||
| 146 | * @param array $env |
||
| 147 | * |
||
| 148 | * @return $this |
||
| 149 | */ |
||
| 150 | public function env(array $env) |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Pass an input to the process. Can be resource created with fopen() or string |
||
| 158 | * |
||
| 159 | * @param resource|string $input |
||
| 160 | * |
||
| 161 | * @return $this |
||
| 162 | */ |
||
| 163 | public function setInput($input) |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Attach tty to process for interactive input |
||
| 171 | * |
||
| 172 | * @param $interactive bool |
||
| 173 | * |
||
| 174 | * @return $this |
||
| 175 | */ |
||
| 176 | public function interactive($interactive) |
||
| 181 | |||
| 182 | |||
| 183 | /** |
||
| 184 | * Is command printing its output to screen |
||
| 185 | * |
||
| 186 | * @return bool |
||
| 187 | */ |
||
| 188 | public function getPrinted() |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Changes working directory of command |
||
| 195 | * |
||
| 196 | * @param string $dir |
||
| 197 | * |
||
| 198 | * @return $this |
||
| 199 | */ |
||
| 200 | public function dir($dir) |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Shortcut for setting isPrinted() and isMetadataPrinted() to false. |
||
| 208 | * |
||
| 209 | * @param bool $arg |
||
| 210 | * |
||
| 211 | * @return $this |
||
| 212 | */ |
||
| 213 | public function silent($arg) |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Should command output be printed |
||
| 224 | * |
||
| 225 | * @param bool $arg |
||
| 226 | * |
||
| 227 | * @return $this |
||
| 228 | * |
||
| 229 | * @deprecated |
||
| 230 | */ |
||
| 231 | public function printed($arg) |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Should command output be printed |
||
| 239 | * |
||
| 240 | * @param bool $arg |
||
| 241 | * |
||
| 242 | * @return $this |
||
| 243 | */ |
||
| 244 | public function printOutput($arg) |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Should command metadata be printed. I,e., command and timer. |
||
| 254 | * |
||
| 255 | * @param bool $arg |
||
| 256 | * |
||
| 257 | * @return $this |
||
| 258 | */ |
||
| 259 | public function printMetadata($arg) |
||
| 266 | |||
| 267 | /** |
||
| 268 | * @param Process $process |
||
| 269 | * @param callable $output_callback |
||
| 270 | * |
||
| 271 | * @return \Robo\ResultData |
||
| 272 | */ |
||
| 273 | protected function execute($process, $output_callback = null) |
||
| 339 | |||
| 340 | /** |
||
| 341 | * |
||
| 342 | */ |
||
| 343 | protected function stop() |
||
| 353 | |||
| 354 | /** |
||
| 355 | * @param array $context |
||
| 356 | */ |
||
| 357 | protected function printAction($context = []) |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Gets the data array to be passed to Result(). |
||
| 369 | * |
||
| 370 | * @return array |
||
| 371 | * The data array passed to Result(). |
||
| 372 | */ |
||
| 373 | protected function getResultData() |
||
| 381 | } |
||
| 382 |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.