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 TypeCommandHandler 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 TypeCommandHandler, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 36 | class TypeCommandHandler |
||
| 37 | { |
||
| 38 | /** |
||
| 39 | * @var DiscoveryManager |
||
| 40 | */ |
||
| 41 | private $discoveryManager; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var ModuleList |
||
| 45 | */ |
||
| 46 | private $modules; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Creates the handler. |
||
| 50 | * |
||
| 51 | * @param DiscoveryManager $discoveryManager The discovery manager |
||
| 52 | * @param ModuleList $modules The loaded modules |
||
| 53 | */ |
||
| 54 | 25 | public function __construct(DiscoveryManager $discoveryManager, ModuleList $modules) |
|
| 59 | |||
| 60 | /** |
||
| 61 | * Handles the "puli type --list" command. |
||
| 62 | * |
||
| 63 | * @param Args $args The console arguments |
||
| 64 | * @param IO $io The I/O |
||
| 65 | * |
||
| 66 | * @return int The status code |
||
| 67 | */ |
||
| 68 | 11 | public function handleList(Args $args, IO $io) |
|
| 69 | { |
||
| 70 | 11 | $moduleNames = ArgsUtil::getModuleNames($args, $this->modules); |
|
| 71 | 11 | $states = $this->getBindingTypeStates($args); |
|
| 72 | |||
| 73 | 11 | $printStates = count($states) > 1; |
|
| 74 | 11 | $printModuleName = count($moduleNames) > 1; |
|
| 75 | 11 | $printHeaders = $printStates || $printModuleName; |
|
| 76 | 11 | $printTypeAdvice = true; |
|
| 77 | 11 | $printBindAdvice = false; |
|
| 78 | 11 | $indentation = $printStates && $printModuleName ? 8 |
|
| 79 | 11 | : ($printStates || $printModuleName ? 4 : 0); |
|
| 80 | |||
| 81 | 11 | foreach ($states as $state) { |
|
| 82 | 11 | $statePrinted = !$printStates; |
|
| 83 | |||
| 84 | 11 | foreach ($moduleNames as $moduleName) { |
|
| 85 | 11 | $expr = Expr::method('getContainingModule', Expr::method('getName', Expr::same($moduleName))) |
|
| 86 | 11 | ->andMethod('getState', Expr::same($state)); |
|
| 87 | |||
| 88 | 11 | $bindingTypes = $this->discoveryManager->findTypeDescriptors($expr); |
|
| 89 | |||
| 90 | 11 | if (!$bindingTypes) { |
|
|
|
|||
| 91 | 1 | continue; |
|
| 92 | } |
||
| 93 | |||
| 94 | 10 | $printTypeAdvice = false; |
|
| 95 | |||
| 96 | 10 | if (!$statePrinted) { |
|
| 97 | 6 | $this->printBindingTypeState($io, $state); |
|
| 98 | 6 | $statePrinted = true; |
|
| 99 | |||
| 100 | // Only print the advice if at least one type was printed |
||
| 101 | 6 | $printBindAdvice = true; |
|
| 102 | } |
||
| 103 | |||
| 104 | 10 | View Code Duplication | if ($printModuleName) { |
| 105 | 6 | $prefix = $printStates ? ' ' : ''; |
|
| 106 | 6 | $io->writeLine("{$prefix}Module: $moduleName"); |
|
| 107 | 6 | $io->writeLine(''); |
|
| 108 | } |
||
| 109 | |||
| 110 | 10 | $styleTag = BindingTypeState::ENABLED === $state ? null : 'bad'; |
|
| 111 | |||
| 112 | 10 | $this->printTypeTable($io, $bindingTypes, $styleTag, $indentation); |
|
| 113 | |||
| 114 | 10 | if ($printHeaders) { |
|
| 115 | 11 | $io->writeLine(''); |
|
| 116 | } |
||
| 117 | } |
||
| 118 | } |
||
| 119 | |||
| 120 | 11 | if ($printTypeAdvice) { |
|
| 121 | 1 | $io->writeLine('No types defined. Use "puli type --define <name>" to define a type.'); |
|
| 122 | } |
||
| 123 | 11 | if ($printBindAdvice) { |
|
| 124 | 6 | $io->writeLine('Use "puli bind <resource> <type>" to bind a resource to a type.'); |
|
| 125 | } |
||
| 126 | |||
| 127 | 11 | return 0; |
|
| 128 | } |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Handles the "puli type --define" command. |
||
| 132 | * |
||
| 133 | * @param Args $args The console arguments |
||
| 134 | * |
||
| 135 | * @return int The status code |
||
| 136 | */ |
||
| 137 | 6 | public function handleDefine(Args $args) |
|
| 138 | { |
||
| 139 | 6 | $flags = $args->isOptionSet('force') ? DiscoveryManager::OVERRIDE : 0; |
|
| 140 | 6 | $bindingParams = array(); |
|
| 141 | 6 | $paramDescriptions = array(); |
|
| 142 | |||
| 143 | 6 | $this->parseParamDescriptions($args, $paramDescriptions); |
|
| 144 | 6 | $this->parseParams($args, $bindingParams); |
|
| 145 | |||
| 146 | 6 | $this->discoveryManager->addRootTypeDescriptor(new BindingTypeDescriptor( |
|
| 147 | 6 | new BindingType($args->getArgument('name'), $bindingParams), |
|
| 148 | 6 | $args->getOption('description'), |
|
| 149 | $paramDescriptions |
||
| 150 | ), $flags); |
||
| 151 | |||
| 152 | 6 | return 0; |
|
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Handles the "puli type --update" command. |
||
| 157 | * |
||
| 158 | * @param Args $args The console arguments |
||
| 159 | * |
||
| 160 | * @return int The status code |
||
| 161 | */ |
||
| 162 | 6 | public function handleUpdate(Args $args) |
|
| 192 | |||
| 193 | /** |
||
| 194 | * Handles the "puli type --delete" command. |
||
| 195 | * |
||
| 196 | * @param Args $args The console arguments |
||
| 197 | * |
||
| 198 | * @return int The status code |
||
| 199 | */ |
||
| 200 | 2 | public function handleDelete(Args $args) |
|
| 216 | |||
| 217 | /** |
||
| 218 | * Returns the binding type states selected in the console arguments. |
||
| 219 | * |
||
| 220 | * @param Args $args The console arguments |
||
| 221 | * |
||
| 222 | * @return int[] A list of {@link BindingTypeState} constants |
||
| 223 | */ |
||
| 224 | 11 | private function getBindingTypeStates(Args $args) |
|
| 225 | { |
||
| 226 | 11 | $states = array(); |
|
| 227 | |||
| 228 | 11 | if ($args->isOptionSet('enabled')) { |
|
| 229 | 4 | $states[] = BindingTypeState::ENABLED; |
|
| 230 | } |
||
| 231 | |||
| 232 | 11 | if ($args->isOptionSet('duplicate')) { |
|
| 233 | 2 | $states[] = BindingTypeState::DUPLICATE; |
|
| 234 | } |
||
| 235 | |||
| 236 | 11 | return $states ?: BindingTypeState::all(); |
|
| 237 | } |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Prints the binding types in a table. |
||
| 241 | * |
||
| 242 | * @param IO $io The I/O |
||
| 243 | * @param BindingTypeDescriptor[] $descriptors The type descriptors to print |
||
| 244 | * @param string $styleTag The tag used to style the output |
||
| 245 | * @param int $indentation The number of spaces to indent |
||
| 246 | */ |
||
| 247 | 10 | private function printTypeTable(IO $io, array $descriptors, $styleTag = null, $indentation = 0) |
|
| 248 | { |
||
| 249 | 10 | $table = new Table(PuliTableStyle::borderless()); |
|
| 250 | |||
| 251 | 10 | $table->setHeaderRow(array('Type', 'Description', 'Parameters')); |
|
| 252 | |||
| 253 | 10 | $paramTag = $styleTag ?: 'c1'; |
|
| 254 | 10 | $typeTag = $styleTag ?: 'u'; |
|
| 255 | |||
| 256 | 10 | foreach ($descriptors as $descriptor) { |
|
| 257 | 10 | $type = $descriptor->getType(); |
|
| 258 | 10 | $parameters = array(); |
|
| 259 | |||
| 260 | 10 | foreach ($type->getParameters() as $parameter) { |
|
| 261 | 6 | $paramString = $parameter->isRequired() |
|
| 262 | 6 | ? $parameter->getName() |
|
| 263 | 6 | : $parameter->getName().'='.StringUtil::formatValue($parameter->getDefaultValue()); |
|
| 264 | |||
| 265 | 6 | $parameters[$parameter->getName()] = "<$paramTag>$paramString</$paramTag>"; |
|
| 266 | } |
||
| 267 | |||
| 268 | 10 | $description = $descriptor->getDescription(); |
|
| 269 | |||
| 270 | 10 | if ($styleTag) { |
|
| 271 | 7 | $description = "<$styleTag>$description</$styleTag>"; |
|
| 272 | } |
||
| 273 | |||
| 274 | 10 | ksort($parameters); |
|
| 275 | |||
| 276 | 10 | $table->addRow(array( |
|
| 277 | 10 | "<$typeTag>".$descriptor->getTypeName()."</$typeTag>", |
|
| 278 | 10 | $description, |
|
| 279 | 10 | implode("\n", $parameters), |
|
| 280 | )); |
||
| 281 | } |
||
| 282 | |||
| 283 | 10 | $table->render($io, $indentation); |
|
| 284 | 10 | } |
|
| 285 | |||
| 286 | /** |
||
| 287 | * Prints the heading for a binding type state. |
||
| 288 | * |
||
| 289 | * @param IO $io The I/O |
||
| 290 | * @param int $typeState The {@link BindingTypeState} constant |
||
| 291 | */ |
||
| 292 | 6 | private function printBindingTypeState(IO $io, $typeState) |
|
| 293 | { |
||
| 294 | switch ($typeState) { |
||
| 295 | 6 | case BindingTypeState::ENABLED: |
|
| 296 | 6 | $io->writeLine('The following binding types are currently enabled:'); |
|
| 297 | 6 | $io->writeLine(''); |
|
| 298 | |||
| 299 | 6 | return; |
|
| 300 | 6 | case BindingTypeState::DUPLICATE: |
|
| 301 | 6 | $io->writeLine('The following types have duplicate definitions and are disabled:'); |
|
| 302 | 6 | $io->writeLine(''); |
|
| 303 | |||
| 304 | 6 | return; |
|
| 305 | } |
||
| 306 | } |
||
| 307 | |||
| 308 | 12 | View Code Duplication | private function parseParamDescriptions(Args $args, array &$paramDescriptions) |
| 309 | { |
||
| 310 | 12 | foreach ($args->getOption('param-description') as $paramDescription) { |
|
| 311 | 2 | $pos = strpos($paramDescription, '='); |
|
| 312 | |||
| 313 | 2 | if (false === $pos) { |
|
| 314 | throw new RuntimeException(sprintf( |
||
| 315 | 'The "--param-description" option expects a parameter in '. |
||
| 316 | 'the form "key=value". Got: "%s"', |
||
| 317 | $paramDescription |
||
| 318 | )); |
||
| 319 | } |
||
| 320 | |||
| 321 | 2 | $key = substr($paramDescription, 0, $pos); |
|
| 322 | 2 | $paramDescriptions[$key] = StringUtil::parseValue(substr($paramDescription, $pos + 1)); |
|
| 323 | } |
||
| 324 | 12 | } |
|
| 325 | |||
| 326 | 12 | private function parseParams(Args $args, array &$bindingParams) |
|
| 350 | |||
| 351 | 6 | private function parseUnsetParams(Args $args, array &$bindingParams, array &$paramDescriptions) |
|
| 358 | |||
| 359 | 6 | private function typesEqual(BindingTypeDescriptor $descriptor1, BindingTypeDescriptor $descriptor2) |
|
| 366 | } |
||
| 367 |
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.