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) |
||
| 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 ($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 | } elseif ($this->isNeedUpdate($depPackage)) { |
||
| 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) |
||
| 165 | { |
||
| 166 | if (!$this->isPackageInstalled($package)) { |
||
| 167 | throw new RuntimeException(sprintf('Package %s is not installed.', $package->getName())); |
||
| 168 | } |
||
| 169 | if (is_null($package->getRequiredVersion())) { |
||
| 170 | $decode = $this->config->getBowerFileContent(); |
||
| 171 | if (empty($decode['dependencies']) || empty($decode['dependencies'][$package->getName()])) { |
||
| 172 | throw new InvalidArgumentException(sprintf('Package %s not found in bower.json', $package->getName())); |
||
| 173 | } |
||
| 174 | $package->setRequiredVersion($decode['dependencies'][$package->getName()]); |
||
| 175 | } |
||
| 176 | |||
| 177 | $bower = $this->config->getPackageBowerFileContent($package); |
||
| 178 | $package->setInfo($bower); |
||
| 179 | $package->setVersion($bower['version']); |
||
| 180 | $package->setRequires(isset($bower['dependencies']) ? $bower['dependencies'] : null); |
||
| 181 | |||
| 182 | $packageTag = $this->getPackageTag($package); |
||
| 183 | $package->setRepository($this->repository); |
||
| 184 | if ($packageTag == $package->getVersion()) { |
||
| 185 | // if version is fully matching, there's no need to update |
||
| 186 | return; |
||
| 187 | } |
||
| 188 | $package->setVersion($packageTag); |
||
| 189 | |||
| 190 | $this->output->writelnUpdatingPackage($package); |
||
| 191 | |||
| 192 | $this->cachePackage($package); |
||
| 193 | |||
| 194 | $installer->update($package); |
||
| 195 | |||
| 196 | $overrides = $this->config->getOverrideFor($package->getName()); |
||
| 197 | if (array_key_exists('dependencies', $overrides)) { |
||
| 198 | $dependencies = $overrides['dependencies']; |
||
| 199 | } else { |
||
| 200 | $dependencies = $package->getRequires(); |
||
| 201 | } |
||
| 202 | if (!empty($dependencies)) { |
||
| 203 | foreach ($dependencies as $name => $requiredVersion) { |
||
| 204 | $depPackage = new Package($name, $requiredVersion); |
||
| 205 | if (!$this->isPackageInstalled($depPackage)) { |
||
| 206 | $this->installPackage($depPackage, $installer, true); |
||
| 207 | } elseif ($this->isNeedUpdate($depPackage)) { |
||
| 208 | $this->updatePackage($depPackage, $installer); |
||
| 209 | } |
||
| 210 | } |
||
| 211 | } |
||
| 212 | } |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Update all dependencies |
||
| 216 | * |
||
| 217 | * @param InstallerInterface $installer |
||
| 218 | */ |
||
| 219 | public function updatePackages(InstallerInterface $installer) |
||
| 228 | |||
| 229 | /** |
||
| 230 | * @param PackageInterface $package |
||
| 231 | * @param string $info |
||
| 232 | * @return mixed |
||
| 233 | */ |
||
| 234 | public function getPackageInfo(PackageInterface $package, $info = 'url') |
||
| 257 | |||
| 258 | /** |
||
| 259 | * @param string $name |
||
| 260 | * @return array |
||
| 261 | */ |
||
| 262 | public function lookupPackage($name) |
||
| 266 | |||
| 267 | /** |
||
| 268 | * @param PackageInterface $package |
||
| 269 | * @return string |
||
| 270 | */ |
||
| 271 | public function getPackageBowerFile(PackageInterface $package) |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Uninstall a single package |
||
| 283 | * |
||
| 284 | * @param PackageInterface $package |
||
| 285 | * @param InstallerInterface $installer |
||
| 286 | */ |
||
| 287 | public function uninstallPackage(PackageInterface $package, InstallerInterface $installer) |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Search packages by name |
||
| 297 | * |
||
| 298 | * @param string $name |
||
| 299 | * @return array |
||
| 300 | */ |
||
| 301 | public function searchPackages($name) |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Get a list of installed packages |
||
| 315 | * |
||
| 316 | * @param InstallerInterface $installer |
||
| 317 | * @param Finder $finder |
||
| 318 | * @return array |
||
| 319 | */ |
||
| 320 | public function getInstalledPackages(InstallerInterface $installer, Finder $finder) |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Check if package is installed |
||
| 327 | * |
||
| 328 | * @param PackageInterface $package |
||
| 329 | * @return bool |
||
| 330 | */ |
||
| 331 | public function isPackageInstalled(PackageInterface $package) |
||
| 335 | |||
| 336 | /** |
||
| 337 | * {@inheritdoc} |
||
| 338 | */ |
||
| 339 | public function isPackageExtraneous(PackageInterface $package, $checkInstall = false) |
||
| 377 | |||
| 378 | /** |
||
| 379 | * @param array $params |
||
| 380 | * @return array |
||
| 381 | */ |
||
| 382 | protected function createAClearBowerFile(array $params) |
||
| 397 | |||
| 398 | /** |
||
| 399 | * @param PackageInterface $package |
||
| 400 | * @param bool $setInfo |
||
| 401 | * @return string |
||
| 402 | */ |
||
| 403 | protected function getPackageTag(PackageInterface $package, $setInfo = false) |
||
| 424 | |||
| 425 | /** |
||
| 426 | * @param string $name |
||
| 427 | * @return array |
||
| 428 | */ |
||
| 429 | protected function findPackage($name) |
||
| 443 | |||
| 444 | /** |
||
| 445 | * @param PackageInterface $package |
||
| 446 | */ |
||
| 447 | private function cachePackage(PackageInterface $package) |
||
| 455 | |||
| 456 | /** |
||
| 457 | * @param PackageInterface $package |
||
| 458 | * @param bool $isDependency |
||
| 459 | */ |
||
| 460 | private function updateBowerFile(PackageInterface $package, $isDependency = false) |
||
| 470 | |||
| 471 | /** |
||
| 472 | * Update only if needed is greater version |
||
| 473 | * |
||
| 474 | * @param PackageInterface $package |
||
| 475 | * @return bool |
||
| 476 | */ |
||
| 477 | public function isNeedUpdate($package) |
||
| 497 | } |
||
| 498 |