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 | * Sets $this->interactive() based on posix_isatty(). |
||
| 66 | */ |
||
| 67 | public function detectInteractive() |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Executes command in background mode (asynchronously) |
||
| 80 | * |
||
| 81 | * @return $this |
||
| 82 | */ |
||
| 83 | public function background($arg = true) |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Stop command if it runs longer then $timeout in seconds |
||
| 91 | * |
||
| 92 | * @param int $timeout |
||
| 93 | * |
||
| 94 | * @return $this |
||
| 95 | */ |
||
| 96 | public function timeout($timeout) |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Stops command if it does not output something for a while |
||
| 104 | * |
||
| 105 | * @param int $timeout |
||
| 106 | * |
||
| 107 | * @return $this |
||
| 108 | */ |
||
| 109 | public function idleTimeout($timeout) |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Sets the environment variables for the command |
||
| 117 | * |
||
| 118 | * @param array $env |
||
| 119 | * |
||
| 120 | * @return $this |
||
| 121 | */ |
||
| 122 | public function env(array $env) |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Pass an input to the process. Can be resource created with fopen() or string |
||
| 130 | * |
||
| 131 | * @param resource|string $input |
||
| 132 | * |
||
| 133 | * @return $this |
||
| 134 | */ |
||
| 135 | public function setInput($input) |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Attach tty to process for interactive input |
||
| 143 | * |
||
| 144 | * @param $interactive bool |
||
| 145 | * |
||
| 146 | * @return $this |
||
| 147 | */ |
||
| 148 | public function interactive($interactive) |
||
| 153 | |||
| 154 | |||
| 155 | /** |
||
| 156 | * Is command printing its output to screen |
||
| 157 | * |
||
| 158 | * @return bool |
||
| 159 | */ |
||
| 160 | public function getPrinted() |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Changes working directory of command |
||
| 167 | * |
||
| 168 | * @param string $dir |
||
| 169 | * |
||
| 170 | * @return $this |
||
| 171 | */ |
||
| 172 | public function dir($dir) |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Shortcut for setting isPrinted() and isMetadataPrinted() to false. |
||
| 180 | * |
||
| 181 | * @param bool $arg |
||
| 182 | * |
||
| 183 | * @return $this |
||
| 184 | */ |
||
| 185 | View Code Duplication | public function silent($arg) |
|
| 193 | |||
| 194 | /** |
||
| 195 | * Should command output be printed |
||
| 196 | * |
||
| 197 | * @param bool $arg |
||
| 198 | * |
||
| 199 | * @return $this |
||
| 200 | * |
||
| 201 | * @deprecated |
||
| 202 | */ |
||
| 203 | public function printed($arg) |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Should command output be printed |
||
| 211 | * |
||
| 212 | * @param bool $arg |
||
| 213 | * |
||
| 214 | * @return $this |
||
| 215 | */ |
||
| 216 | public function printOutput($arg) |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Should command metadata be printed. I,e., command and timer. |
||
| 226 | * |
||
| 227 | * @param bool $arg |
||
| 228 | * |
||
| 229 | * @return $this |
||
| 230 | */ |
||
| 231 | public function printMetadata($arg) |
||
| 238 | |||
| 239 | /** |
||
| 240 | * @param Process $process |
||
| 241 | * @param callable $output_callback |
||
| 242 | * |
||
| 243 | * @return \Robo\ResultData |
||
| 244 | */ |
||
| 245 | protected function execute($process, $output_callback = null) |
||
| 311 | |||
| 312 | /** |
||
| 313 | * |
||
| 314 | */ |
||
| 315 | View Code Duplication | protected function stop() |
|
| 325 | |||
| 326 | /** |
||
| 327 | * @param array $context |
||
| 328 | */ |
||
| 329 | protected function printAction($context = []) |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Gets the data array to be passed to Result(). |
||
| 341 | * |
||
| 342 | * @return array |
||
| 343 | * The data array passed to Result(). |
||
| 344 | */ |
||
| 345 | protected function getResultData() |
||
| 353 | } |
||
| 354 |
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.