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) |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Supoorted By Name |
||
| 194 | * |
||
| 195 | * Is package supoorted by name. All supported package names shoudl start with aframe |
||
| 196 | * |
||
| 197 | * @param string $pretty_name |
||
| 198 | * @return bool |
||
| 199 | */ |
||
| 200 | protected function supportedByName(string $pretty_name) |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Get component dist path |
||
| 210 | * |
||
| 211 | * @return string |
||
| 212 | */ |
||
| 213 | protected function getComponentSrcDistPath(PackageInterface $package) : string |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Get A-Frame component path |
||
| 220 | * |
||
| 221 | * @return string |
||
| 222 | */ |
||
| 223 | public function getComponentPath() |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Set A-Frame componet public path |
||
| 230 | * |
||
| 231 | * @return string |
||
| 232 | */ |
||
| 233 | protected function setComponentPath() |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Get public path |
||
| 240 | * |
||
| 241 | * Path where all package dist files will be saved |
||
| 242 | * |
||
| 243 | * @return string $public_path |
||
| 244 | */ |
||
| 245 | public function getPublicRoot() |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Get A-Frame assets path |
||
| 253 | * |
||
| 254 | * @return string |
||
| 255 | */ |
||
| 256 | public function getPublicAframeCoreDir() |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Get Composer Vendor dir |
||
| 263 | * |
||
| 264 | * @return string |
||
| 265 | */ |
||
| 266 | public function getVendorDir() |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Get A-Frame core assets source path |
||
| 273 | * |
||
| 274 | * @return string |
||
| 275 | */ |
||
| 276 | public function getAframeCoreSrcDir() |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Copy directory or files |
||
| 283 | * |
||
| 284 | * @param string $source |
||
| 285 | * @param string $dest |
||
| 286 | */ |
||
| 287 | public function copy(string $source, string $dest) |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Remove files or directories |
||
| 310 | * |
||
| 311 | * @param string $pathname |
||
| 312 | * @return boolean |
||
| 313 | */ |
||
| 314 | public function rm(string $pathname) |
||
| 334 | |||
| 335 | /** |
||
| 336 | * initializeVendorDir |
||
| 337 | * |
||
| 338 | * @return void |
||
| 339 | */ |
||
| 340 | protected function initializeVendorDir() |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Set A-Frame assets relative base url |
||
| 353 | */ |
||
| 354 | public function updateConfig() |
||
| 368 | } |
||
| 369 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.