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 |
||
| 10 | class Installer |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * The module name. |
||
| 14 | * |
||
| 15 | * @var string |
||
| 16 | */ |
||
| 17 | protected $name; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * The version of module being installed. |
||
| 21 | * |
||
| 22 | * @var string |
||
| 23 | */ |
||
| 24 | protected $version; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * The module repository instance. |
||
| 28 | * |
||
| 29 | * @var \Nwidart\Modules\Repository |
||
| 30 | */ |
||
| 31 | protected $repository; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * The console command instance. |
||
| 35 | * |
||
| 36 | * @var \Illuminate\Console\Command |
||
| 37 | */ |
||
| 38 | protected $console; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * The destionation path. |
||
| 42 | * |
||
| 43 | * @var string |
||
| 44 | */ |
||
| 45 | protected $path; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * The process timeout. |
||
| 49 | * |
||
| 50 | * @var int |
||
| 51 | */ |
||
| 52 | protected $timeout = 3360; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * The constructor. |
||
| 56 | * |
||
| 57 | * @param string $name |
||
| 58 | * @param string $version |
||
| 59 | * @param string $type |
||
| 60 | * @param bool $tree |
||
| 61 | */ |
||
| 62 | public function __construct($name, $version = null, $type = null, $tree = false) |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Set destination path. |
||
| 72 | * |
||
| 73 | * @param string $path |
||
| 74 | * |
||
| 75 | * @return $this |
||
| 76 | */ |
||
| 77 | public function setPath($path) |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Set the module repository instance. |
||
| 86 | * |
||
| 87 | * @param \Nwidart\Modules\Repository $repository |
||
| 88 | * |
||
| 89 | * @return $this |
||
| 90 | */ |
||
| 91 | public function setRepository(Repository $repository) |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Set console command instance. |
||
| 100 | * |
||
| 101 | * @param \Illuminate\Console\Command $console |
||
| 102 | * |
||
| 103 | * @return $this |
||
| 104 | */ |
||
| 105 | public function setConsole(Command $console) |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Set process timeout. |
||
| 114 | * |
||
| 115 | * @param int $timeout |
||
| 116 | * |
||
| 117 | * @return $this |
||
| 118 | */ |
||
| 119 | public function setTimeout($timeout) |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Run the installation process. |
||
| 128 | * |
||
| 129 | * @return \Symfony\Component\Process\Process |
||
| 130 | */ |
||
| 131 | public function run() |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Get process instance. |
||
| 148 | * |
||
| 149 | * @return \Symfony\Component\Process\Process |
||
| 150 | */ |
||
| 151 | public function getProcess() |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Get destination path. |
||
| 166 | * |
||
| 167 | * @return string |
||
| 168 | */ |
||
| 169 | public function getDestinationPath() |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Get git repo url. |
||
| 180 | * |
||
| 181 | * @return string|null |
||
| 182 | */ |
||
| 183 | public function getRepoUrl() |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Get branch name. |
||
| 218 | * |
||
| 219 | * @return string |
||
| 220 | */ |
||
| 221 | public function getBranch() |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Get module name. |
||
| 228 | * |
||
| 229 | * @return string |
||
| 230 | */ |
||
| 231 | public function getModuleName() |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Get composer package name. |
||
| 240 | * |
||
| 241 | * @return string |
||
| 242 | */ |
||
| 243 | public function getPackageName() |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Install the module via git. |
||
| 254 | * |
||
| 255 | * @return \Symfony\Component\Process\Process |
||
| 256 | */ |
||
| 257 | View Code Duplication | public function installViaGit() |
|
| 268 | |||
| 269 | /** |
||
| 270 | * Install the module via git subtree. |
||
| 271 | * |
||
| 272 | * @return \Symfony\Component\Process\Process |
||
| 273 | */ |
||
| 274 | View Code Duplication | public function installViaSubtree() |
|
| 286 | |||
| 287 | /** |
||
| 288 | * Install the module via composer. |
||
| 289 | * |
||
| 290 | * @return \Symfony\Component\Process\Process |
||
| 291 | */ |
||
| 292 | public function installViaComposer() |
||
| 300 | } |
||
| 301 |
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: