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 | /** |
||
| 70 | * Sets $this->interactive() based on posix_isatty(). |
||
| 71 | */ |
||
| 72 | public function detectInteractive() |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Executes command in background mode (asynchronously) |
||
| 81 | * |
||
| 82 | * @return $this |
||
| 83 | */ |
||
| 84 | public function background($arg = true) |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Stop command if it runs longer then $timeout in seconds |
||
| 92 | * |
||
| 93 | * @param int $timeout |
||
| 94 | * |
||
| 95 | * @return $this |
||
| 96 | */ |
||
| 97 | public function timeout($timeout) |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Stops command if it does not output something for a while |
||
| 105 | * |
||
| 106 | * @param int $timeout |
||
| 107 | * |
||
| 108 | * @return $this |
||
| 109 | */ |
||
| 110 | public function idleTimeout($timeout) |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Sets the environment variables for the command |
||
| 118 | * |
||
| 119 | * @param array $env |
||
| 120 | * |
||
| 121 | * @return $this |
||
| 122 | */ |
||
| 123 | public function env(array $env) |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Pass an input to the process. Can be resource created with fopen() or string |
||
| 131 | * |
||
| 132 | * @param resource|string $input |
||
| 133 | * |
||
| 134 | * @return $this |
||
| 135 | */ |
||
| 136 | public function setInput($input) |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Attach tty to process for interactive input |
||
| 144 | * |
||
| 145 | * @param $interactive bool |
||
| 146 | * |
||
| 147 | * @return $this |
||
| 148 | */ |
||
| 149 | public function interactive($interactive) |
||
| 154 | |||
| 155 | |||
| 156 | /** |
||
| 157 | * Is command printing its output to screen |
||
| 158 | * |
||
| 159 | * @return bool |
||
| 160 | */ |
||
| 161 | public function getPrinted() |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Changes working directory of command |
||
| 168 | * |
||
| 169 | * @param string $dir |
||
| 170 | * |
||
| 171 | * @return $this |
||
| 172 | */ |
||
| 173 | public function dir($dir) |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Shortcut for setting isPrinted() and isMetadataPrinted() to false. |
||
| 181 | * |
||
| 182 | * @param bool $arg |
||
| 183 | * |
||
| 184 | * @return $this |
||
| 185 | */ |
||
| 186 | public function silent($arg) |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Should command output be printed |
||
| 197 | * |
||
| 198 | * @param bool $arg |
||
| 199 | * |
||
| 200 | * @return $this |
||
| 201 | * |
||
| 202 | * @deprecated |
||
| 203 | */ |
||
| 204 | public function printed($arg) |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Should command output be printed |
||
| 212 | * |
||
| 213 | * @param bool $arg |
||
| 214 | * |
||
| 215 | * @return $this |
||
| 216 | */ |
||
| 217 | public function printOutput($arg) |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Should command metadata be printed. I,e., command and timer. |
||
| 227 | * |
||
| 228 | * @param bool $arg |
||
| 229 | * |
||
| 230 | * @return $this |
||
| 231 | */ |
||
| 232 | public function printMetadata($arg) |
||
| 239 | |||
| 240 | /** |
||
| 241 | * |
||
| 242 | */ |
||
| 243 | public function __destruct() |
||
| 247 | |||
| 248 | /** |
||
| 249 | * {@inheritdoc} |
||
| 250 | */ |
||
| 251 | public function run() |
||
| 262 | |||
| 263 | /** |
||
| 264 | * @param $command |
||
| 265 | * @param null $output_callback |
||
| 266 | * |
||
| 267 | * @return \Robo\Result |
||
| 268 | */ |
||
| 269 | public function execute($command, $output_callback = null) |
||
| 320 | |||
| 321 | /** |
||
| 322 | * |
||
| 323 | */ |
||
| 324 | protected function stop() |
||
| 334 | |||
| 335 | /** |
||
| 336 | * @param array $context |
||
| 337 | */ |
||
| 338 | protected function printAction($context = []) |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Gets the data array to be passed to Result(). |
||
| 350 | * |
||
| 351 | * @return array |
||
| 352 | * The data array passed to Result(). |
||
| 353 | */ |
||
| 354 | protected function getResultData() |
||
| 362 | |||
| 363 | } |
||
| 364 |
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.