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:
| 1 | <?php |
||
| 14 | trait ExecTrait |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * @var bool |
||
| 18 | */ |
||
| 19 | protected $background = false; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @var null|int |
||
| 23 | */ |
||
| 24 | protected $timeout = null; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var null|int |
||
| 28 | */ |
||
| 29 | protected $idleTimeout = null; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var null|array |
||
| 33 | */ |
||
| 34 | protected $env = null; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var Process |
||
| 38 | */ |
||
| 39 | protected $process; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var resource|string |
||
| 43 | */ |
||
| 44 | protected $input; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var boolean |
||
| 48 | */ |
||
| 49 | protected $interactive = false; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var bool |
||
| 53 | */ |
||
| 54 | protected $isPrinted = true; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var bool |
||
| 58 | */ |
||
| 59 | protected $isMetadataPrinted = true; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var string |
||
| 63 | */ |
||
| 64 | protected $workingDirectory; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Sets $this->interactive() based on posix_isatty(). |
||
| 68 | */ |
||
| 69 | public function detectInteractive() |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Executes command in background mode (asynchronously) |
||
| 78 | * |
||
| 79 | * @return $this |
||
| 80 | */ |
||
| 81 | public function background($arg = true) |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Stop command if it runs longer then $timeout in seconds |
||
| 89 | * |
||
| 90 | * @param int $timeout |
||
| 91 | * |
||
| 92 | * @return $this |
||
| 93 | */ |
||
| 94 | public function timeout($timeout) |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Stops command if it does not output something for a while |
||
| 102 | * |
||
| 103 | * @param int $timeout |
||
| 104 | * |
||
| 105 | * @return $this |
||
| 106 | */ |
||
| 107 | public function idleTimeout($timeout) |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Sets the environment variables for the command |
||
| 115 | * |
||
| 116 | * @param array $env |
||
| 117 | * |
||
| 118 | * @return $this |
||
| 119 | */ |
||
| 120 | public function env(array $env) |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Pass an input to the process. Can be resource created with fopen() or string |
||
| 128 | * |
||
| 129 | * @param resource|string $input |
||
| 130 | * |
||
| 131 | * @return $this |
||
| 132 | */ |
||
| 133 | public function setInput($input) |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Attach tty to process for interactive input |
||
| 141 | * |
||
| 142 | * @param $interactive bool |
||
| 143 | * |
||
| 144 | * @return $this |
||
| 145 | */ |
||
| 146 | public function interactive($interactive) |
||
| 151 | |||
| 152 | |||
| 153 | /** |
||
| 154 | * Is command printing its output to screen |
||
| 155 | * |
||
| 156 | * @return bool |
||
| 157 | */ |
||
| 158 | public function getPrinted() |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Changes working directory of command |
||
| 165 | * |
||
| 166 | * @param string $dir |
||
| 167 | * |
||
| 168 | * @return $this |
||
| 169 | */ |
||
| 170 | public function dir($dir) |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Shortcut for setting isPrinted() and isMetadataPrinted() to false. |
||
| 178 | * |
||
| 179 | * @param bool $arg |
||
| 180 | * |
||
| 181 | * @return $this |
||
| 182 | */ |
||
| 183 | public function silent($arg) |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Should command output be printed |
||
| 194 | * |
||
| 195 | * @param bool $arg |
||
| 196 | * |
||
| 197 | * @return $this |
||
| 198 | * |
||
| 199 | * @deprecated |
||
| 200 | */ |
||
| 201 | public function printed($arg) |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Should command output be printed |
||
| 209 | * |
||
| 210 | * @param bool $arg |
||
| 211 | * |
||
| 212 | * @return $this |
||
| 213 | */ |
||
| 214 | public function printOutput($arg) |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Should command metadata be printed. I,e., command and timer. |
||
| 224 | * |
||
| 225 | * @param bool $arg |
||
| 226 | * |
||
| 227 | * @return $this |
||
| 228 | */ |
||
| 229 | public function printMetadata($arg) |
||
| 236 | |||
| 237 | /** |
||
| 238 | * @param Process $process |
||
| 239 | * @param callable $output_callback |
||
| 240 | * |
||
| 241 | * @return \Robo\ResultData |
||
| 242 | */ |
||
| 243 | protected function execute($process, $output_callback = null) |
||
| 305 | |||
| 306 | /** |
||
| 307 | * |
||
| 308 | */ |
||
| 309 | protected function stop() |
||
| 319 | |||
| 320 | /** |
||
| 321 | * @param array $context |
||
| 322 | */ |
||
| 323 | protected function printAction($context = []) |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Gets the data array to be passed to Result(). |
||
| 335 | * |
||
| 336 | * @return array |
||
| 337 | * The data array passed to Result(). |
||
| 338 | */ |
||
| 339 | protected function getResultData() |
||
| 347 | } |
||
| 348 |
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: