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 MapCommandHandler 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 MapCommandHandler, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 35 | class MapCommandHandler |
||
| 36 | { |
||
| 37 | /** |
||
| 38 | * @var RepositoryManager |
||
| 39 | */ |
||
| 40 | private $repoManager; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var ModuleList |
||
| 44 | */ |
||
| 45 | private $modules; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var string |
||
| 49 | */ |
||
| 50 | private $currentPath = '/'; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Creates the handler. |
||
| 54 | * |
||
| 55 | * @param RepositoryManager $repoManager The repository manager |
||
| 56 | * @param ModuleList $modules The loaded modules |
||
| 57 | */ |
||
| 58 | 24 | public function __construct(RepositoryManager $repoManager, ModuleList $modules) |
|
| 63 | |||
| 64 | /** |
||
| 65 | * Handles the "puli map --list" command. |
||
| 66 | * |
||
| 67 | * @param Args $args The console arguments |
||
| 68 | * @param IO $io The I/O |
||
| 69 | * |
||
| 70 | * @return int The status code |
||
| 71 | */ |
||
| 72 | 12 | public function handleList(Args $args, IO $io) |
|
| 73 | { |
||
| 74 | 12 | $moduleNames = ArgsUtil::getModuleNames($args, $this->modules); |
|
| 75 | 12 | $states = $this->getPathMappingStates($args); |
|
| 76 | |||
| 77 | 12 | $printState = count($states) > 1; |
|
| 78 | 12 | $printModuleName = count($moduleNames) > 1; |
|
| 79 | 12 | $printHeaders = $printState || $printModuleName; |
|
| 80 | 12 | $printAdvice = true; |
|
| 81 | 12 | $indentation = ($printState && $printModuleName) ? 8 |
|
| 82 | 12 | : ($printState || $printModuleName ? 4 : 0); |
|
| 83 | |||
| 84 | 12 | foreach ($states as $state) { |
|
| 85 | 12 | $statePrinted = !$printState; |
|
| 86 | |||
| 87 | 12 | if (PathMappingState::CONFLICT === $state) { |
|
| 88 | 7 | $expr = Expr::method('getContainingModule', Expr::method('getName', Expr::in($moduleNames))) |
|
| 89 | 7 | ->andMethod('getState', Expr::same($state)); |
|
| 90 | |||
| 91 | 7 | $mappings = $this->repoManager->findPathMappings($expr); |
|
| 92 | |||
| 93 | 7 | if (!$mappings) { |
|
|
|
|||
| 94 | 1 | continue; |
|
| 95 | } |
||
| 96 | |||
| 97 | 6 | $printAdvice = false; |
|
| 98 | |||
| 99 | 6 | if ($printState) { |
|
| 100 | 5 | $this->printPathMappingStateHeader($io, $state); |
|
| 101 | } |
||
| 102 | |||
| 103 | 6 | $this->printConflictTable($io, $mappings, $printState ? 4 : 0); |
|
| 104 | |||
| 105 | 6 | if ($printHeaders) { |
|
| 106 | 6 | $io->writeLine(''); |
|
| 107 | } |
||
| 108 | |||
| 109 | 6 | continue; |
|
| 110 | } |
||
| 111 | |||
| 112 | 11 | foreach ($moduleNames as $moduleName) { |
|
| 113 | 11 | $expr = Expr::method('getContainingModule', Expr::method('getName', Expr::same($moduleName))) |
|
| 114 | 11 | ->andMethod('getState', Expr::same($state)); |
|
| 115 | |||
| 116 | 11 | $mappings = $this->repoManager->findPathMappings($expr); |
|
| 117 | |||
| 118 | 11 | if (!$mappings) { |
|
| 119 | 1 | continue; |
|
| 120 | } |
||
| 121 | |||
| 122 | 10 | $printAdvice = false; |
|
| 123 | |||
| 124 | 10 | if (!$statePrinted) { |
|
| 125 | 6 | $this->printPathMappingStateHeader($io, $state); |
|
| 126 | 6 | $statePrinted = true; |
|
| 127 | } |
||
| 128 | |||
| 129 | 10 | View Code Duplication | if ($printModuleName) { |
| 130 | 6 | $prefix = $printState ? ' ' : ''; |
|
| 131 | 6 | $io->writeLine(sprintf('%sModule: %s', $prefix, $moduleName)); |
|
| 132 | 6 | $io->writeLine(''); |
|
| 133 | } |
||
| 134 | |||
| 135 | 10 | $this->printMappingTable($io, $mappings, $indentation, PathMappingState::ENABLED === $state); |
|
| 136 | |||
| 137 | 10 | if ($printHeaders) { |
|
| 138 | 11 | $io->writeLine(''); |
|
| 139 | } |
||
| 140 | } |
||
| 141 | } |
||
| 142 | |||
| 143 | 12 | if ($printAdvice) { |
|
| 144 | 1 | $io->writeLine('No path mappings. Use "puli map <path> <file>" to map a Puli path to a file or directory.'); |
|
| 145 | } |
||
| 146 | |||
| 147 | 12 | return 0; |
|
| 148 | } |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Handles the "puli map" command. |
||
| 152 | * |
||
| 153 | * @param Args $args The console arguments |
||
| 154 | * |
||
| 155 | * @return int The status code |
||
| 156 | */ |
||
| 157 | 3 | View Code Duplication | public function handleAdd(Args $args) |
| 169 | |||
| 170 | /** |
||
| 171 | * Handles the "puli map --update" command. |
||
| 172 | * |
||
| 173 | * @param Args $args The console arguments |
||
| 174 | * |
||
| 175 | * @return int The status code |
||
| 176 | */ |
||
| 177 | 6 | public function handleUpdate(Args $args) |
|
| 210 | |||
| 211 | /** |
||
| 212 | * Handles the "puli map --delete" command. |
||
| 213 | * |
||
| 214 | * @param Args $args The console arguments |
||
| 215 | * |
||
| 216 | * @return int The status code |
||
| 217 | */ |
||
| 218 | 3 | public function handleDelete(Args $args) |
|
| 219 | { |
||
| 220 | 3 | $repositoryPath = Path::makeAbsolute($args->getArgument('path'), $this->currentPath); |
|
| 221 | |||
| 222 | 3 | if (!$this->repoManager->hasRootPathMapping($repositoryPath)) { |
|
| 223 | 1 | throw new RuntimeException(sprintf( |
|
| 224 | 1 | 'The path "%s" is not mapped in the module "%s".', |
|
| 225 | $repositoryPath, |
||
| 226 | 1 | $this->modules->getRootModuleName() |
|
| 227 | )); |
||
| 228 | } |
||
| 229 | |||
| 230 | 2 | $this->repoManager->removeRootPathMapping($repositoryPath); |
|
| 231 | |||
| 232 | 2 | return 0; |
|
| 233 | } |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Prints a list of path mappings. |
||
| 237 | * |
||
| 238 | * @param IO $io The I/O |
||
| 239 | * @param PathMapping[] $mappings The path mappings |
||
| 240 | * @param int $indentation The number of spaces to indent the |
||
| 241 | * output |
||
| 242 | * @param bool $enabled Whether the path mappings are enabled. |
||
| 243 | * If not, the output is printed in red |
||
| 244 | */ |
||
| 245 | 10 | private function printMappingTable(IO $io, array $mappings, $indentation = 0, $enabled = true) |
|
| 246 | { |
||
| 247 | 10 | $table = new Table(PuliTableStyle::borderless()); |
|
| 248 | |||
| 249 | 10 | $table->setHeaderRow(array('Puli Path', 'Real Path(s)')); |
|
| 250 | |||
| 251 | 10 | $pathTag = $enabled ? 'c1' : 'bad'; |
|
| 252 | |||
| 253 | 10 | foreach ($mappings as $mapping) { |
|
| 254 | 10 | if ($enabled) { |
|
| 255 | 9 | $pathReferences = array(); |
|
| 256 | |||
| 257 | 9 | foreach ($mapping->getPathReferences() as $pathReference) { |
|
| 258 | // Underline referenced packages |
||
| 259 | 9 | $pathReference = preg_replace('~^@([^:]+):~', '@<u>$1</u>:', $pathReference); |
|
| 260 | |||
| 261 | // Highlight path parts |
||
| 262 | 9 | $pathReference = preg_replace('~^(@([^:]+):)?(.*)$~', '$1<c2>$3</c2>', $pathReference); |
|
| 263 | |||
| 264 | 9 | $pathReferences[] = $pathReference; |
|
| 265 | } |
||
| 266 | |||
| 267 | 9 | $pathReferences = implode(', ', $pathReferences); |
|
| 268 | } else { |
||
| 269 | 7 | $pathReferences = '<bad>'.implode(', ', $mapping->getPathReferences()).'</bad>'; |
|
| 270 | } |
||
| 271 | |||
| 272 | 10 | $table->addRow(array( |
|
| 273 | 10 | sprintf('<%s>%s</%s>', $pathTag, $mapping->getRepositoryPath(), $pathTag), |
|
| 274 | 10 | $pathReferences, |
|
| 275 | )); |
||
| 276 | } |
||
| 277 | |||
| 278 | 10 | $table->render($io, $indentation); |
|
| 279 | 10 | } |
|
| 280 | |||
| 281 | /** |
||
| 282 | * Prints a list of conflicting path mappings. |
||
| 283 | * |
||
| 284 | * @param IO $io The I/O |
||
| 285 | * @param PathMapping[] $mappings The path mappings |
||
| 286 | * @param int $indentation The number of spaces to indent the |
||
| 287 | * output |
||
| 288 | */ |
||
| 289 | 6 | private function printConflictTable(IO $io, array $mappings, $indentation = 0) |
|
| 331 | |||
| 332 | /** |
||
| 333 | * Returns the path mapping states selected in the console arguments. |
||
| 334 | * |
||
| 335 | * @param Args $args The console arguments |
||
| 336 | * |
||
| 337 | * @return int[] The selected {@link PathMappingState} constants |
||
| 338 | */ |
||
| 339 | 12 | private function getPathMappingStates(Args $args) |
|
| 353 | |||
| 354 | /** |
||
| 355 | * Prints the header for a path mapping state. |
||
| 356 | * |
||
| 357 | * @param IO $io The I/O |
||
| 358 | * @param int $pathMappingState The {@link PathMappingState} constant |
||
| 359 | */ |
||
| 360 | 6 | private function printPathMappingStateHeader(IO $io, $pathMappingState) |
|
| 361 | { |
||
| 362 | View Code Duplication | switch ($pathMappingState) { |
|
| 363 | 6 | case PathMappingState::ENABLED: |
|
| 364 | 6 | $io->writeLine('The following path mappings are currently enabled:'); |
|
| 365 | 6 | $io->writeLine(''); |
|
| 366 | |||
| 367 | 6 | return; |
|
| 368 | 6 | case PathMappingState::NOT_FOUND: |
|
| 369 | 6 | $io->writeLine('The target paths of the following path mappings were not found:'); |
|
| 370 | 6 | $io->writeLine(''); |
|
| 371 | |||
| 372 | 6 | return; |
|
| 373 | 5 | case PathMappingState::CONFLICT: |
|
| 374 | 5 | $io->writeLine('Some path mappings have conflicting paths:'); |
|
| 375 | 5 | $io->writeLine(' (add the module names to the "override-order" key in puli.json to resolve)'); |
|
| 376 | 5 | $io->writeLine(''); |
|
| 377 | |||
| 378 | 5 | return; |
|
| 379 | } |
||
| 380 | } |
||
| 381 | |||
| 382 | 5 | private function mappingsEqual(PathMapping $mapping1, PathMapping $mapping2) |
|
| 387 | } |
||
| 388 |
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.