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 ClassGenerator 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 ClassGenerator, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class ClassGenerator |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * @var ClassType |
||
| 19 | */ |
||
| 20 | protected $class; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var string |
||
| 24 | */ |
||
| 25 | protected $ownerCanonicalClassName; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @param string $value |
||
| 29 | * @return string |
||
| 30 | */ |
||
| 31 | protected static function camelCase(string $value): string |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @param string $type |
||
| 38 | * @return string |
||
| 39 | */ |
||
| 40 | protected function getTypeHint(string $type) |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @param string $type |
||
| 55 | * @return string |
||
| 56 | */ |
||
| 57 | protected function getCommentTypeHint(string $type) |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @return string |
||
| 76 | */ |
||
| 77 | protected function getCanonicalClassName(): string |
||
| 81 | |||
| 82 | /** |
||
| 83 | * ClassGenerator constructor. |
||
| 84 | * |
||
| 85 | * @param Config $annotation |
||
| 86 | * @param string $className |
||
| 87 | * @param string $classNamespace |
||
| 88 | * @param string $ownerCanonicalClassName |
||
| 89 | */ |
||
| 90 | public function __construct( |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @inheritDoc |
||
| 102 | */ |
||
| 103 | public function __toString() |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @param string $name |
||
| 133 | * @param string $type |
||
| 134 | * @param null $default |
||
| 135 | * @return ClassGenerator |
||
| 136 | */ |
||
| 137 | public function generateProperty(string $name, string $type, $default = null): ClassGenerator |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @param string $name |
||
| 149 | * @param string $type |
||
| 150 | * @return ClassGenerator |
||
| 151 | */ |
||
| 152 | View Code Duplication | public function generateGet(string $name, string $type): ClassGenerator |
|
| 168 | |||
| 169 | /** |
||
| 170 | * @param string $name |
||
| 171 | * @param string $type |
||
| 172 | * @return ClassGenerator |
||
| 173 | */ |
||
| 174 | public function generateSet(string $name, string $type): ClassGenerator |
||
| 189 | |||
| 190 | /** |
||
| 191 | * @param string $name |
||
| 192 | * @return ClassGenerator |
||
| 193 | */ |
||
| 194 | View Code Duplication | public function generateIsset(string $name): ClassGenerator |
|
| 206 | |||
| 207 | /** |
||
| 208 | * @param string $name |
||
| 209 | * @return ClassGenerator |
||
| 210 | */ |
||
| 211 | View Code Duplication | public function generateUnset(string $name): ClassGenerator |
|
| 224 | |||
| 225 | /** |
||
| 226 | * @param string $name |
||
| 227 | * @param string $type |
||
| 228 | * @return ClassGenerator |
||
| 229 | */ |
||
| 230 | View Code Duplication | public function generateListSet(string $name, string $type): ClassGenerator |
|
| 231 | { |
||
| 232 | $this->class |
||
| 233 | ->addMethod(static::camelCase('set_' . $name)) |
||
| 234 | ->addComment( |
||
| 235 | '@param ' . $this->getCommentTypeHint($type) . ' $values' . PHP_EOL . |
||
| 236 | '@return ' . $this->class->getName() |
||
| 237 | )->setReturnType($this->getCanonicalClassName()) |
||
| 238 | ->setBody( |
||
| 239 | '$this->' . static::camelCase('clear_' . $name) . '();' . PHP_EOL . |
||
| 240 | 'foreach ($values as $value) {' . PHP_EOL . |
||
| 241 | ' $this->' . static::camelCase('push_' . $name) . '($value);' . PHP_EOL . |
||
| 242 | '}' . PHP_EOL . |
||
| 243 | 'return $this;' |
||
| 244 | )->addParameter('values') |
||
| 245 | ->setTypeHint($this->getTypeHint($type)); |
||
| 246 | return $this; |
||
| 247 | } |
||
| 248 | |||
| 249 | /** |
||
| 250 | * @param string $name |
||
| 251 | * @param string $type |
||
| 252 | * @return ClassGenerator |
||
| 253 | */ |
||
| 254 | View Code Duplication | public function generateListGetAt(string $name, string $type): ClassGenerator |
|
| 255 | { |
||
| 256 | $this->class |
||
| 257 | ->addMethod(static::camelCase('get_' . $name . '_at')) |
||
| 258 | ->addComment( |
||
| 259 | '@param int $index' . PHP_EOL . |
||
| 260 | '@return ' . $this->getCommentTypeHint($type) |
||
| 261 | )->setReturnType($this->getTypeHint($type)) |
||
| 262 | ->setReturnNullable(true) |
||
| 263 | ->setBody( |
||
| 264 | 'if (isset($this->__' . $name . '__[0]) && array_key_exists($index, $this->__' . $name . '__[0])) {' . |
||
| 265 | PHP_EOL . ' return $this->__' . $name . '__[0][$index];' . PHP_EOL . |
||
| 266 | '}' . PHP_EOL . |
||
| 267 | 'return null;' |
||
| 268 | )->addParameter('index') |
||
| 269 | ->setTypeHint('int'); |
||
| 270 | return $this; |
||
| 271 | } |
||
| 272 | |||
| 273 | /** |
||
| 274 | * @param string $name |
||
| 275 | * @param string $type |
||
| 276 | * @return ClassGenerator |
||
| 277 | */ |
||
| 278 | public function generateListSetAt(string $name, string $type): ClassGenerator |
||
| 304 | |||
| 305 | /** |
||
| 306 | * @param string $name |
||
| 307 | * @param string $type |
||
| 308 | * @return ClassGenerator |
||
| 309 | */ |
||
| 310 | View Code Duplication | public function generateListPush(string $name, string $type): ClassGenerator |
|
| 328 | |||
| 329 | /** |
||
| 330 | * @param string $name |
||
| 331 | * @param string $type |
||
| 332 | * @return ClassGenerator |
||
| 333 | */ |
||
| 334 | View Code Duplication | public function generateListUnshift(string $name, string $type): ClassGenerator |
|
| 352 | |||
| 353 | /** |
||
| 354 | * @param string $name |
||
| 355 | * @param string $type |
||
| 356 | * @return ClassGenerator |
||
| 357 | */ |
||
| 358 | View Code Duplication | public function generateMapSet(string $name, string $type): ClassGenerator |
|
| 376 | |||
| 377 | /** |
||
| 378 | * @param string $name |
||
| 379 | * @param string $type |
||
| 380 | * @return ClassGenerator |
||
| 381 | */ |
||
| 382 | View Code Duplication | public function generateMapGetAt(string $name, string $type): ClassGenerator |
|
| 399 | |||
| 400 | /** |
||
| 401 | * @param string $name |
||
| 402 | * @param string $type |
||
| 403 | * @return ClassGenerator |
||
| 404 | */ |
||
| 405 | public function generateMapSetAt(string $name, string $type): ClassGenerator |
||
| 427 | |||
| 428 | /** |
||
| 429 | * @param string $name |
||
| 430 | * @param string $type |
||
| 431 | * @return ClassGenerator |
||
| 432 | */ |
||
| 433 | View Code Duplication | public function generateArrayPop(string $name, string $type): ClassGenerator |
|
| 449 | |||
| 450 | /** |
||
| 451 | * @param string $name |
||
| 452 | * @param string $type |
||
| 453 | * @return ClassGenerator |
||
| 454 | */ |
||
| 455 | View Code Duplication | public function generateArrayShift(string $name, string $type): ClassGenerator |
|
| 471 | |||
| 472 | /** |
||
| 473 | * @param string $name |
||
| 474 | * @return ClassGenerator |
||
| 475 | */ |
||
| 476 | View Code Duplication | public function generateArrayClear(string $name): ClassGenerator |
|
| 489 | |||
| 490 | /** |
||
| 491 | * @param string $name |
||
| 492 | * @param string $type |
||
| 493 | * @return ClassGenerator |
||
| 494 | */ |
||
| 495 | public function generateConfigGet(string $name, string $type): ClassGenerator |
||
| 511 | |||
| 512 | /** |
||
| 513 | * @param string $name |
||
| 514 | * @return ClassGenerator |
||
| 515 | */ |
||
| 516 | public function generateConfigSet(string $name): ClassGenerator |
||
| 529 | |||
| 530 | /** |
||
| 531 | * @param string $name |
||
| 532 | * @return ClassGenerator |
||
| 533 | */ |
||
| 534 | View Code Duplication | public function generateConfigIsset(string $name): ClassGenerator |
|
| 547 | |||
| 548 | /** |
||
| 549 | * @param string $name |
||
| 550 | * @return ClassGenerator |
||
| 551 | */ |
||
| 552 | public function generateConfigUnset(string $name): ClassGenerator |
||
| 565 | |||
| 566 | /** |
||
| 567 | * @return ClassGenerator |
||
| 568 | */ |
||
| 569 | View Code Duplication | public function generateMagicGet(): ClassGenerator |
|
| 601 | |||
| 602 | /** |
||
| 603 | * @return ClassGenerator |
||
| 604 | */ |
||
| 605 | public function generateMagicSet(): ClassGenerator |
||
| 642 | |||
| 643 | /** |
||
| 644 | * @return ClassGenerator |
||
| 645 | */ |
||
| 646 | View Code Duplication | public function generateMagicIsset(): ClassGenerator |
|
| 678 | |||
| 679 | /** |
||
| 680 | * @return ClassGenerator |
||
| 681 | */ |
||
| 682 | View Code Duplication | public function generateMagicUnset(): ClassGenerator |
|
| 714 | } |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.