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 ModuleCommandHandler 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 ModuleCommandHandler, and based on these observations, apply Extract Interface, too.
| 1 | <?php | ||
| 35 | class ModuleCommandHandler | ||
| 36 | { | ||
| 37 | /** | ||
| 38 | * @var array | ||
| 39 | */ | ||
| 40 | private static $stateStrings = array( | ||
| 41 | ModuleState::ENABLED => 'enabled', | ||
| 42 | ModuleState::NOT_FOUND => 'not-found', | ||
| 43 | ModuleState::NOT_LOADABLE => 'not-loadable', | ||
| 44 | ); | ||
| 45 | |||
| 46 | /** | ||
| 47 | * @var ModuleManager | ||
| 48 | */ | ||
| 49 | private $moduleManager; | ||
| 50 | |||
| 51 | /** | ||
| 52 | * Creates the handler. | ||
| 53 | * | ||
| 54 | * @param ModuleManager $moduleManager The module manager | ||
| 55 | */ | ||
| 56 | 20 | public function __construct(ModuleManager $moduleManager) | |
| 60 | |||
| 61 | /** | ||
| 62 | * Handles the "module --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) | |
| 81 | |||
| 82 | /** | ||
| 83 | * Handles the "module --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 "module --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 "module --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 "module --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 module 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 ModuleState} constants | ||
| 168 | */ | ||
| 169 | 11 | private function getSelectedStates(Args $args) | |
| 187 | |||
| 188 | /** | ||
| 189 | * Returns the modules that should be displayed for the given console | ||
| 190 | * arguments. | ||
| 191 | * | ||
| 192 | * @param Args $args The console arguments | ||
| 193 | * | ||
| 194 | * @return ModuleList The modules | ||
| 195 | */ | ||
| 196 | 11 | private function getSelectedModules(Args $args) | |
| 224 | |||
| 225 | /** | ||
| 226 | * Prints modules with intermediate headers for the module states. | ||
| 227 | * | ||
| 228 | * @param IO $io The I/O | ||
| 229 | * @param ModuleList $modules The modules to print | ||
| 230 | * @param int[] $states The states to print | ||
| 231 | */ | ||
| 232 | 10 | private function printModulesByState(IO $io, ModuleList $modules, array $states) | |
| 261 | |||
| 262 | /** | ||
| 263 | * Prints modules using the given format. | ||
| 264 | * | ||
| 265 | * @param IO $io The I/O | ||
| 266 | * @param ModuleList $modules The modules to print | ||
| 267 | * @param string $format The format string | ||
| 268 | */ | ||
| 269 | 1 | private function printModulesWithFormat(IO $io, ModuleList $modules, $format) | |
| 284 | |||
| 285 | /** | ||
| 286 | * Prints the heading for a given module state. | ||
| 287 | * | ||
| 288 | * @param IO $io The I/O | ||
| 289 |      * @param int $ModuleState The {@link ModuleState} constant | ||
| 290 | */ | ||
| 291 | 6 | private function printModuleState(IO $io, $ModuleState) | |
| 312 | |||
| 313 | /** | ||
| 314 | * Prints a list of modules in a table. | ||
| 315 | * | ||
| 316 | * @param IO $io The I/O | ||
| 317 | * @param Module[] $modules The modules | ||
| 318 | * @param string|null $styleTag The tag used to style the output. If `null`, | ||
| 319 | * the default colors are used | ||
| 320 | * @param bool $indent Whether to indent the output | ||
| 321 | */ | ||
| 322 | 9 | private function printModuleTable(IO $io, array $modules, $styleTag = null, $indent = false) | |
| 350 | |||
| 351 | /** | ||
| 352 | * Prints not-loadable modules in a table. | ||
| 353 | * | ||
| 354 | * @param IO $io The I/O | ||
| 355 | * @param Module[] $modules The not-loadable modules | ||
| 356 | * @param bool $indent Whether to indent the output | ||
| 357 | */ | ||
| 358 | 5 | private function printNotLoadableModules(IO $io, array $modules, $indent = false) | |
| 392 | } | ||
| 393 | 
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.