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 | * @return string |
||
| 67 | */ |
||
| 68 | public function getCommand() |
||
| 69 | { |
||
| 70 | return $this->command; |
||
|
|
|||
| 71 | } |
||
| 72 | /** |
||
| 73 | * Sets $this->interactive() based on posix_isatty(). |
||
| 74 | */ |
||
| 75 | public function detectInteractive() |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Executes command in background mode (asynchronously) |
||
| 84 | * |
||
| 85 | * @return $this |
||
| 86 | */ |
||
| 87 | public function background($arg = true) |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Stop command if it runs longer then $timeout in seconds |
||
| 95 | * |
||
| 96 | * @param int $timeout |
||
| 97 | * |
||
| 98 | * @return $this |
||
| 99 | */ |
||
| 100 | public function timeout($timeout) |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Stops command if it does not output something for a while |
||
| 108 | * |
||
| 109 | * @param int $timeout |
||
| 110 | * |
||
| 111 | * @return $this |
||
| 112 | */ |
||
| 113 | public function idleTimeout($timeout) |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Sets the environment variables for the command |
||
| 121 | * |
||
| 122 | * @param array $env |
||
| 123 | * |
||
| 124 | * @return $this |
||
| 125 | */ |
||
| 126 | public function env(array $env) |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Pass an input to the process. Can be resource created with fopen() or string |
||
| 134 | * |
||
| 135 | * @param resource|string $input |
||
| 136 | * |
||
| 137 | * @return $this |
||
| 138 | */ |
||
| 139 | public function setInput($input) |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Attach tty to process for interactive input |
||
| 147 | * |
||
| 148 | * @param $interactive bool |
||
| 149 | * |
||
| 150 | * @return $this |
||
| 151 | */ |
||
| 152 | public function interactive($interactive) |
||
| 157 | |||
| 158 | |||
| 159 | /** |
||
| 160 | * Is command printing its output to screen |
||
| 161 | * |
||
| 162 | * @return bool |
||
| 163 | */ |
||
| 164 | public function getPrinted() |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Changes working directory of command |
||
| 171 | * |
||
| 172 | * @param string $dir |
||
| 173 | * |
||
| 174 | * @return $this |
||
| 175 | */ |
||
| 176 | public function dir($dir) |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Shortcut for setting isPrinted() and isMetadataPrinted() to false. |
||
| 184 | * |
||
| 185 | * @param bool $arg |
||
| 186 | * |
||
| 187 | * @return $this |
||
| 188 | */ |
||
| 189 | public function silent($arg) |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Should command output be printed |
||
| 200 | * |
||
| 201 | * @param bool $arg |
||
| 202 | * |
||
| 203 | * @return $this |
||
| 204 | * |
||
| 205 | * @deprecated |
||
| 206 | */ |
||
| 207 | public function printed($arg) |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Should command output be printed |
||
| 215 | * |
||
| 216 | * @param bool $arg |
||
| 217 | * |
||
| 218 | * @return $this |
||
| 219 | */ |
||
| 220 | public function printOutput($arg) |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Should command metadata be printed. I,e., command and timer. |
||
| 230 | * |
||
| 231 | * @param bool $arg |
||
| 232 | * |
||
| 233 | * @return $this |
||
| 234 | */ |
||
| 235 | public function printMetadata($arg) |
||
| 242 | |||
| 243 | /** |
||
| 244 | * |
||
| 245 | */ |
||
| 246 | public function __destruct() |
||
| 252 | |||
| 253 | /** |
||
| 254 | * {@inheritdoc} |
||
| 255 | */ |
||
| 256 | public function run() |
||
| 260 | |||
| 261 | /** |
||
| 262 | * @param string $command |
||
| 263 | * @param callable $output_callback |
||
| 264 | * |
||
| 265 | * @return \Robo\Result |
||
| 266 | */ |
||
| 267 | protected function execute($command, $output_callback = null) |
||
| 322 | |||
| 323 | /** |
||
| 324 | * |
||
| 325 | */ |
||
| 326 | protected function stop() |
||
| 336 | |||
| 337 | /** |
||
| 338 | * @param array $context |
||
| 339 | */ |
||
| 340 | protected function printAction($context = []) |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Gets the data array to be passed to Result(). |
||
| 352 | * |
||
| 353 | * @return array |
||
| 354 | * The data array passed to Result(). |
||
| 355 | */ |
||
| 356 | protected function getResultData() |
||
| 364 | } |
||
| 365 |
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: