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 getCommandDescription(); |
||
|
|
|||
| 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(); |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Typically provided by VerbosityThresholdTrait. |
||
| 83 | */ |
||
| 84 | abstract function verbosityMeetsThreshold(); |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Sets $this->interactive() based on posix_isatty(). |
||
| 89 | */ |
||
| 90 | public function detectInteractive() |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Executes command in background mode (asynchronously) |
||
| 103 | * |
||
| 104 | * @return $this |
||
| 105 | */ |
||
| 106 | public function background($arg = true) |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Stop command if it runs longer then $timeout in seconds |
||
| 114 | * |
||
| 115 | * @param int $timeout |
||
| 116 | * |
||
| 117 | * @return $this |
||
| 118 | */ |
||
| 119 | public function timeout($timeout) |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Stops command if it does not output something for a while |
||
| 127 | * |
||
| 128 | * @param int $timeout |
||
| 129 | * |
||
| 130 | * @return $this |
||
| 131 | */ |
||
| 132 | public function idleTimeout($timeout) |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Sets the environment variables for the command |
||
| 140 | * |
||
| 141 | * @param array $env |
||
| 142 | * |
||
| 143 | * @return $this |
||
| 144 | */ |
||
| 145 | public function env(array $env) |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Pass an input to the process. Can be resource created with fopen() or string |
||
| 153 | * |
||
| 154 | * @param resource|string $input |
||
| 155 | * |
||
| 156 | * @return $this |
||
| 157 | */ |
||
| 158 | public function setInput($input) |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Attach tty to process for interactive input |
||
| 166 | * |
||
| 167 | * @param $interactive bool |
||
| 168 | * |
||
| 169 | * @return $this |
||
| 170 | */ |
||
| 171 | public function interactive($interactive) |
||
| 176 | |||
| 177 | |||
| 178 | /** |
||
| 179 | * Is command printing its output to screen |
||
| 180 | * |
||
| 181 | * @return bool |
||
| 182 | */ |
||
| 183 | public function getPrinted() |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Changes working directory of command |
||
| 190 | * |
||
| 191 | * @param string $dir |
||
| 192 | * |
||
| 193 | * @return $this |
||
| 194 | */ |
||
| 195 | public function dir($dir) |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Shortcut for setting isPrinted() and isMetadataPrinted() to false. |
||
| 203 | * |
||
| 204 | * @param bool $arg |
||
| 205 | * |
||
| 206 | * @return $this |
||
| 207 | */ |
||
| 208 | public function silent($arg) |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Should command output be printed |
||
| 219 | * |
||
| 220 | * @param bool $arg |
||
| 221 | * |
||
| 222 | * @return $this |
||
| 223 | * |
||
| 224 | * @deprecated |
||
| 225 | */ |
||
| 226 | public function printed($arg) |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Should command output be printed |
||
| 234 | * |
||
| 235 | * @param bool $arg |
||
| 236 | * |
||
| 237 | * @return $this |
||
| 238 | */ |
||
| 239 | public function printOutput($arg) |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Should command metadata be printed. I,e., command and timer. |
||
| 249 | * |
||
| 250 | * @param bool $arg |
||
| 251 | * |
||
| 252 | * @return $this |
||
| 253 | */ |
||
| 254 | public function printMetadata($arg) |
||
| 261 | |||
| 262 | /** |
||
| 263 | * @param Process $process |
||
| 264 | * @param callable $output_callback |
||
| 265 | * |
||
| 266 | * @return \Robo\ResultData |
||
| 267 | */ |
||
| 268 | protected function execute($process, $output_callback = null) |
||
| 334 | |||
| 335 | /** |
||
| 336 | * |
||
| 337 | */ |
||
| 338 | protected function stop() |
||
| 348 | |||
| 349 | /** |
||
| 350 | * @param array $context |
||
| 351 | */ |
||
| 352 | protected function printAction($context = []) |
||
| 361 | |||
| 362 | /** |
||
| 363 | * Gets the data array to be passed to Result(). |
||
| 364 | * |
||
| 365 | * @return array |
||
| 366 | * The data array passed to Result(). |
||
| 367 | */ |
||
| 368 | protected function getResultData() |
||
| 376 | } |
||
| 377 |
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.