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 |
||
| 30 | class AframeComponentInstaller extends LibraryInstaller |
||
| 31 | { |
||
| 32 | |||
| 33 | const AFRAME_PHP_VERSION = '0.3.0.1'; |
||
| 34 | |||
| 35 | const SUPPORTED_TYPES = array( |
||
| 36 | 'component', |
||
| 37 | 'aframe', |
||
| 38 | 'aframe-component' |
||
| 39 | ); |
||
| 40 | |||
| 41 | /** |
||
| 42 | * A-Frame Component vendor |
||
| 43 | * |
||
| 44 | * @var string |
||
| 45 | */ |
||
| 46 | protected $aframe_component_vendor; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * A-Frame Component package name |
||
| 50 | * |
||
| 51 | * @var string |
||
| 52 | */ |
||
| 53 | protected $aframe_component_name; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Path to directory of A-Frame assets |
||
| 57 | * |
||
| 58 | * @var string |
||
| 59 | */ |
||
| 60 | protected $public_root; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Path to A-Frame core scripts |
||
| 64 | * |
||
| 65 | * @var string |
||
| 66 | */ |
||
| 67 | protected $public_core_dir; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * URI of A-Frame assets relative to your document root |
||
| 71 | * |
||
| 72 | * @var string |
||
| 73 | */ |
||
| 74 | protected $aframe_assets_url = '/aframe'; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Decides if the installer supports the given type |
||
| 78 | * and package. |
||
| 79 | * We only handle components with name prefix aframe |
||
| 80 | * |
||
| 81 | * @param string $package_type |
||
| 82 | * @return bool |
||
| 83 | */ |
||
| 84 | public function supports($package_type) |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Checks that provided package is installed. |
||
| 91 | * |
||
| 92 | * @param InstalledRepositoryInterface $repo |
||
| 93 | * repository in which to check |
||
| 94 | * @param PackageInterface $package |
||
| 95 | * package instance |
||
| 96 | * |
||
| 97 | * @return bool |
||
| 98 | */ |
||
| 99 | public function isInstalled(InstalledRepositoryInterface $repo, PackageInterface $package) |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Installs specific package. |
||
| 106 | * |
||
| 107 | * @param InstalledRepositoryInterface $repo |
||
| 108 | * repository in which to check |
||
| 109 | * @param PackageInterface $package |
||
| 110 | * package instance |
||
| 111 | * |
||
| 112 | * @return void |
||
| 113 | */ |
||
| 114 | public function install(InstalledRepositoryInterface $repo, PackageInterface $package) |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Updates specific package. |
||
| 137 | * |
||
| 138 | * @param InstalledRepositoryInterface $repo |
||
| 139 | * repository in which to check |
||
| 140 | * @param PackageInterface $initial |
||
| 141 | * already installed package version |
||
| 142 | * @param PackageInterface $target |
||
| 143 | * updated version |
||
| 144 | * |
||
| 145 | * @throws InvalidArgumentException if $initial package is not installed |
||
| 146 | * @return void |
||
| 147 | */ |
||
| 148 | public function update(InstalledRepositoryInterface $repo, PackageInterface $initial, PackageInterface $target) |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Remove the code |
||
| 170 | * |
||
| 171 | * @param PackageInterface $package |
||
| 172 | * @return void |
||
| 173 | */ |
||
| 174 | public function removeCode(PackageInterface $package) |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Supoorted By Name |
||
| 191 | * |
||
| 192 | * Is package supoorted by name. All supported package names shoudl start with aframe |
||
| 193 | * |
||
| 194 | * @param string $pretty_name |
||
| 195 | * @return bool |
||
| 196 | */ |
||
| 197 | protected function supportedByName(string $pretty_name) |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Get component dist path |
||
| 207 | * |
||
| 208 | * @return string |
||
| 209 | */ |
||
| 210 | protected function getComponentSrcDistPath(PackageInterface $package): string |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Get A-Frame component path |
||
| 217 | * |
||
| 218 | * @return string |
||
| 219 | */ |
||
| 220 | public function getComponentPath() |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Set A-Frame componet public path |
||
| 227 | * |
||
| 228 | * @return string |
||
| 229 | */ |
||
| 230 | protected function setComponentPath() |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Get public path |
||
| 237 | * |
||
| 238 | * Path where all package dist files will be saved |
||
| 239 | * |
||
| 240 | * @return string $public_path |
||
| 241 | */ |
||
| 242 | public function getPublicRoot() |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Get A-Frame assets path |
||
| 250 | * |
||
| 251 | * @return string |
||
| 252 | */ |
||
| 253 | public function getPublicAframeCoreDir() |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Get Composer Vendor dir |
||
| 260 | * |
||
| 261 | * @return string |
||
| 262 | */ |
||
| 263 | public function getVendorDir() |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Get A-Frame core assets source path |
||
| 270 | * |
||
| 271 | * @return string |
||
| 272 | */ |
||
| 273 | public function getAframeCoreSrcDir() |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Copy directory or files |
||
| 280 | * |
||
| 281 | * @param string $source |
||
| 282 | * @param string $dest |
||
| 283 | * @return bool |
||
| 284 | */ |
||
| 285 | public function copy(string $source, string $dest) |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Copy directory |
||
| 292 | * |
||
| 293 | * @param string $source |
||
| 294 | * @param string $dest |
||
| 295 | */ |
||
| 296 | private function copyDir(string $source, string $dest) |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Remove files or directories |
||
| 312 | * |
||
| 313 | * @param string $pathname |
||
| 314 | * @return boolean |
||
| 315 | */ |
||
| 316 | public function rm(string $pathname) |
||
| 336 | |||
| 337 | /** |
||
| 338 | * initializeVendorDir |
||
| 339 | * |
||
| 340 | * @return void |
||
| 341 | */ |
||
| 342 | protected function initializeVendorDir() |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Set A-Frame assets relative base url |
||
| 355 | */ |
||
| 356 | public function updateConfig() |
||
| 370 | } |
||
| 371 |