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 |
||
| 13 | trait ExecTrait |
||
| 14 | { |
||
| 15 | |||
| 16 | use LoggerAwareTrait; |
||
| 17 | use Timer; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @var bool |
||
| 21 | */ |
||
| 22 | protected $background = false; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var null|int |
||
| 26 | */ |
||
| 27 | protected $timeout = null; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var null|int |
||
| 31 | */ |
||
| 32 | protected $idleTimeout = null; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var null|array |
||
| 36 | */ |
||
| 37 | protected $env = null; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var Process |
||
| 41 | */ |
||
| 42 | protected $process; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var resource|string |
||
| 46 | */ |
||
| 47 | protected $input; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var boolean |
||
| 51 | */ |
||
| 52 | protected $interactive = false; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var bool |
||
| 56 | */ |
||
| 57 | protected $isPrinted = true; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var bool |
||
| 61 | */ |
||
| 62 | protected $isMetadataPrinted = true; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var string |
||
| 66 | */ |
||
| 67 | protected $workingDirectory; |
||
| 68 | |||
| 69 | /** @var string */ |
||
| 70 | protected $command; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Sets $this->interactive() based on posix_isatty(). |
||
| 74 | */ |
||
| 75 | public function detectInteractive() |
||
| 76 | { |
||
| 77 | if (!isset($this->interactive) && function_exists('posix_isatty')) { |
||
| 78 | $this->interactive = posix_isatty(STDOUT); |
||
| 79 | } |
||
| 80 | } |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Executes command in background mode (asynchronously) |
||
| 84 | * |
||
| 85 | * @return $this |
||
| 86 | */ |
||
| 87 | public function background($arg = true) |
||
| 88 | { |
||
| 89 | $this->background = $arg; |
||
| 90 | return $this; |
||
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Stop command if it runs longer then $timeout in seconds |
||
| 95 | * |
||
| 96 | * @param int $timeout |
||
| 97 | * |
||
| 98 | * @return $this |
||
| 99 | */ |
||
| 100 | public function timeout($timeout) |
||
| 101 | { |
||
| 102 | $this->timeout = $timeout; |
||
| 103 | return $this; |
||
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Stops command if it does not output something for a while |
||
| 108 | * |
||
| 109 | * @param int $timeout |
||
| 110 | * |
||
| 111 | * @return $this |
||
| 112 | */ |
||
| 113 | public function idleTimeout($timeout) |
||
| 114 | { |
||
| 115 | $this->idleTimeout = $timeout; |
||
| 116 | return $this; |
||
| 117 | } |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Sets the environment variables for the command |
||
| 121 | * |
||
| 122 | * @param array $env |
||
| 123 | * |
||
| 124 | * @return $this |
||
| 125 | */ |
||
| 126 | public function env(array $env) |
||
| 127 | { |
||
| 128 | $this->env = $env; |
||
| 129 | return $this; |
||
| 130 | } |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Pass an input to the process. Can be resource created with fopen() or string |
||
| 134 | * |
||
| 135 | * @param resource|string $input |
||
| 136 | * |
||
| 137 | * @return $this |
||
| 138 | */ |
||
| 139 | public function setInput($input) |
||
| 140 | { |
||
| 141 | $this->input = $input; |
||
| 142 | return $this; |
||
| 143 | } |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Attach tty to process for interactive input |
||
| 147 | * |
||
| 148 | * @param $interactive bool |
||
| 149 | * |
||
| 150 | * @return $this |
||
| 151 | */ |
||
| 152 | public function interactive($interactive) |
||
| 153 | { |
||
| 154 | $this->interactive = $interactive; |
||
| 155 | return $this; |
||
| 156 | } |
||
| 157 | |||
| 158 | |||
| 159 | /** |
||
| 160 | * Is command printing its output to screen |
||
| 161 | * |
||
| 162 | * @return bool |
||
| 163 | */ |
||
| 164 | public function getPrinted() |
||
| 165 | { |
||
| 166 | return $this->isPrinted; |
||
| 167 | } |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Changes working directory of command |
||
| 171 | * |
||
| 172 | * @param string $dir |
||
| 173 | * |
||
| 174 | * @return $this |
||
| 175 | */ |
||
| 176 | public function dir($dir) |
||
| 177 | { |
||
| 178 | $this->workingDirectory = $dir; |
||
| 179 | return $this; |
||
| 180 | } |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Shortcut for setting isPrinted() and isMetadataPrinted() to false. |
||
| 184 | * |
||
| 185 | * @param bool $arg |
||
| 186 | * |
||
| 187 | * @return $this |
||
| 188 | */ |
||
| 189 | public function silent($arg) |
||
| 190 | { |
||
| 191 | if (is_bool($arg)) { |
||
| 192 | $this->isPrinted = !$arg; |
||
| 193 | $this->isMetadataPrinted = !$arg; |
||
| 194 | } |
||
| 195 | return $this; |
||
| 196 | } |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Should command output be printed |
||
| 200 | * |
||
| 201 | * @param bool $arg |
||
| 202 | * |
||
| 203 | * @return $this |
||
| 204 | * |
||
| 205 | * @deprecated |
||
| 206 | */ |
||
| 207 | public function printed($arg) |
||
| 208 | { |
||
| 209 | $this->logger->warning("printed() is deprecated. Please use printOutput()."); |
||
| 210 | return $this->printOutput($arg); |
||
| 211 | } |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Should command output be printed |
||
| 215 | * |
||
| 216 | * @param bool $arg |
||
| 217 | * |
||
| 218 | * @return $this |
||
| 219 | */ |
||
| 220 | public function printOutput($arg) |
||
| 221 | { |
||
| 222 | if (is_bool($arg)) { |
||
| 223 | $this->isPrinted = $arg; |
||
| 224 | } |
||
| 225 | return $this; |
||
| 226 | } |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Should command metadata be printed. I,e., command and timer. |
||
| 230 | * |
||
| 231 | * @param bool $arg |
||
| 232 | * |
||
| 233 | * @return $this |
||
| 234 | */ |
||
| 235 | public function printMetadata($arg) |
||
| 236 | { |
||
| 237 | if (is_bool($arg)) { |
||
| 238 | $this->isMetadataPrinted = $arg; |
||
| 239 | } |
||
| 240 | return $this; |
||
| 241 | } |
||
| 242 | |||
| 243 | /** |
||
| 244 | * |
||
| 245 | */ |
||
| 246 | public function __destruct() |
||
| 247 | { |
||
| 248 | if (!$this->background()) { |
||
| 249 | $this->stop(); |
||
| 250 | } |
||
| 251 | } |
||
| 252 | |||
| 253 | /** |
||
| 254 | * {@inheritdoc} |
||
| 255 | */ |
||
| 256 | public function run() |
||
| 257 | { |
||
| 258 | return $this->execute($this->getCommand()); |
||
|
|
|||
| 259 | } |
||
| 260 | |||
| 261 | /** |
||
| 262 | * @param string $command |
||
| 263 | * @param callable $output_callback |
||
| 264 | * |
||
| 265 | * @return \Robo\Result |
||
| 266 | */ |
||
| 267 | protected function execute($command, $output_callback = null) |
||
| 268 | { |
||
| 269 | if (!$output_callback) { |
||
| 270 | $output_callback = function ($type, $buffer) { |
||
| 271 | print($buffer); |
||
| 272 | }; |
||
| 273 | } |
||
| 274 | |||
| 275 | if ($this->isMetadataPrinted) { |
||
| 276 | $this->printAction(); |
||
| 277 | } |
||
| 278 | $this->process = new Process($command); |
||
| 279 | $this->process->setTimeout($this->timeout); |
||
| 280 | $this->process->setIdleTimeout($this->idleTimeout); |
||
| 281 | $this->process->setWorkingDirectory($this->workingDirectory); |
||
| 282 | |||
| 283 | if ($this->input) { |
||
| 284 | $this->process->setInput($this->input); |
||
| 285 | } |
||
| 286 | |||
| 287 | if ($this->interactive) { |
||
| 288 | $this->process->setTty(true); |
||
| 289 | } |
||
| 290 | |||
| 291 | if (isset($this->env)) { |
||
| 292 | $this->process->setEnv($this->env); |
||
| 293 | } |
||
| 294 | |||
| 295 | View Code Duplication | if (!$this->background and !$this->isPrinted) { |
|
| 296 | $this->startTimer(); |
||
| 297 | $this->process->run(); |
||
| 298 | $this->stopTimer(); |
||
| 299 | return new Result($this, $this->process->getExitCode(), |
||
| 300 | $this->process->getOutput(), $this->getResultData()); |
||
| 301 | } |
||
| 302 | |||
| 303 | View Code Duplication | if (!$this->background and $this->isPrinted) { |
|
| 304 | $this->startTimer(); |
||
| 305 | $this->process->run($output_callback); |
||
| 306 | $this->stopTimer(); |
||
| 307 | return new Result($this, $this->process->getExitCode(), |
||
| 308 | $this->process->getOutput(), $this->getResultData()); |
||
| 309 | } |
||
| 310 | |||
| 311 | try { |
||
| 312 | $this->process->start(); |
||
| 313 | } catch (\Exception $e) { |
||
| 314 | return Result::fromException($this, $e); |
||
| 315 | } |
||
| 316 | return Result::success($this); |
||
| 317 | } |
||
| 318 | |||
| 319 | /** |
||
| 320 | * |
||
| 321 | */ |
||
| 322 | protected function stop() |
||
| 323 | { |
||
| 324 | if ($this->background && $this->process->isRunning()) { |
||
| 325 | $this->process->stop(); |
||
| 326 | $this->printTaskInfo( |
||
| 327 | "Stopped {command}", |
||
| 328 | ['command' => $this->getCommand()] |
||
| 329 | ); |
||
| 330 | } |
||
| 331 | } |
||
| 332 | |||
| 333 | /** |
||
| 334 | * @param array $context |
||
| 335 | */ |
||
| 336 | protected function printAction($context = []) |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Gets the data array to be passed to Result(). |
||
| 348 | * |
||
| 349 | * @return array |
||
| 350 | * The data array passed to Result(). |
||
| 351 | */ |
||
| 352 | protected function getResultData() |
||
| 360 | } |
||
| 361 |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.