Complex classes like VersionedPackage 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 VersionedPackage, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 33 | class VersionedPackage implements PackageInterface |
||
| 34 | { |
||
| 35 | /** |
||
| 36 | * The package to hold versions for. |
||
| 37 | * |
||
| 38 | * @var PackageInterface |
||
| 39 | */ |
||
| 40 | protected $package; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * The version list. |
||
| 44 | * |
||
| 45 | * @var PackageInterface[] |
||
| 46 | */ |
||
| 47 | protected $versions; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * The meta data. |
||
| 51 | * |
||
| 52 | * @var array |
||
| 53 | */ |
||
| 54 | protected $meta = []; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Create a new instance. |
||
| 58 | * |
||
| 59 | * @param PackageInterface $package The package to hold versions for. |
||
| 60 | * |
||
| 61 | * @param array $versions The initial versions. |
||
| 62 | */ |
||
| 63 | public function __construct(PackageInterface $package, array $versions = []) |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Retrieve the meta data. |
||
| 71 | * |
||
| 72 | * @param string $key The key of the meta data to retrieve. |
||
| 73 | * |
||
| 74 | * @return mixed|null |
||
| 75 | */ |
||
| 76 | public function getMetaData($key) |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Add meta data. |
||
| 83 | * |
||
| 84 | * @param string $key The key to use. |
||
| 85 | * |
||
| 86 | * @param mixed $value The value to use. |
||
| 87 | * |
||
| 88 | * @param bool $overwrite Flag if the previous value shall be overridden. |
||
| 89 | * |
||
| 90 | * @return $this |
||
| 91 | */ |
||
| 92 | public function addMetaData($key, $value, $overwrite = true) |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Replace the meta data of the package. |
||
| 103 | * |
||
| 104 | * @param array $meta The new meta data. |
||
| 105 | * |
||
| 106 | * @return $this |
||
| 107 | */ |
||
| 108 | public function replaceMetaData(array $meta) |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Retrieve the versions. |
||
| 117 | * |
||
| 118 | * @return PackageInterface[] |
||
| 119 | */ |
||
| 120 | public function getVersions() |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Set the versions and override all previously registered versions. |
||
| 127 | * |
||
| 128 | * @param PackageInterface[] $versions The versions to use. |
||
| 129 | * |
||
| 130 | * @return $this |
||
| 131 | */ |
||
| 132 | public function setVersions(array $versions) |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Add a version to the package. |
||
| 141 | * |
||
| 142 | * @param PackageInterface $version The version to add. |
||
| 143 | * |
||
| 144 | * @return $this |
||
| 145 | */ |
||
| 146 | public function addVersion(PackageInterface $version) |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Add multiple versions. |
||
| 155 | * |
||
| 156 | * @param PackageInterface[] $versions The versions to add. |
||
| 157 | * |
||
| 158 | * @return $this |
||
| 159 | */ |
||
| 160 | public function addVersions(array $versions) |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Remove a version from the package. |
||
| 171 | * |
||
| 172 | * @param PackageInterface|string $version The version to remove. |
||
| 173 | * |
||
| 174 | * @return $this |
||
| 175 | * |
||
| 176 | * @throws \InvalidArgumentException For any invalid version. |
||
| 177 | */ |
||
| 178 | public function removeVersion($version) |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Retrieve the latest version of the package (if any available). |
||
| 204 | * |
||
| 205 | * @return PackageInterface |
||
| 206 | */ |
||
| 207 | public function getLatestVersion() |
||
| 224 | |||
| 225 | /** |
||
| 226 | * {@inheritdoc} |
||
| 227 | */ |
||
| 228 | public function getName() |
||
| 232 | |||
| 233 | /** |
||
| 234 | * {@inheritdoc} |
||
| 235 | */ |
||
| 236 | public function getPrettyName() |
||
| 240 | |||
| 241 | /** |
||
| 242 | * {@inheritdoc} |
||
| 243 | */ |
||
| 244 | public function getNames() |
||
| 248 | |||
| 249 | /** |
||
| 250 | * {@inheritdoc} |
||
| 251 | * |
||
| 252 | * @SuppressWarnings(PHPMD.ShortVariableName) |
||
| 253 | */ |
||
| 254 | public function setId($id) |
||
| 258 | |||
| 259 | /** |
||
| 260 | * {@inheritdoc} |
||
| 261 | */ |
||
| 262 | public function getId() |
||
| 266 | |||
| 267 | /** |
||
| 268 | * {@inheritdoc} |
||
| 269 | */ |
||
| 270 | public function isDev() |
||
| 274 | |||
| 275 | /** |
||
| 276 | * {@inheritdoc} |
||
| 277 | */ |
||
| 278 | public function getType() |
||
| 282 | |||
| 283 | /** |
||
| 284 | * {@inheritdoc} |
||
| 285 | */ |
||
| 286 | public function getTargetDir() |
||
| 290 | |||
| 291 | /** |
||
| 292 | * {@inheritdoc} |
||
| 293 | */ |
||
| 294 | public function getExtra() |
||
| 298 | |||
| 299 | /** |
||
| 300 | * {@inheritdoc} |
||
| 301 | */ |
||
| 302 | public function setInstallationSource($type) |
||
| 306 | |||
| 307 | /** |
||
| 308 | * {@inheritdoc} |
||
| 309 | */ |
||
| 310 | public function getInstallationSource() |
||
| 314 | |||
| 315 | /** |
||
| 316 | * {@inheritdoc} |
||
| 317 | */ |
||
| 318 | public function getSourceType() |
||
| 322 | |||
| 323 | /** |
||
| 324 | * {@inheritdoc} |
||
| 325 | */ |
||
| 326 | public function getSourceUrl() |
||
| 330 | |||
| 331 | /** |
||
| 332 | * {@inheritdoc} |
||
| 333 | */ |
||
| 334 | public function getSourceUrls() |
||
| 338 | |||
| 339 | /** |
||
| 340 | * {@inheritdoc} |
||
| 341 | */ |
||
| 342 | public function getSourceReference() |
||
| 346 | |||
| 347 | /** |
||
| 348 | * {@inheritdoc} |
||
| 349 | */ |
||
| 350 | public function getSourceMirrors() |
||
| 354 | |||
| 355 | /** |
||
| 356 | * {@inheritdoc} |
||
| 357 | */ |
||
| 358 | public function getDistType() |
||
| 362 | |||
| 363 | /** |
||
| 364 | * {@inheritdoc} |
||
| 365 | */ |
||
| 366 | public function getDistUrl() |
||
| 370 | |||
| 371 | /** |
||
| 372 | * {@inheritdoc} |
||
| 373 | */ |
||
| 374 | public function getDistUrls() |
||
| 378 | |||
| 379 | /** |
||
| 380 | * {@inheritdoc} |
||
| 381 | */ |
||
| 382 | public function getDistReference() |
||
| 386 | |||
| 387 | /** |
||
| 388 | * {@inheritdoc} |
||
| 389 | */ |
||
| 390 | public function getDistSha1Checksum() |
||
| 394 | |||
| 395 | /** |
||
| 396 | * {@inheritdoc} |
||
| 397 | */ |
||
| 398 | public function getDistMirrors() |
||
| 402 | |||
| 403 | /** |
||
| 404 | * {@inheritdoc} |
||
| 405 | */ |
||
| 406 | public function getVersion() |
||
| 410 | |||
| 411 | /** |
||
| 412 | * {@inheritdoc} |
||
| 413 | */ |
||
| 414 | public function getPrettyVersion() |
||
| 418 | |||
| 419 | /** |
||
| 420 | * {@inheritdoc} |
||
| 421 | */ |
||
| 422 | public function getReleaseDate() |
||
| 426 | |||
| 427 | /** |
||
| 428 | * {@inheritdoc} |
||
| 429 | */ |
||
| 430 | public function getStability() |
||
| 434 | |||
| 435 | /** |
||
| 436 | * {@inheritdoc} |
||
| 437 | */ |
||
| 438 | public function getRequires() |
||
| 442 | |||
| 443 | /** |
||
| 444 | * {@inheritdoc} |
||
| 445 | */ |
||
| 446 | public function getConflicts() |
||
| 450 | |||
| 451 | /** |
||
| 452 | * {@inheritdoc} |
||
| 453 | */ |
||
| 454 | public function getProvides() |
||
| 458 | |||
| 459 | /** |
||
| 460 | * {@inheritdoc} |
||
| 461 | */ |
||
| 462 | public function getReplaces() |
||
| 466 | |||
| 467 | /** |
||
| 468 | * {@inheritdoc} |
||
| 469 | */ |
||
| 470 | public function getDevRequires() |
||
| 474 | |||
| 475 | /** |
||
| 476 | * {@inheritdoc} |
||
| 477 | */ |
||
| 478 | public function getSuggests() |
||
| 482 | |||
| 483 | /** |
||
| 484 | * {@inheritdoc} |
||
| 485 | */ |
||
| 486 | public function getAutoload() |
||
| 490 | |||
| 491 | /** |
||
| 492 | * {@inheritdoc} |
||
| 493 | */ |
||
| 494 | public function getDevAutoload() |
||
| 498 | |||
| 499 | /** |
||
| 500 | * {@inheritdoc} |
||
| 501 | */ |
||
| 502 | public function getIncludePaths() |
||
| 506 | |||
| 507 | /** |
||
| 508 | * {@inheritdoc} |
||
| 509 | */ |
||
| 510 | public function setRepository(RepositoryInterface $repository) |
||
| 514 | |||
| 515 | /** |
||
| 516 | * {@inheritdoc} |
||
| 517 | */ |
||
| 518 | public function getRepository() |
||
| 522 | |||
| 523 | /** |
||
| 524 | * {@inheritdoc} |
||
| 525 | */ |
||
| 526 | public function getBinaries() |
||
| 530 | |||
| 531 | /** |
||
| 532 | * {@inheritdoc} |
||
| 533 | */ |
||
| 534 | public function getUniqueName() |
||
| 538 | |||
| 539 | /** |
||
| 540 | * {@inheritdoc} |
||
| 541 | */ |
||
| 542 | public function getNotificationUrl() |
||
| 546 | |||
| 547 | /** |
||
| 548 | * {@inheritdoc} |
||
| 549 | */ |
||
| 550 | public function getPrettyString() |
||
| 554 | |||
| 555 | /** |
||
| 556 | * {@inheritDoc} |
||
| 557 | */ |
||
| 558 | public function getFullPrettyVersion($truncate = true) |
||
| 562 | |||
| 563 | /** |
||
| 564 | * {@inheritdoc} |
||
| 565 | */ |
||
| 566 | public function getArchiveExcludes() |
||
| 570 | |||
| 571 | /** |
||
| 572 | * {@inheritdoc} |
||
| 573 | */ |
||
| 574 | public function getTransportOptions() |
||
| 578 | |||
| 579 | /** |
||
| 580 | * {@inheritdoc} |
||
| 581 | */ |
||
| 582 | public function __toString() |
||
| 586 | } |
||
| 587 |
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@returnannotation as described here.