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:
Complex classes like PackageManagerImpl 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 PackageManagerImpl, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 41 | class PackageManagerImpl implements PackageManager |
||
| 42 | { |
||
| 43 | /** |
||
| 44 | * @var ProjectContext |
||
| 45 | */ |
||
| 46 | private $context; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var string |
||
| 50 | */ |
||
| 51 | private $rootDir; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var RootPackageFile |
||
| 55 | */ |
||
| 56 | private $rootPackageFile; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var PackageFileStorage |
||
| 60 | */ |
||
| 61 | private $packageFileStorage; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var PackageCollection |
||
| 65 | */ |
||
| 66 | private $packages; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Loads the package repository for a given project. |
||
| 70 | * |
||
| 71 | * @param ProjectContext $context The project context. |
||
| 72 | * @param PackageFileStorage $packageFileStorage The package file storage. |
||
| 73 | * |
||
| 74 | * @throws FileNotFoundException If the install path of a package not exist. |
||
| 75 | * @throws NoDirectoryException If the install path of a package points to a file. |
||
| 76 | * @throws InvalidConfigException If a configuration file contains invalid configuration. |
||
| 77 | * @throws NameConflictException If a package has the same name as another loaded package. |
||
| 78 | */ |
||
| 79 | 52 | public function __construct(ProjectContext $context, PackageFileStorage $packageFileStorage) |
|
| 86 | |||
| 87 | /** |
||
| 88 | * {@inheritdoc} |
||
| 89 | */ |
||
| 90 | 12 | public function installPackage($installPath, $name = null, $installerName = InstallInfo::DEFAULT_INSTALLER_NAME, $env = Environment::PROD) |
|
| 91 | { |
||
| 92 | 12 | Assert::string($installPath, 'The install path must be a string. Got: %s'); |
|
| 93 | 12 | Assert::string($installerName, 'The installer name must be a string. Got: %s'); |
|
| 94 | 12 | Assert::oneOf($env, Environment::all(), 'The environment must be one of: %2$s. Got: %s'); |
|
| 95 | 12 | Assert::nullOrPackageName($name); |
|
| 96 | |||
| 97 | 11 | $this->assertPackagesLoaded(); |
|
| 98 | |||
| 99 | 11 | $installPath = Path::makeAbsolute($installPath, $this->rootDir); |
|
| 100 | |||
| 101 | 11 | foreach ($this->packages as $package) { |
|
| 102 | 11 | if ($installPath === $package->getInstallPath()) { |
|
| 103 | 11 | return; |
|
| 104 | } |
||
| 105 | } |
||
| 106 | |||
| 107 | 10 | if (null === $name) { |
|
| 108 | // Read the name from the package file |
||
| 109 | 8 | $name = $this->loadPackageFile($installPath)->getPackageName(); |
|
| 110 | } |
||
| 111 | |||
| 112 | 8 | if (null === $name) { |
|
| 113 | 1 | throw new InvalidConfigException(sprintf( |
|
| 114 | 'Could not find a name for the package at %s. The name should '. |
||
| 115 | 'either be passed to the installer or be set in the "name" '. |
||
| 116 | 1 | 'property of %s.', |
|
| 117 | $installPath, |
||
| 118 | 1 | $installPath.'/puli.json' |
|
| 119 | )); |
||
| 120 | } |
||
| 121 | |||
| 122 | 7 | if ($this->packages->contains($name)) { |
|
| 123 | 1 | throw NameConflictException::forName($name); |
|
| 124 | } |
||
| 125 | |||
| 126 | 6 | $relInstallPath = Path::makeRelative($installPath, $this->rootDir); |
|
| 127 | 6 | $installInfo = new InstallInfo($name, $relInstallPath); |
|
| 128 | 6 | $installInfo->setInstallerName($installerName); |
|
| 129 | 6 | $installInfo->setEnvironment($env); |
|
| 130 | |||
| 131 | 6 | $package = $this->loadPackage($installInfo); |
|
| 132 | |||
| 133 | 6 | $this->assertNoLoadErrors($package); |
|
| 134 | 5 | $this->rootPackageFile->addInstallInfo($installInfo); |
|
| 135 | |||
| 136 | try { |
||
| 137 | 5 | $this->packageFileStorage->saveRootPackageFile($this->rootPackageFile); |
|
| 138 | } catch (Exception $e) { |
||
| 139 | $this->rootPackageFile->removeInstallInfo($name); |
||
| 140 | |||
| 141 | throw $e; |
||
| 142 | } |
||
| 143 | |||
| 144 | 5 | $this->packages->add($package); |
|
| 145 | 5 | } |
|
| 146 | |||
| 147 | /** |
||
| 148 | * {@inheritdoc} |
||
| 149 | */ |
||
| 150 | 8 | public function renamePackage($name, $newName) |
|
| 151 | { |
||
| 152 | 8 | $package = $this->getPackage($name); |
|
| 153 | |||
| 154 | 8 | if ($name === $newName) { |
|
| 155 | 2 | return; |
|
| 156 | } |
||
| 157 | |||
| 158 | 6 | if ($this->packages->contains($newName)) { |
|
| 159 | 2 | throw NameConflictException::forName($newName); |
|
| 160 | } |
||
| 161 | |||
| 162 | 4 | if ($package instanceof RootPackage) { |
|
| 163 | 2 | $this->renameRootPackage($package, $newName); |
|
| 164 | } else { |
||
| 165 | 2 | $this->renameNonRootPackage($package, $newName); |
|
| 166 | } |
||
| 167 | 2 | } |
|
| 168 | |||
| 169 | /** |
||
| 170 | * {@inheritdoc} |
||
| 171 | */ |
||
| 172 | 4 | public function removePackage($name) |
|
| 173 | { |
||
| 174 | // Only check that this is a string. The error message "not found" is |
||
| 175 | // more helpful than e.g. "package name must contain /". |
||
| 176 | 4 | Assert::string($name, 'The package name must be a string. Got: %s'); |
|
| 177 | |||
| 178 | 4 | $this->assertPackagesLoaded(); |
|
| 179 | |||
| 180 | 4 | if ($this->rootPackageFile->hasInstallInfo($name)) { |
|
| 181 | 2 | $installInfo = $this->rootPackageFile->getInstallInfo($name); |
|
| 182 | 2 | $this->rootPackageFile->removeInstallInfo($name); |
|
| 183 | |||
| 184 | try { |
||
| 185 | 2 | $this->packageFileStorage->saveRootPackageFile($this->rootPackageFile); |
|
| 186 | 1 | } catch (Exception $e) { |
|
| 187 | 1 | $this->rootPackageFile->addInstallInfo($installInfo); |
|
| 188 | |||
| 189 | 1 | throw $e; |
|
| 190 | } |
||
| 191 | } |
||
| 192 | |||
| 193 | 3 | $this->packages->remove($name); |
|
| 194 | 3 | } |
|
| 195 | |||
| 196 | /** |
||
| 197 | * {@inheritdoc} |
||
| 198 | */ |
||
| 199 | 3 | public function removePackages(Expression $expr) |
|
| 200 | { |
||
| 201 | 3 | $this->assertPackagesLoaded(); |
|
| 202 | |||
| 203 | 3 | $installInfos = $this->rootPackageFile->getInstallInfos(); |
|
| 204 | 3 | $packages = $this->packages->toArray(); |
|
| 205 | |||
| 206 | 3 | foreach ($this->packages->getInstalledPackages() as $package) { |
|
| 207 | 3 | if ($expr->evaluate($package)) { |
|
| 208 | 3 | $this->rootPackageFile->removeInstallInfo($package->getName()); |
|
| 209 | 3 | $this->packages->remove($package->getName()); |
|
| 210 | } |
||
| 211 | } |
||
| 212 | |||
| 213 | 3 | if (!$installInfos) { |
|
|
|
|||
| 214 | return; |
||
| 215 | } |
||
| 216 | |||
| 217 | try { |
||
| 218 | 3 | $this->packageFileStorage->saveRootPackageFile($this->rootPackageFile); |
|
| 219 | 1 | } catch (Exception $e) { |
|
| 220 | 1 | $this->rootPackageFile->setInstallInfos($installInfos); |
|
| 221 | 1 | $this->packages->replace($packages); |
|
| 222 | |||
| 223 | 1 | throw $e; |
|
| 224 | } |
||
| 225 | 2 | } |
|
| 226 | |||
| 227 | /** |
||
| 228 | * {@inheritdoc} |
||
| 229 | */ |
||
| 230 | 1 | public function clearPackages() |
|
| 234 | |||
| 235 | /** |
||
| 236 | * {@inheritdoc} |
||
| 237 | */ |
||
| 238 | 10 | public function getPackage($name) |
|
| 246 | |||
| 247 | /** |
||
| 248 | * {@inheritdoc} |
||
| 249 | */ |
||
| 250 | 1 | public function getRootPackage() |
|
| 256 | |||
| 257 | /** |
||
| 258 | * {@inheritdoc} |
||
| 259 | */ |
||
| 260 | 23 | public function getPackages() |
|
| 267 | |||
| 268 | /** |
||
| 269 | * {@inheritdoc} |
||
| 270 | */ |
||
| 271 | 4 | public function findPackages(Expression $expr) |
|
| 272 | { |
||
| 273 | 4 | $this->assertPackagesLoaded(); |
|
| 274 | |||
| 275 | 4 | $packages = new PackageCollection(); |
|
| 276 | |||
| 277 | 4 | foreach ($this->packages as $package) { |
|
| 278 | 4 | if ($expr->evaluate($package)) { |
|
| 279 | 4 | $packages->add($package); |
|
| 280 | } |
||
| 281 | } |
||
| 282 | |||
| 283 | 4 | return $packages; |
|
| 284 | } |
||
| 285 | |||
| 286 | /** |
||
| 287 | * {@inheritdoc} |
||
| 288 | */ |
||
| 289 | 11 | public function hasPackage($name) |
|
| 297 | |||
| 298 | /** |
||
| 299 | * {@inheritdoc} |
||
| 300 | */ |
||
| 301 | 1 | View Code Duplication | public function hasPackages(Expression $expr = null) |
| 302 | { |
||
| 303 | 1 | $this->assertPackagesLoaded(); |
|
| 304 | |||
| 305 | 1 | if (!$expr) { |
|
| 306 | 1 | return !$this->packages->isEmpty(); |
|
| 307 | } |
||
| 308 | |||
| 309 | 1 | foreach ($this->packages as $package) { |
|
| 310 | 1 | if ($expr->evaluate($package)) { |
|
| 311 | 1 | return true; |
|
| 312 | } |
||
| 313 | } |
||
| 314 | |||
| 315 | 1 | return false; |
|
| 316 | } |
||
| 317 | |||
| 318 | /** |
||
| 319 | * {@inheritdoc} |
||
| 320 | */ |
||
| 321 | 1 | public function getContext() |
|
| 325 | |||
| 326 | /** |
||
| 327 | * Loads all packages referenced by the install file. |
||
| 328 | * |
||
| 329 | * @throws FileNotFoundException If the install path of a package not exist. |
||
| 330 | * @throws NoDirectoryException If the install path of a package points to a |
||
| 331 | * file. |
||
| 332 | * @throws InvalidConfigException If a package is not configured correctly. |
||
| 333 | * @throws NameConflictException If a package has the same name as another |
||
| 334 | * loaded package. |
||
| 335 | */ |
||
| 336 | 51 | private function loadPackages() |
|
| 337 | { |
||
| 338 | 51 | $this->packages = new PackageCollection(); |
|
| 339 | 51 | $this->packages->add(new RootPackage($this->rootPackageFile, $this->rootDir)); |
|
| 340 | |||
| 341 | 51 | foreach ($this->rootPackageFile->getInstallInfos() as $installInfo) { |
|
| 342 | // Catch and log exceptions so that single packages cannot break |
||
| 343 | // the whole repository |
||
| 344 | 50 | $this->packages->add($this->loadPackage($installInfo)); |
|
| 345 | } |
||
| 346 | 51 | } |
|
| 347 | |||
| 348 | /** |
||
| 349 | * Loads a package for the given install info. |
||
| 350 | * |
||
| 351 | * @param InstallInfo $installInfo The install info. |
||
| 352 | * |
||
| 353 | * @return Package The package. |
||
| 354 | */ |
||
| 355 | 51 | private function loadPackage(InstallInfo $installInfo) |
|
| 373 | |||
| 374 | /** |
||
| 375 | * Loads the package file for the package at the given install path. |
||
| 376 | * |
||
| 377 | * @param string $installPath The absolute install path of the package |
||
| 378 | * |
||
| 379 | * @return PackageFile The loaded package file. |
||
| 380 | */ |
||
| 381 | 51 | private function loadPackageFile($installPath) |
|
| 382 | { |
||
| 383 | 51 | if (!file_exists($installPath)) { |
|
| 384 | 4 | throw FileNotFoundException::forPath($installPath); |
|
| 385 | } |
||
| 386 | |||
| 387 | 50 | if (!is_dir($installPath)) { |
|
| 388 | 2 | throw new NoDirectoryException(sprintf( |
|
| 389 | 2 | 'The path %s is a file. Expected a directory.', |
|
| 390 | $installPath |
||
| 391 | )); |
||
| 392 | } |
||
| 393 | |||
| 394 | 49 | return $this->packageFileStorage->loadPackageFile($installPath.'/puli.json'); |
|
| 395 | } |
||
| 396 | |||
| 397 | 51 | private function assertPackagesLoaded() |
|
| 398 | { |
||
| 399 | 51 | if (!$this->packages) { |
|
| 400 | 51 | $this->loadPackages(); |
|
| 401 | } |
||
| 402 | 51 | } |
|
| 403 | |||
| 404 | 6 | private function assertNoLoadErrors(Package $package) |
|
| 413 | |||
| 414 | 2 | private function renameRootPackage(RootPackage $package, $newName) |
|
| 431 | |||
| 432 | 2 | private function renameNonRootPackage(Package $package, $newName) |
|
| 433 | { |
||
| 434 | 2 | $previousInstallInfo = $package->getInstallInfo(); |
|
| 435 | |||
| 436 | 2 | $installInfo = new InstallInfo($newName, $previousInstallInfo->getInstallPath()); |
|
| 437 | 2 | $installInfo->setInstallerName($previousInstallInfo->getInstallerName()); |
|
| 438 | |||
| 439 | 2 | foreach ($previousInstallInfo->getDisabledBindingUuids() as $uuid) { |
|
| 440 | 1 | $installInfo->addDisabledBindingUuid($uuid); |
|
| 463 | } |
||
| 464 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.