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 PackageCommandHandler 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 PackageCommandHandler, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 35 | class PackageCommandHandler |
||
| 36 | { |
||
| 37 | /** |
||
| 38 | * @var array |
||
| 39 | */ |
||
| 40 | private static $stateStrings = array( |
||
| 41 | PackageState::ENABLED => 'enabled', |
||
| 42 | PackageState::NOT_FOUND => 'not-found', |
||
| 43 | PackageState::NOT_LOADABLE => 'not-loadable', |
||
| 44 | ); |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var PackageManager |
||
| 48 | */ |
||
| 49 | private $packageManager; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Creates the handler. |
||
| 53 | * |
||
| 54 | * @param PackageManager $packageManager The package manager. |
||
| 55 | */ |
||
| 56 | 20 | public function __construct(PackageManager $packageManager) |
|
| 60 | |||
| 61 | /** |
||
| 62 | * Handles the "package --list" command. |
||
| 63 | * |
||
| 64 | * @param Args $args The console arguments. |
||
| 65 | * @param IO $io The I/O. |
||
| 66 | * |
||
| 67 | * @return int The status code. |
||
| 68 | */ |
||
| 69 | 11 | public function handleList(Args $args, IO $io) |
|
| 70 | { |
||
| 71 | 11 | $packages = $this->getSelectedPackages($args); |
|
| 72 | |||
| 73 | 11 | if ($args->isOptionSet('format')) { |
|
| 74 | 1 | $this->printPackagesWithFormat($io, $packages, $args->getOption('format')); |
|
| 75 | } else { |
||
| 76 | 10 | $this->printPackagesByState($io, $packages, $this->getSelectedStates($args)); |
|
| 77 | } |
||
| 78 | |||
| 79 | 11 | return 0; |
|
| 80 | } |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Handles the "package --install" command. |
||
| 84 | * |
||
| 85 | * @param Args $args The console arguments. |
||
| 86 | * |
||
| 87 | * @return int The status code. |
||
| 88 | */ |
||
| 89 | 5 | public function handleInstall(Args $args) |
|
| 100 | |||
| 101 | /** |
||
| 102 | * Handles the "package --rename" command. |
||
| 103 | * |
||
| 104 | * @param Args $args The console arguments. |
||
| 105 | * |
||
| 106 | * @return int The status code. |
||
| 107 | */ |
||
| 108 | 1 | public function handleRename(Args $args) |
|
| 117 | |||
| 118 | /** |
||
| 119 | * Handles the "package --delete" command. |
||
| 120 | * |
||
| 121 | * @param Args $args The console arguments. |
||
| 122 | * |
||
| 123 | * @return int The status code. |
||
| 124 | */ |
||
| 125 | 2 | public function handleDelete(Args $args) |
|
| 140 | |||
| 141 | /** |
||
| 142 | * Handles the "package --clean" command. |
||
| 143 | * |
||
| 144 | * @param Args $args The console arguments. |
||
| 145 | * @param IO $io The I/O. |
||
| 146 | * |
||
| 147 | * @return int The status code. |
||
| 148 | */ |
||
| 149 | 1 | public function handleClean(Args $args, IO $io) |
|
| 160 | |||
| 161 | /** |
||
| 162 | * Returns the package states that should be displayed for the given |
||
| 163 | * console arguments. |
||
| 164 | * |
||
| 165 | * @param Args $args The console arguments. |
||
| 166 | * |
||
| 167 | * @return int[] A list of {@link PackageState} constants. |
||
| 168 | */ |
||
| 169 | 11 | private function getSelectedStates(Args $args) |
|
| 187 | |||
| 188 | /** |
||
| 189 | * Returns the packages that should be displayed for the given console |
||
| 190 | * arguments. |
||
| 191 | * |
||
| 192 | * @param Args $args The console arguments. |
||
| 193 | * |
||
| 194 | * @return PackageCollection The packages. |
||
| 195 | */ |
||
| 196 | 11 | private function getSelectedPackages(Args $args) |
|
| 224 | |||
| 225 | /** |
||
| 226 | * Prints packages with intermediate headers for the package states. |
||
| 227 | * |
||
| 228 | * @param IO $io The I/O. |
||
| 229 | * @param PackageCollection $packages The packages to print. |
||
| 230 | * @param int[] $states The states to print. |
||
| 231 | */ |
||
| 232 | 10 | private function printPackagesByState(IO $io, PackageCollection $packages, array $states) |
|
| 261 | |||
| 262 | /** |
||
| 263 | * Prints packages using the given format. |
||
| 264 | * |
||
| 265 | * @param IO $io The I/O. |
||
| 266 | * @param PackageCollection $packages The packages to print. |
||
| 267 | * @param string $format The format string. |
||
| 268 | */ |
||
| 269 | 1 | private function printPackagesWithFormat(IO $io, PackageCollection $packages, $format) |
|
| 283 | |||
| 284 | /** |
||
| 285 | * Prints the heading for a given package state. |
||
| 286 | * |
||
| 287 | * @param IO $io The I/O. |
||
| 288 | * @param int $packageState The {@link PackageState} constant. |
||
| 289 | */ |
||
| 290 | 6 | View Code Duplication | private function printPackageState(IO $io, $packageState) |
| 311 | |||
| 312 | /** |
||
| 313 | * Prints a list of packages in a table. |
||
| 314 | * |
||
| 315 | * @param IO $io The I/O. |
||
| 316 | * @param Package[] $packages The packages. |
||
| 317 | * @param string|null $styleTag The tag used to style the output. If `null`, |
||
| 318 | * the default colors are used. |
||
| 319 | * @param bool $indent Whether to indent the output. |
||
| 320 | */ |
||
| 321 | 9 | private function printPackageTable(IO $io, array $packages, $styleTag = null, $indent = false) |
|
| 349 | |||
| 350 | /** |
||
| 351 | * Prints not-loadable packages in a table. |
||
| 352 | * |
||
| 353 | * @param IO $io The I/O. |
||
| 354 | * @param Package[] $packages The not-loadable packages. |
||
| 355 | * @param bool $indent Whether to indent the output. |
||
| 356 | */ |
||
| 357 | 5 | private function printNotLoadablePackages(IO $io, array $packages, $indent = false) |
|
| 391 | } |
||
| 392 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.