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 |
||
| 30 | class Bowerphp |
||
| 31 | { |
||
| 32 | protected $config; |
||
| 33 | protected $filesystem; |
||
| 34 | protected $githubClient; |
||
| 35 | protected $repository; |
||
| 36 | protected $output; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @param ConfigInterface $config |
||
| 40 | * @param Filesystem $filesystem |
||
| 41 | * @param Client $githubClient |
||
| 42 | * @param RepositoryInterface $repository |
||
| 43 | * @param BowerphpConsoleOutput $output |
||
| 44 | */ |
||
| 45 | public function __construct( |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Init bower.json |
||
| 61 | * |
||
| 62 | * @param array $params |
||
| 63 | */ |
||
| 64 | public function init(array $params) |
||
| 65 | { |
||
| 66 | if ($this->config->bowerFileExists()) { |
||
| 67 | $bowerJson = $this->config->getBowerFileContent(); |
||
| 68 | $this->config->setSaveToBowerJsonFile(true); |
||
| 69 | $this->config->updateBowerJsonFile2($bowerJson, $params); |
||
| 70 | } else { |
||
| 71 | $this->config->initBowerJsonFile($params); |
||
| 72 | } |
||
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Install a single package |
||
| 77 | * |
||
| 78 | * @param PackageInterface $package |
||
| 79 | * @param InstallerInterface $installer |
||
| 80 | * @param bool $isDependency |
||
| 81 | */ |
||
| 82 | public function installPackage(PackageInterface $package, InstallerInterface $installer, $isDependency = false) |
||
| 83 | { |
||
| 84 | if (strpos($package->getName(), 'github') !== false) { |
||
| 85 | // install from a github endpoint |
||
| 86 | $name = basename($package->getName(), '.git'); |
||
| 87 | $repoUrl = $package->getName(); |
||
| 88 | $package = new Package($name, $package->getRequiredVersion()); |
||
| 89 | $this->repository->setUrl($repoUrl)->setHttpClient($this->githubClient); |
||
| 90 | $package->setRepository($this->repository); |
||
| 91 | $packageTag = $this->repository->findPackage($package->getRequiredVersion()); |
||
| 92 | if (is_null($packageTag)) { |
||
| 93 | throw new RuntimeException(sprintf('Cannot find package %s version %s.', $package->getName(), $package->getRequiredVersion())); |
||
| 94 | } |
||
| 95 | } else { |
||
| 96 | $packageTag = $this->getPackageTag($package, true); |
||
| 97 | $package->setRepository($this->repository); |
||
| 98 | } |
||
| 99 | |||
| 100 | $package->setVersion($packageTag); |
||
| 101 | |||
| 102 | $this->updateBowerFile($package, $isDependency); |
||
| 103 | |||
| 104 | // if package is already installed, match current version with latest available version |
||
| 105 | if ($this->isPackageInstalled($package)) { |
||
| 106 | $packageBower = $this->config->getPackageBowerFileContent($package); |
||
| 107 | if(isset($packageBower['version']) && $packageTag == $packageBower['version']) { |
||
| 108 | // if version is fully matching, there's no need to install |
||
| 109 | return; |
||
| 110 | } |
||
| 111 | } |
||
| 112 | |||
| 113 | $this->output->writelnInfoPackage($package); |
||
| 114 | |||
| 115 | $this->output->writelnInstalledPackage($package); |
||
| 116 | |||
| 117 | $this->cachePackage($package); |
||
| 118 | |||
| 119 | $installer->install($package); |
||
| 120 | |||
| 121 | $overrides = $this->config->getOverrideFor($package->getName()); |
||
| 122 | if (array_key_exists('dependencies', $overrides)) { |
||
| 123 | $dependencies = $overrides['dependencies']; |
||
| 124 | } else { |
||
| 125 | $dependencies = $package->getRequires(); |
||
| 126 | } |
||
| 127 | if (!empty($dependencies)) { |
||
| 128 | foreach ($dependencies as $name => $version) { |
||
| 129 | $depPackage = new Package($name, $version); |
||
| 130 | if (!$this->isPackageInstalled($depPackage)) { |
||
| 131 | $this->installPackage($depPackage, $installer, true); |
||
| 132 | } else { |
||
| 133 | $this->updatePackage($depPackage, $installer); |
||
| 134 | } |
||
| 135 | } |
||
| 136 | } |
||
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Install all dependencies |
||
| 141 | * |
||
| 142 | * @param InstallerInterface $installer |
||
| 143 | */ |
||
| 144 | public function installDependencies(InstallerInterface $installer) |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Update a single package |
||
| 160 | * |
||
| 161 | * @param PackageInterface $package |
||
| 162 | * @param InstallerInterface $installer |
||
| 163 | */ |
||
| 164 | public function updatePackage(PackageInterface $package, InstallerInterface $installer) |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Update all dependencies |
||
| 218 | * |
||
| 219 | * @param InstallerInterface $installer |
||
| 220 | */ |
||
| 221 | public function updatePackages(InstallerInterface $installer) |
||
| 230 | |||
| 231 | /** |
||
| 232 | * @param PackageInterface $package |
||
| 233 | * @param string $info |
||
| 234 | * @return mixed |
||
| 235 | */ |
||
| 236 | public function getPackageInfo(PackageInterface $package, $info = 'url') |
||
| 259 | |||
| 260 | /** |
||
| 261 | * @param string $name |
||
| 262 | * @return array |
||
| 263 | */ |
||
| 264 | public function lookupPackage($package) |
||
| 268 | |||
| 269 | /** |
||
| 270 | * @param PackageInterface $package |
||
| 271 | * @return string |
||
| 272 | */ |
||
| 273 | public function getPackageBowerFile(PackageInterface $package) |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Uninstall a single package |
||
| 285 | * |
||
| 286 | * @param PackageInterface $package |
||
| 287 | * @param InstallerInterface $installer |
||
| 288 | */ |
||
| 289 | public function uninstallPackage(PackageInterface $package, InstallerInterface $installer) |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Search packages by name |
||
| 299 | * |
||
| 300 | * @param string $name |
||
| 301 | * @return array |
||
| 302 | */ |
||
| 303 | public function searchPackages($name) |
||
| 304 | { |
||
| 305 | try { |
||
| 306 | $url = $this->config->getBasePackagesUrl() . 'search/' . $name; |
||
| 307 | $response = $this->githubClient->getHttpClient()->get($url); |
||
| 308 | |||
| 309 | return json_decode($response->getBody(true), true); |
||
| 310 | } catch (RequestException $e) { |
||
| 311 | throw new RuntimeException(sprintf('Cannot get package list from %s.', str_replace('/packages/', '', $this->config->getBasePackagesUrl()))); |
||
| 312 | } |
||
| 313 | } |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Get a list of installed packages |
||
| 317 | * |
||
| 318 | * @param InstallerInterface $installer |
||
| 319 | * @param Finder $finder |
||
| 320 | * @return array |
||
| 321 | */ |
||
| 322 | public function getInstalledPackages(InstallerInterface $installer, Finder $finder) |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Check if package is installed |
||
| 329 | * |
||
| 330 | * @param PackageInterface $package |
||
| 331 | * @return bool |
||
| 332 | */ |
||
| 333 | public function isPackageInstalled(PackageInterface $package) |
||
| 337 | |||
| 338 | /** |
||
| 339 | * {@inheritdoc} |
||
| 340 | */ |
||
| 341 | public function isPackageExtraneous(PackageInterface $package, $checkInstall = false) |
||
| 380 | |||
| 381 | /** |
||
| 382 | * @param array $params |
||
| 383 | * @return array |
||
| 384 | */ |
||
| 385 | protected function createAClearBowerFile(array $params) |
||
| 400 | |||
| 401 | /** |
||
| 402 | * @param PackageInterface $package |
||
| 403 | * @param bool $setInfo |
||
| 404 | * @return string |
||
| 405 | */ |
||
| 406 | protected function getPackageTag(PackageInterface $package, $setInfo = false) |
||
| 427 | |||
| 428 | /** |
||
| 429 | * @param string $name |
||
| 430 | * @return array |
||
| 431 | */ |
||
| 432 | protected function findPackage($package) |
||
| 452 | |||
| 453 | /** |
||
| 454 | * @param PackageInterface $package |
||
| 455 | */ |
||
| 456 | private function cachePackage(PackageInterface $package) |
||
| 464 | |||
| 465 | /** |
||
| 466 | * @param PackageInterface $package |
||
| 467 | * @param bool $isDependency |
||
| 468 | */ |
||
| 469 | private function updateBowerFile(PackageInterface $package, $isDependency = false) |
||
| 479 | } |
||
| 480 |