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 = 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 | * @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 | public function detectInteractive() |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Executes command in background mode (asynchronously) |
||
| 103 | * |
||
| 104 | * @return $this |
||
| 105 | */ |
||
| 106 | public function background($arg = true) |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Stop command if it runs longer then $timeout in seconds |
||
| 114 | * |
||
| 115 | * @param int $timeout |
||
| 116 | * |
||
| 117 | * @return $this |
||
| 118 | */ |
||
| 119 | public function timeout($timeout) |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Stops command if it does not output something for a while |
||
| 127 | * |
||
| 128 | * @param int $timeout |
||
| 129 | * |
||
| 130 | * @return $this |
||
| 131 | */ |
||
| 132 | public function idleTimeout($timeout) |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Set a single environment variable, or multiple. |
||
| 140 | */ |
||
| 141 | public function env($env, $value = null) |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Sets the environment variables for the command |
||
| 151 | * |
||
| 152 | * @param array $env |
||
| 153 | * |
||
| 154 | * @return $this |
||
| 155 | */ |
||
| 156 | public function envVars(array $env) |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Pass an input to the process. Can be resource created with fopen() or string |
||
| 164 | * |
||
| 165 | * @param resource|string $input |
||
| 166 | * |
||
| 167 | * @return $this |
||
| 168 | */ |
||
| 169 | public function setInput($input) |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Attach tty to process for interactive input |
||
| 177 | * |
||
| 178 | * @param $interactive bool |
||
| 179 | * |
||
| 180 | * @return $this |
||
| 181 | */ |
||
| 182 | public function interactive($interactive = true) |
||
| 187 | |||
| 188 | |||
| 189 | /** |
||
| 190 | * Is command printing its output to screen |
||
| 191 | * |
||
| 192 | * @return bool |
||
| 193 | */ |
||
| 194 | public function getPrinted() |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Changes working directory of command |
||
| 201 | * |
||
| 202 | * @param string $dir |
||
| 203 | * |
||
| 204 | * @return $this |
||
| 205 | */ |
||
| 206 | public function dir($dir) |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Shortcut for setting isPrinted() and isMetadataPrinted() to false. |
||
| 214 | * |
||
| 215 | * @param bool $arg |
||
| 216 | * |
||
| 217 | * @return $this |
||
| 218 | */ |
||
| 219 | public function silent($arg) |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Should command output be printed |
||
| 230 | * |
||
| 231 | * @param bool $arg |
||
| 232 | * |
||
| 233 | * @return $this |
||
| 234 | * |
||
| 235 | * @deprecated |
||
| 236 | */ |
||
| 237 | public function printed($arg) |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Should command output be printed |
||
| 245 | * |
||
| 246 | * @param bool $arg |
||
| 247 | * |
||
| 248 | * @return $this |
||
| 249 | */ |
||
| 250 | public function printOutput($arg) |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Should command metadata be printed. I,e., command and timer. |
||
| 260 | * |
||
| 261 | * @param bool $arg |
||
| 262 | * |
||
| 263 | * @return $this |
||
| 264 | */ |
||
| 265 | public function printMetadata($arg) |
||
| 272 | |||
| 273 | /** |
||
| 274 | * @param Process $process |
||
| 275 | * @param callable $output_callback |
||
| 276 | * |
||
| 277 | * @return \Robo\ResultData |
||
| 278 | */ |
||
| 279 | protected function execute($process, $output_callback = null) |
||
| 345 | |||
| 346 | /** |
||
| 347 | * |
||
| 348 | */ |
||
| 349 | protected function stop() |
||
| 359 | |||
| 360 | /** |
||
| 361 | * @param array $context |
||
| 362 | */ |
||
| 363 | protected function printAction($context = []) |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Gets the data array to be passed to Result(). |
||
| 375 | * |
||
| 376 | * @return array |
||
| 377 | * The data array passed to Result(). |
||
| 378 | */ |
||
| 379 | protected function getResultData() |
||
| 387 | } |
||
| 388 |
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: