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 | use LoggerAwareTrait; |
||
| 16 | use Timer; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @var bool |
||
| 20 | */ |
||
| 21 | protected $background = FALSE; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var null|int |
||
| 25 | */ |
||
| 26 | protected $timeout = NULL; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var null|int |
||
| 30 | */ |
||
| 31 | protected $idleTimeout = NULL; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var null|array |
||
| 35 | */ |
||
| 36 | protected $env = NULL; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var Process |
||
| 40 | */ |
||
| 41 | protected $process; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var resource|string |
||
| 45 | */ |
||
| 46 | protected $input; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var boolean |
||
| 50 | */ |
||
| 51 | protected $interactive; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var bool |
||
| 55 | */ |
||
| 56 | protected $isPrinted = TRUE; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var bool |
||
| 60 | */ |
||
| 61 | protected $isMetadataPrinted = TRUE; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var string |
||
| 65 | */ |
||
| 66 | protected $workingDirectory; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Sets $this->interactive() based on posix_isatty(). |
||
| 70 | */ |
||
| 71 | public function detectInteractive() { |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Executes command in background mode (asynchronously) |
||
| 79 | * |
||
| 80 | * @return $this |
||
| 81 | */ |
||
| 82 | public function background($arg = TRUE) { |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Stop command if it runs longer then $timeout in seconds |
||
| 89 | * |
||
| 90 | * @param int $timeout |
||
| 91 | * |
||
| 92 | * @return $this |
||
| 93 | */ |
||
| 94 | public function timeout($timeout) { |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Stops command if it does not output something for a while |
||
| 101 | * |
||
| 102 | * @param int $timeout |
||
| 103 | * |
||
| 104 | * @return $this |
||
| 105 | */ |
||
| 106 | public function idleTimeout($timeout) { |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Sets the environment variables for the command |
||
| 113 | * |
||
| 114 | * @param array $env |
||
| 115 | * |
||
| 116 | * @return $this |
||
| 117 | */ |
||
| 118 | public function env(array $env) { |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Pass an input to the process. Can be resource created with fopen() or string |
||
| 125 | * |
||
| 126 | * @param resource|string $input |
||
| 127 | * |
||
| 128 | * @return $this |
||
| 129 | */ |
||
| 130 | public function setInput($input) { |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Attach tty to process for interactive input |
||
| 137 | * |
||
| 138 | * @param $interactive bool |
||
| 139 | * |
||
| 140 | * @return $this |
||
| 141 | */ |
||
| 142 | public function interactive($interactive) { |
||
| 146 | |||
| 147 | |||
| 148 | /** |
||
| 149 | * Is command printing its output to screen |
||
| 150 | * |
||
| 151 | * @return bool |
||
| 152 | */ |
||
| 153 | public function getPrinted() { |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Changes working directory of command |
||
| 159 | * |
||
| 160 | * @param string $dir |
||
| 161 | * |
||
| 162 | * @return $this |
||
| 163 | */ |
||
| 164 | public function dir($dir) { |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Shortcut for setting isPrinted() and isMetadataPrinted() to false. |
||
| 171 | * |
||
| 172 | * @param bool $arg |
||
| 173 | * |
||
| 174 | * @return $this |
||
| 175 | */ |
||
| 176 | public function silent($arg) { |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Should command output be printed |
||
| 186 | * |
||
| 187 | * @param bool $arg |
||
| 188 | * |
||
| 189 | * @return $this |
||
| 190 | * |
||
| 191 | * @deprecated |
||
| 192 | */ |
||
| 193 | public function printed($arg) { |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Should command output be printed |
||
| 200 | * |
||
| 201 | * @param bool $arg |
||
| 202 | * |
||
| 203 | * @return $this |
||
| 204 | */ |
||
| 205 | public function printOutput($arg) { |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Should command metadata be printed. I,e., command and timer. |
||
| 214 | * |
||
| 215 | * @param bool $arg |
||
| 216 | * |
||
| 217 | * @return $this |
||
| 218 | */ |
||
| 219 | public function printMetadata($arg) { |
||
| 225 | |||
| 226 | /** |
||
| 227 | * |
||
| 228 | */ |
||
| 229 | public function __destruct() { |
||
| 232 | |||
| 233 | /** |
||
| 234 | * {@inheritdoc} |
||
| 235 | */ |
||
| 236 | public function run() { |
||
| 246 | |||
| 247 | /** |
||
| 248 | * @param $command |
||
| 249 | * @param null $output_callback |
||
| 250 | * |
||
| 251 | * @return \Robo\Result |
||
| 252 | */ |
||
| 253 | public function execute($command, $output_callback = NULL) { |
||
| 303 | |||
| 304 | /** |
||
| 305 | * |
||
| 306 | */ |
||
| 307 | protected function stop() { |
||
| 314 | |||
| 315 | /** |
||
| 316 | * @param array $context |
||
| 317 | */ |
||
| 318 | protected function printAction($context = []) { |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Gets the data array to be passed to Result(). |
||
| 329 | * |
||
| 330 | * @return array |
||
| 331 | * The data array passed to Result(). |
||
| 332 | */ |
||
| 333 | protected function getResultData() { |
||
| 340 | |||
| 341 | } |
||
| 342 |
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.