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 |
||
| 13 | trait ExecTrait |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * @var bool |
||
| 17 | */ |
||
| 18 | protected $background = false; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @var null|int |
||
| 22 | */ |
||
| 23 | protected $timeout = null; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var null|int |
||
| 27 | */ |
||
| 28 | protected $idleTimeout = null; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var null|array |
||
| 32 | */ |
||
| 33 | protected $env = null; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var Process |
||
| 37 | */ |
||
| 38 | protected $process; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var resource|string |
||
| 42 | */ |
||
| 43 | protected $input; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var boolean |
||
| 47 | */ |
||
| 48 | protected $interactive = false; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var bool |
||
| 52 | */ |
||
| 53 | protected $isPrinted = true; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var bool |
||
| 57 | */ |
||
| 58 | protected $isMetadataPrinted = true; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var string |
||
| 62 | */ |
||
| 63 | protected $workingDirectory; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var string |
||
| 67 | */ |
||
| 68 | protected $commandString; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @return string |
||
| 72 | */ |
||
| 73 | public function getCommandString() |
||
| 77 | /** |
||
| 78 | * Sets $this->interactive() based on posix_isatty(). |
||
| 79 | */ |
||
| 80 | public function detectInteractive() |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Executes command in background mode (asynchronously) |
||
| 89 | * |
||
| 90 | * @return $this |
||
| 91 | */ |
||
| 92 | public function background($arg = true) |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Stop command if it runs longer then $timeout in seconds |
||
| 100 | * |
||
| 101 | * @param int $timeout |
||
| 102 | * |
||
| 103 | * @return $this |
||
| 104 | */ |
||
| 105 | public function timeout($timeout) |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Stops command if it does not output something for a while |
||
| 113 | * |
||
| 114 | * @param int $timeout |
||
| 115 | * |
||
| 116 | * @return $this |
||
| 117 | */ |
||
| 118 | public function idleTimeout($timeout) |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Sets the environment variables for the command |
||
| 126 | * |
||
| 127 | * @param array $env |
||
| 128 | * |
||
| 129 | * @return $this |
||
| 130 | */ |
||
| 131 | public function env(array $env) |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Pass an input to the process. Can be resource created with fopen() or string |
||
| 139 | * |
||
| 140 | * @param resource|string $input |
||
| 141 | * |
||
| 142 | * @return $this |
||
| 143 | */ |
||
| 144 | public function setInput($input) |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Attach tty to process for interactive input |
||
| 152 | * |
||
| 153 | * @param $interactive bool |
||
| 154 | * |
||
| 155 | * @return $this |
||
| 156 | */ |
||
| 157 | public function interactive($interactive) |
||
| 162 | |||
| 163 | |||
| 164 | /** |
||
| 165 | * Is command printing its output to screen |
||
| 166 | * |
||
| 167 | * @return bool |
||
| 168 | */ |
||
| 169 | public function getPrinted() |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Changes working directory of command |
||
| 176 | * |
||
| 177 | * @param string $dir |
||
| 178 | * |
||
| 179 | * @return $this |
||
| 180 | */ |
||
| 181 | public function dir($dir) |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Shortcut for setting isPrinted() and isMetadataPrinted() to false. |
||
| 189 | * |
||
| 190 | * @param bool $arg |
||
| 191 | * |
||
| 192 | * @return $this |
||
| 193 | */ |
||
| 194 | public function silent($arg) |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Should command output be printed |
||
| 205 | * |
||
| 206 | * @param bool $arg |
||
| 207 | * |
||
| 208 | * @return $this |
||
| 209 | * |
||
| 210 | * @deprecated |
||
| 211 | */ |
||
| 212 | public function printed($arg) |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Should command output be printed |
||
| 220 | * |
||
| 221 | * @param bool $arg |
||
| 222 | * |
||
| 223 | * @return $this |
||
| 224 | */ |
||
| 225 | public function printOutput($arg) |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Should command metadata be printed. I,e., command and timer. |
||
| 235 | * |
||
| 236 | * @param bool $arg |
||
| 237 | * |
||
| 238 | * @return $this |
||
| 239 | */ |
||
| 240 | public function printMetadata($arg) |
||
| 247 | |||
| 248 | /** |
||
| 249 | * |
||
| 250 | */ |
||
| 251 | public function __destruct() |
||
| 257 | |||
| 258 | /** |
||
| 259 | * {@inheritdoc} |
||
| 260 | */ |
||
| 261 | public function run() |
||
| 265 | |||
| 266 | /** |
||
| 267 | * @param string $command_string |
||
| 268 | * @param callable $output_callback |
||
| 269 | * |
||
| 270 | * @return \Robo\Result |
||
| 271 | */ |
||
| 272 | protected function execute($command_string, $output_callback = null) |
||
| 331 | |||
| 332 | /** |
||
| 333 | * |
||
| 334 | */ |
||
| 335 | protected function stop() |
||
| 345 | |||
| 346 | /** |
||
| 347 | * @param array $context |
||
| 348 | */ |
||
| 349 | protected function printAction($context = []) |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Gets the data array to be passed to Result(). |
||
| 361 | * |
||
| 362 | * @return array |
||
| 363 | * The data array passed to Result(). |
||
| 364 | */ |
||
| 365 | protected function getResultData() |
||
| 373 | } |
||
| 374 |
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: