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 | /** @var string */ |
||
| 66 | protected $command; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @return string |
||
| 70 | */ |
||
| 71 | public function getCommand() |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Sets $this->interactive() based on posix_isatty(). |
||
| 78 | */ |
||
| 79 | public function detectInteractive() |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Executes command in background mode (asynchronously) |
||
| 88 | * |
||
| 89 | * @return $this |
||
| 90 | */ |
||
| 91 | public function background($arg = true) |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Stop command if it runs longer then $timeout in seconds |
||
| 99 | * |
||
| 100 | * @param int $timeout |
||
| 101 | * |
||
| 102 | * @return $this |
||
| 103 | */ |
||
| 104 | public function timeout($timeout) |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Stops command if it does not output something for a while |
||
| 112 | * |
||
| 113 | * @param int $timeout |
||
| 114 | * |
||
| 115 | * @return $this |
||
| 116 | */ |
||
| 117 | public function idleTimeout($timeout) |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Sets the environment variables for the command |
||
| 125 | * |
||
| 126 | * @param array $env |
||
| 127 | * |
||
| 128 | * @return $this |
||
| 129 | */ |
||
| 130 | public function env(array $env) |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Pass an input to the process. Can be resource created with fopen() or string |
||
| 138 | * |
||
| 139 | * @param resource|string $input |
||
| 140 | * |
||
| 141 | * @return $this |
||
| 142 | */ |
||
| 143 | public function setInput($input) |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Attach tty to process for interactive input |
||
| 151 | * |
||
| 152 | * @param $interactive bool |
||
| 153 | * |
||
| 154 | * @return $this |
||
| 155 | */ |
||
| 156 | public function interactive($interactive) |
||
| 161 | |||
| 162 | |||
| 163 | /** |
||
| 164 | * Is command printing its output to screen |
||
| 165 | * |
||
| 166 | * @return bool |
||
| 167 | */ |
||
| 168 | public function getPrinted() |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Changes working directory of command |
||
| 175 | * |
||
| 176 | * @param string $dir |
||
| 177 | * |
||
| 178 | * @return $this |
||
| 179 | */ |
||
| 180 | public function dir($dir) |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Shortcut for setting isPrinted() and isMetadataPrinted() to false. |
||
| 188 | * |
||
| 189 | * @param bool $arg |
||
| 190 | * |
||
| 191 | * @return $this |
||
| 192 | */ |
||
| 193 | public function silent($arg) |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Should command output be printed |
||
| 204 | * |
||
| 205 | * @param bool $arg |
||
| 206 | * |
||
| 207 | * @return $this |
||
| 208 | * |
||
| 209 | * @deprecated |
||
| 210 | */ |
||
| 211 | public function printed($arg) |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Should command output be printed |
||
| 219 | * |
||
| 220 | * @param bool $arg |
||
| 221 | * |
||
| 222 | * @return $this |
||
| 223 | */ |
||
| 224 | public function printOutput($arg) |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Should command metadata be printed. I,e., command and timer. |
||
| 234 | * |
||
| 235 | * @param bool $arg |
||
| 236 | * |
||
| 237 | * @return $this |
||
| 238 | */ |
||
| 239 | public function printMetadata($arg) |
||
| 246 | |||
| 247 | /** |
||
| 248 | * |
||
| 249 | */ |
||
| 250 | public function __destruct() |
||
| 256 | |||
| 257 | /** |
||
| 258 | * {@inheritdoc} |
||
| 259 | */ |
||
| 260 | public function run() |
||
| 264 | |||
| 265 | /** |
||
| 266 | * @param string $command |
||
| 267 | * @param callable $output_callback |
||
| 268 | * |
||
| 269 | * @return \Robo\Result |
||
| 270 | */ |
||
| 271 | protected function execute($command, $output_callback = null) |
||
| 330 | |||
| 331 | /** |
||
| 332 | * |
||
| 333 | */ |
||
| 334 | protected function stop() |
||
| 344 | |||
| 345 | /** |
||
| 346 | * @param array $context |
||
| 347 | */ |
||
| 348 | protected function printAction($context = []) |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Gets the data array to be passed to Result(). |
||
| 360 | * |
||
| 361 | * @return array |
||
| 362 | * The data array passed to Result(). |
||
| 363 | */ |
||
| 364 | protected function getResultData() |
||
| 372 | } |
||
| 373 |
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: