Complex classes like Bowerphp 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.
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 Bowerphp, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 33 | class Bowerphp |
||
| 34 | { |
||
| 35 | protected $config; |
||
| 36 | protected $filesystem; |
||
| 37 | protected $githubClient; |
||
| 38 | protected $repository; |
||
| 39 | protected $output; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @param ConfigInterface $config |
||
| 43 | * @param Filesystem $filesystem |
||
| 44 | * @param Client $githubClient |
||
| 45 | * @param RepositoryInterface $repository |
||
| 46 | * @param BowerphpConsoleOutput $output |
||
| 47 | */ |
||
| 48 | public function __construct( |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Init bower.json |
||
| 64 | * |
||
| 65 | * @param array $params |
||
| 66 | */ |
||
| 67 | public function init(array $params) |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Install a single package |
||
| 80 | * |
||
| 81 | * @param PackageInterface $package |
||
| 82 | * @param InstallerInterface $installer |
||
| 83 | * @param bool $isDependency |
||
| 84 | */ |
||
| 85 | public function installPackage(PackageInterface $package, InstallerInterface $installer, $isDependency = false) |
||
| 86 | { |
||
| 87 | if (strpos($package->getName(), 'github') !== false) { |
||
| 88 | // install from a github endpoint |
||
| 89 | $name = basename($package->getName(), '.git'); |
||
| 90 | $repoUrl = $package->getName(); |
||
| 91 | $package = new Package($name, $package->getRequiredVersion()); |
||
| 92 | $this->repository->setUrl($repoUrl)->setHttpClient($this->githubClient); |
||
| 93 | $package->setRepository($this->repository); |
||
| 94 | $packageTag = $this->repository->findPackage($package->getRequiredVersion()); |
||
| 95 | if (is_null($packageTag)) { |
||
| 96 | throw new RuntimeException(sprintf('Cannot find package %s version %s.', $package->getName(), $package->getRequiredVersion())); |
||
| 97 | } |
||
| 98 | } else { |
||
| 99 | $packageTag = $this->getPackageTag($package, true); |
||
| 100 | $package->setRepository($this->repository); |
||
| 101 | } |
||
| 102 | $package->setVersion($packageTag); |
||
| 103 | $this->updateBowerFile($package, $isDependency); |
||
| 104 | |||
| 105 | |||
| 106 | // if package is already installed, match current version with latest available version |
||
| 107 | if ($this->isPackageInstalled($package)) { |
||
| 108 | $packageBower = $this->config->getPackageBowerFileContent($package); |
||
| 109 | if(isset($packageBower['version']) && $packageTag == $packageBower['version']) { |
||
| 110 | // if version is fully matching, there's no need to install |
||
| 111 | return; |
||
| 112 | } |
||
| 113 | } |
||
| 114 | |||
| 115 | $this->output->writelnInfoPackage($package); |
||
| 116 | |||
| 117 | $this->output->writelnInstalledPackage($package); |
||
| 118 | |||
| 119 | $this->cachePackage($package); |
||
| 120 | |||
| 121 | $installer->install($package); |
||
| 122 | |||
| 123 | $overrides = $this->config->getOverrideFor($package->getName()); |
||
| 124 | if (array_key_exists('dependencies', $overrides)) { |
||
| 125 | $dependencies = $overrides['dependencies']; |
||
| 126 | } else { |
||
| 127 | $dependencies = $package->getRequires(); |
||
| 128 | } |
||
| 129 | if (!empty($dependencies)) { |
||
| 130 | foreach ($dependencies as $name => $version) { |
||
| 131 | $depPackage = new Package($name, $version); |
||
| 132 | if (!$this->isPackageInstalled($depPackage)) { |
||
| 133 | $this->installPackage($depPackage, $installer, true); |
||
| 134 | } else { |
||
| 135 | $this->updatePackage($depPackage, $installer, false); |
||
| 136 | } |
||
| 137 | } |
||
| 138 | } |
||
| 139 | } |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Install all dependencies |
||
| 143 | * |
||
| 144 | * @param InstallerInterface $installer |
||
| 145 | */ |
||
| 146 | public function installDependencies(InstallerInterface $installer) |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Update a single package |
||
| 167 | * |
||
| 168 | * @param PackageInterface $package |
||
| 169 | * @param InstallerInterface $installer |
||
| 170 | */ |
||
| 171 | public function updatePackage(PackageInterface $package, InstallerInterface $installer, $force=true) |
||
| 172 | { |
||
| 173 | if (!$this->isPackageInstalled($package)) { |
||
| 174 | throw new RuntimeException(sprintf('Package %s is not installed.', $package->getName())); |
||
| 175 | } |
||
| 176 | |||
| 177 | if(!$force){ |
||
| 178 | $packageTag = $this->getPackageTag($package); |
||
| 179 | $decode = $this->config->getBowerFileContent(); |
||
| 180 | if(isset($decode['dependencies'][$package->getName()])){ |
||
| 181 | $requiredVersion = $decode['dependencies'][$package->getName()]; |
||
| 182 | $semver = new version($packageTag); |
||
| 183 | if($requiredVersion=='latest'||$requiredVersion=='master'){ |
||
| 184 | $requiredVersionExpression = '*'; |
||
| 185 | } |
||
| 186 | else{ |
||
| 187 | $requiredVersionExpression = $requiredVersion; |
||
| 188 | } |
||
| 189 | if(!$semver->satisfies(new expression($requiredVersionExpression))){ |
||
| 190 | $package->setRequiredVersion($requiredVersion); |
||
| 191 | } |
||
| 192 | } |
||
| 193 | } |
||
| 194 | |||
| 195 | if (is_null($package->getRequiredVersion())) { |
||
| 196 | $decode = $this->config->getBowerFileContent(); |
||
| 197 | if (empty($decode['dependencies']) || empty($decode['dependencies'][$package->getName()])) { |
||
| 198 | throw new InvalidArgumentException(sprintf('Package %s not found in bower.json', $package->getName())); |
||
| 199 | } |
||
| 200 | $package->setRequiredVersion($decode['dependencies'][$package->getName()]); |
||
| 201 | } |
||
| 202 | |||
| 203 | $bower = $this->config->getPackageBowerFileContent($package); |
||
| 204 | $package->setInfo($bower); |
||
| 205 | if(isset($bower['version'])){ |
||
| 206 | $package->setVersion($bower['version']); |
||
| 207 | } |
||
| 208 | $package->setRequires(isset($bower['dependencies']) ? $bower['dependencies'] : null); |
||
| 209 | |||
| 210 | $packageTag = $this->getPackageTag($package); |
||
| 211 | $package->setRepository($this->repository); |
||
| 212 | |||
| 213 | if ($packageTag == $package->getVersion()) { |
||
| 214 | // if version is fully matching, there's no need to update |
||
| 215 | return; |
||
| 216 | } |
||
| 217 | $package->setVersion($packageTag); |
||
| 218 | |||
| 219 | $this->output->writelnUpdatingPackage($package); |
||
| 220 | |||
| 221 | $this->cachePackage($package); |
||
| 222 | |||
| 223 | $installer->update($package); |
||
| 224 | |||
| 225 | $overrides = $this->config->getOverrideFor($package->getName()); |
||
| 226 | if (array_key_exists('dependencies', $overrides)) { |
||
| 227 | $dependencies = $overrides['dependencies']; |
||
| 228 | } else { |
||
| 229 | $dependencies = $package->getRequires(); |
||
| 230 | } |
||
| 231 | if (!empty($dependencies)) { |
||
| 232 | foreach ($dependencies as $name => $requiredVersion) { |
||
| 233 | $depPackage = new Package($name, $requiredVersion); |
||
| 234 | if (!$this->isPackageInstalled($depPackage)) { |
||
| 235 | $this->installPackage($depPackage, $installer, true); |
||
| 236 | } else { |
||
| 237 | $this->updatePackage($depPackage, $installer); |
||
| 238 | } |
||
| 239 | } |
||
| 240 | } |
||
| 241 | } |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Update all dependencies |
||
| 245 | * |
||
| 246 | * @param InstallerInterface $installer |
||
| 247 | */ |
||
| 248 | public function updatePackages(InstallerInterface $installer) |
||
| 257 | |||
| 258 | /** |
||
| 259 | * @param PackageInterface $package |
||
| 260 | * @param string $info |
||
| 261 | * @return mixed |
||
| 262 | */ |
||
| 263 | public function getPackageInfo(PackageInterface $package, $info = 'url') |
||
| 286 | |||
| 287 | /** |
||
| 288 | * @param string $name |
||
| 289 | * @return array |
||
| 290 | */ |
||
| 291 | public function lookupPackage($package) |
||
| 295 | |||
| 296 | /** |
||
| 297 | * @param PackageInterface $package |
||
| 298 | * @return string |
||
| 299 | */ |
||
| 300 | public function getPackageBowerFile(PackageInterface $package) |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Uninstall a single package |
||
| 312 | * |
||
| 313 | * @param PackageInterface $package |
||
| 314 | * @param InstallerInterface $installer |
||
| 315 | */ |
||
| 316 | public function uninstallPackage(PackageInterface $package, InstallerInterface $installer) |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Search packages by name |
||
| 326 | * |
||
| 327 | * @param string $name |
||
| 328 | * @return array |
||
| 329 | */ |
||
| 330 | public function searchPackages($name) |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Get a list of installed packages |
||
| 344 | * |
||
| 345 | * @param InstallerInterface $installer |
||
| 346 | * @param Finder $finder |
||
| 347 | * @return array |
||
| 348 | */ |
||
| 349 | public function getInstalledPackages(InstallerInterface $installer, Finder $finder) |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Check if package is installed |
||
| 356 | * |
||
| 357 | * @param PackageInterface $package |
||
| 358 | * @return bool |
||
| 359 | */ |
||
| 360 | public function isPackageInstalled(PackageInterface $package) |
||
| 364 | |||
| 365 | /** |
||
| 366 | * {@inheritdoc} |
||
| 367 | */ |
||
| 368 | public function isPackageExtraneous(PackageInterface $package, $checkInstall = false) |
||
| 407 | |||
| 408 | /** |
||
| 409 | * @param array $params |
||
| 410 | * @return array |
||
| 411 | */ |
||
| 412 | protected function createAClearBowerFile(array $params) |
||
| 427 | |||
| 428 | /** |
||
| 429 | * @param PackageInterface $package |
||
| 430 | * @param bool $setInfo |
||
| 431 | * @return string |
||
| 432 | */ |
||
| 433 | protected function getPackageTag(PackageInterface $package, $setInfo = false) |
||
| 454 | |||
| 455 | /** |
||
| 456 | * @param string $name |
||
| 457 | * @return array |
||
| 458 | */ |
||
| 459 | protected function findPackage($package) |
||
| 479 | |||
| 480 | /** |
||
| 481 | * @param PackageInterface $package |
||
| 482 | */ |
||
| 483 | private function cachePackage(PackageInterface $package) |
||
| 491 | |||
| 492 | /** |
||
| 493 | * @param PackageInterface $package |
||
| 494 | * @param bool $isDependency |
||
| 495 | */ |
||
| 496 | private function updateBowerFile(PackageInterface $package, $isDependency = false) |
||
| 506 | } |
||
| 507 |