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:
Complex classes like ExecTrait often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ExecTrait, and based on these observations, apply Extract Interface, too.
| 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 = null; |
||
| 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 public function getCommandDescription(); |
||
| 68 | |||
| 69 | /** Typically provided by Timer trait via ProgressIndicatorAwareTrait. */ |
||
| 70 | abstract public function startTimer(); |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Typically provided by TaskIO Trait. |
||
| 76 | */ |
||
| 77 | abstract public function hideTaskProgress(); |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Typically provided by VerbosityThresholdTrait. |
||
| 83 | */ |
||
| 84 | abstract public function verbosityMeetsThreshold(); |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Sets $this->interactive() based on posix_isatty(). |
||
| 89 | * |
||
| 90 | * @return $this |
||
| 91 | */ |
||
| 92 | public function detectInteractive() |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Executes command in background mode (asynchronously) |
||
| 107 | * |
||
| 108 | * @return $this |
||
| 109 | */ |
||
| 110 | public function background($arg = true) |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Stop command if it runs longer then $timeout in seconds |
||
| 118 | * |
||
| 119 | * @param int $timeout |
||
| 120 | * |
||
| 121 | * @return $this |
||
| 122 | */ |
||
| 123 | public function timeout($timeout) |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Stops command if it does not output something for a while |
||
| 131 | * |
||
| 132 | * @param int $timeout |
||
| 133 | * |
||
| 134 | * @return $this |
||
| 135 | */ |
||
| 136 | public function idleTimeout($timeout) |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Set a single environment variable, or multiple. |
||
| 144 | */ |
||
| 145 | public function env($env, $value = null) |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Sets the environment variables for the command |
||
| 155 | * |
||
| 156 | * @param array $env |
||
| 157 | * |
||
| 158 | * @return $this |
||
| 159 | */ |
||
| 160 | public function envVars(array $env) |
||
| 161 | { |
||
| 162 | $this->env = $this->env ? $env + $this->env : $env; |
||
| 163 | return $this; |
||
| 164 | } |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Pass an input to the process. Can be resource created with fopen() or string |
||
| 168 | * |
||
| 169 | * @param resource|string $input |
||
| 170 | * |
||
| 171 | * @return $this |
||
| 172 | */ |
||
| 173 | public function setInput($input) |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Attach tty to process for interactive input |
||
| 181 | * |
||
| 182 | * @param $interactive bool |
||
| 183 | * |
||
| 184 | * @return $this |
||
| 185 | */ |
||
| 186 | public function interactive($interactive = true) |
||
| 191 | |||
| 192 | |||
| 193 | /** |
||
| 194 | * Is command printing its output to screen |
||
| 195 | * |
||
| 196 | * @return bool |
||
| 197 | */ |
||
| 198 | public function getPrinted() |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Changes working directory of command |
||
| 205 | * |
||
| 206 | * @param string $dir |
||
| 207 | * |
||
| 208 | * @return $this |
||
| 209 | */ |
||
| 210 | public function dir($dir) |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Shortcut for setting isPrinted() and isMetadataPrinted() to false. |
||
| 218 | * |
||
| 219 | * @param bool $arg |
||
| 220 | * |
||
| 221 | * @return $this |
||
| 222 | */ |
||
| 223 | public function silent($arg) |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Should command output be printed |
||
| 234 | * |
||
| 235 | * @param bool $arg |
||
| 236 | * |
||
| 237 | * @return $this |
||
| 238 | * |
||
| 239 | * @deprecated |
||
| 240 | */ |
||
| 241 | public function printed($arg) |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Should command output be printed |
||
| 249 | * |
||
| 250 | * @param bool $arg |
||
| 251 | * |
||
| 252 | * @return $this |
||
| 253 | */ |
||
| 254 | public function printOutput($arg) |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Should command metadata be printed. I,e., command and timer. |
||
| 264 | * |
||
| 265 | * @param bool $arg |
||
| 266 | * |
||
| 267 | * @return $this |
||
| 268 | */ |
||
| 269 | public function printMetadata($arg) |
||
| 276 | |||
| 277 | /** |
||
| 278 | * @param Process $process |
||
| 279 | * @param callable $output_callback |
||
| 280 | * |
||
| 281 | * @return \Robo\ResultData |
||
| 282 | */ |
||
| 283 | protected function execute($process, $output_callback = null) |
||
| 356 | |||
| 357 | /** |
||
| 358 | * |
||
| 359 | */ |
||
| 360 | protected function stop() |
||
| 370 | |||
| 371 | /** |
||
| 372 | * @param array $context |
||
| 373 | */ |
||
| 374 | protected function printAction($context = []) |
||
| 385 | |||
| 386 | /** |
||
| 387 | * @param $command |
||
| 388 | * |
||
| 389 | * @return mixed |
||
| 390 | */ |
||
| 391 | protected function formatCommandDisplay($command) |
||
| 398 | |||
| 399 | /** |
||
| 400 | * Gets the data array to be passed to Result(). |
||
| 401 | * |
||
| 402 | * @return array |
||
| 403 | * The data array passed to Result(). |
||
| 404 | */ |
||
| 405 | protected function getResultData() |
||
| 413 | } |
||
| 414 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: