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 MetaManipulator 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 MetaManipulator, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | class MetaManipulator |
||
| 22 | { |
||
| 23 | const META_TITLE = 1; |
||
| 24 | const META_DESCRIPTION = 2; |
||
| 25 | const META_KEYWORDS = 3; |
||
| 26 | const META_H1 = 4; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Currency |
||
| 30 | * @var string |
||
| 31 | */ |
||
| 32 | protected $CS; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * ID |
||
| 36 | * @var string |
||
| 37 | */ |
||
| 38 | protected $ID; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Brand |
||
| 42 | * @var string |
||
| 43 | */ |
||
| 44 | protected $brand; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Category |
||
| 48 | * @var string |
||
| 49 | */ |
||
| 50 | protected $category; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var int |
||
| 54 | */ |
||
| 55 | protected $descLength; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var string |
||
| 59 | */ |
||
| 60 | protected $description; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var array |
||
| 64 | */ |
||
| 65 | protected $matching = []; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var array |
||
| 69 | */ |
||
| 70 | protected $metaArray = []; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var string |
||
| 74 | */ |
||
| 75 | protected $metaDescription; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var string |
||
| 79 | */ |
||
| 80 | protected $metaH1; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @var string |
||
| 84 | */ |
||
| 85 | protected $metaKeywords; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @var string |
||
| 89 | */ |
||
| 90 | protected $metaTitle; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @var SCategory|SBrands|SProducts|array|null |
||
| 94 | */ |
||
| 95 | protected $model; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @var array |
||
| 99 | */ |
||
| 100 | protected $morph = []; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @var string |
||
| 104 | */ |
||
| 105 | protected $name; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @var int |
||
| 109 | */ |
||
| 110 | protected $number; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @var string |
||
| 114 | */ |
||
| 115 | protected $pageNumber; |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Price |
||
| 119 | * @var string |
||
| 120 | */ |
||
| 121 | protected $price; |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @var MetaStorage |
||
| 125 | */ |
||
| 126 | protected $storage; |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @var string |
||
| 130 | */ |
||
| 131 | protected $wrapper = '%'; |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @var array |
||
| 135 | */ |
||
| 136 | private $grammens = [ |
||
| 137 | 'ИМ', |
||
| 138 | 'РД', |
||
| 139 | 'ДТ', |
||
| 140 | 'ВН', |
||
| 141 | 'ТВ', |
||
| 142 | 'ПР', |
||
| 143 | ]; |
||
| 144 | |||
| 145 | /** |
||
| 146 | * method processing meta data |
||
| 147 | * @var phpMorphy |
||
| 148 | */ |
||
| 149 | private $phpMorphy; |
||
| 150 | |||
| 151 | /** |
||
| 152 | * MetaManipulator constructor. |
||
| 153 | * @param SBrands|SCategory|SProducts|array $model |
||
| 154 | * @param MetaStorage $storage |
||
| 155 | * @throws Exception |
||
| 156 | */ |
||
| 157 | public function __construct($model, MetaStorage $storage) { |
||
| 195 | |||
| 196 | /** |
||
| 197 | * @return array |
||
| 198 | */ |
||
| 199 | public function getGrammens() { |
||
| 203 | |||
| 204 | /** |
||
| 205 | * @return MetaStorage |
||
| 206 | */ |
||
| 207 | public function getStorage() { |
||
| 211 | |||
| 212 | /** |
||
| 213 | * @param MetaStorage $storage |
||
| 214 | */ |
||
| 215 | public function setStorage($storage) { |
||
| 219 | |||
| 220 | /** |
||
| 221 | * @param string $name |
||
| 222 | * @param string $arguments |
||
| 223 | * @return string |
||
| 224 | */ |
||
| 225 | public function __call($name, $arguments) { |
||
| 233 | |||
| 234 | /** |
||
| 235 | * @param string $string |
||
| 236 | * @param string $prev_string |
||
| 237 | * @return null|string |
||
| 238 | */ |
||
| 239 | public function make($string, $prev_string) { |
||
| 260 | |||
| 261 | /** |
||
| 262 | * @param string $string |
||
| 263 | * @param int $part 1-6 |
||
| 264 | * @return string|null |
||
| 265 | */ |
||
| 266 | protected function morphing($string, $part) { |
||
| 308 | |||
| 309 | /** |
||
| 310 | * @param phpMorphy_Paradigm_ParadigmInterface $paradigm |
||
| 311 | * @param string $param |
||
| 312 | * @param null|string $grammar |
||
| 313 | * @return array |
||
| 314 | */ |
||
| 315 | private function getGrammensWord($paradigm, $param, $grammar = null) { |
||
| 342 | |||
| 343 | /** |
||
| 344 | * @param string $word |
||
| 345 | * @return array |
||
| 346 | */ |
||
| 347 | private function getTypeMany($word) { |
||
| 362 | |||
| 363 | /** |
||
| 364 | * @param string $word |
||
| 365 | * @param string $param |
||
| 366 | * @return array |
||
| 367 | */ |
||
| 368 | private function getTypeSpeechWord($word, $param) { |
||
| 388 | |||
| 389 | /** |
||
| 390 | * @param phpMorphy_Paradigm_ParadigmInterface $paradigm |
||
| 391 | * @param array $param |
||
| 392 | * @param string $word |
||
| 393 | * @return string|null |
||
| 394 | */ |
||
| 395 | private function checkGrammar($paradigm, $param, $word) { |
||
| 418 | |||
| 419 | /** |
||
| 420 | * @param phpMorphy $phpMorphy |
||
| 421 | */ |
||
| 422 | private function setPhpMorphy($phpMorphy) { |
||
| 426 | |||
| 427 | /** |
||
| 428 | * @return phpMorphy |
||
| 429 | */ |
||
| 430 | private function getPhpMorphy() { |
||
| 434 | |||
| 435 | /** |
||
| 436 | * @param string $string |
||
| 437 | * @param integer $part |
||
| 438 | * @param bool $ucFirst |
||
| 439 | * @return array |
||
| 440 | */ |
||
| 441 | public function getMorph($string, $part, $ucFirst = false) { |
||
| 448 | |||
| 449 | /** |
||
| 450 | * @param string $string |
||
| 451 | * @param array $morph |
||
| 452 | */ |
||
| 453 | public function setMorph($string, $morph) { |
||
| 457 | |||
| 458 | /** |
||
| 459 | * @param string $string |
||
| 460 | * @return string |
||
| 461 | */ |
||
| 462 | protected function transliteration($string = '') { |
||
| 468 | |||
| 469 | /** |
||
| 470 | * @return string |
||
| 471 | */ |
||
| 472 | public function getBrand() { |
||
| 476 | |||
| 477 | /** |
||
| 478 | * @param string $brand |
||
| 479 | */ |
||
| 480 | public function setBrand($brand) { |
||
| 484 | |||
| 485 | /** |
||
| 486 | * @return string |
||
| 487 | */ |
||
| 488 | public function getCS() { |
||
| 495 | |||
| 496 | /** |
||
| 497 | * @param string $CS |
||
| 498 | */ |
||
| 499 | public function setCS($CS) { |
||
| 503 | |||
| 504 | /** |
||
| 505 | * @return string |
||
| 506 | */ |
||
| 507 | public function getCategory() { |
||
| 511 | |||
| 512 | /** |
||
| 513 | * @param string $category |
||
| 514 | */ |
||
| 515 | public function setCategory($category) { |
||
| 519 | |||
| 520 | /** |
||
| 521 | * @return int |
||
| 522 | */ |
||
| 523 | public function getDescLength() { |
||
| 526 | |||
| 527 | /** |
||
| 528 | * @param int $descLength |
||
| 529 | */ |
||
| 530 | public function setDescLength($descLength) { |
||
| 538 | |||
| 539 | /** |
||
| 540 | * @return string |
||
| 541 | */ |
||
| 542 | public function getDescription() { |
||
| 546 | |||
| 547 | /** |
||
| 548 | * @param string $description |
||
| 549 | */ |
||
| 550 | public function setDescription($description) { |
||
| 562 | |||
| 563 | /** |
||
| 564 | * @return array|null|SBrands|SCategory|SProducts |
||
| 565 | */ |
||
| 566 | public function getModel() { |
||
| 570 | |||
| 571 | /** |
||
| 572 | * @param array|null|SBrands|SCategory|SProducts $model |
||
| 573 | */ |
||
| 574 | public function setModel($model) { |
||
| 578 | |||
| 579 | /** |
||
| 580 | * @return string |
||
| 581 | */ |
||
| 582 | public function getID() { |
||
| 586 | |||
| 587 | /** |
||
| 588 | * @param string $ID |
||
| 589 | */ |
||
| 590 | public function setID($ID) { |
||
| 594 | |||
| 595 | /** |
||
| 596 | * @return string |
||
| 597 | */ |
||
| 598 | public function getMetaDescription() { |
||
| 602 | |||
| 603 | /** |
||
| 604 | * @param string $metaDescription |
||
| 605 | */ |
||
| 606 | public function setMetaDescription($metaDescription) { |
||
| 610 | |||
| 611 | /** |
||
| 612 | * @return string |
||
| 613 | */ |
||
| 614 | public function getMetaH1() { |
||
| 618 | |||
| 619 | /** |
||
| 620 | * @param string $metaH1 |
||
| 621 | */ |
||
| 622 | public function setMetaH1($metaH1) { |
||
| 626 | |||
| 627 | /** |
||
| 628 | * @return string |
||
| 629 | */ |
||
| 630 | public function getMetaKeywords() { |
||
| 634 | |||
| 635 | /** |
||
| 636 | * @param string $metaKeywords |
||
| 637 | */ |
||
| 638 | public function setMetaKeywords($metaKeywords) { |
||
| 642 | |||
| 643 | /** |
||
| 644 | * @return string |
||
| 645 | */ |
||
| 646 | public function getMetaTitle() { |
||
| 650 | |||
| 651 | /** |
||
| 652 | * @param string $metaTitle |
||
| 653 | */ |
||
| 654 | public function setMetaTitle($metaTitle) { |
||
| 658 | |||
| 659 | /** |
||
| 660 | * @return string |
||
| 661 | */ |
||
| 662 | public function getName() { |
||
| 666 | |||
| 667 | /** |
||
| 668 | * @param string $name |
||
| 669 | */ |
||
| 670 | public function setName($name) { |
||
| 674 | |||
| 675 | /** |
||
| 676 | * @return string |
||
| 677 | */ |
||
| 678 | public function getPageNumber() { |
||
| 682 | |||
| 683 | /** |
||
| 684 | * @param string $pageNumber |
||
| 685 | */ |
||
| 686 | public function setPageNumber($pageNumber) { |
||
| 690 | |||
| 691 | /** |
||
| 692 | * @return string |
||
| 693 | */ |
||
| 694 | public function getNumber() { |
||
| 701 | |||
| 702 | /** |
||
| 703 | * @param string $number |
||
| 704 | */ |
||
| 705 | public function setNumber($number) { |
||
| 709 | |||
| 710 | /** |
||
| 711 | * @return string |
||
| 712 | */ |
||
| 713 | public function getPrice() { |
||
| 717 | |||
| 718 | /** |
||
| 719 | * @param string $price |
||
| 720 | */ |
||
| 721 | public function setPrice($price) { |
||
| 725 | |||
| 726 | /** |
||
| 727 | * @return array<String> |
||
| 728 | */ |
||
| 729 | public function render() { |
||
| 765 | |||
| 766 | /** |
||
| 767 | * @return string |
||
| 768 | */ |
||
| 769 | public function getWrapper() { |
||
| 773 | |||
| 774 | /** |
||
| 775 | * @param string $wrapper |
||
| 776 | */ |
||
| 777 | public function setWrapper($wrapper) { |
||
| 781 | |||
| 782 | /** |
||
| 783 | * @param string $key |
||
| 784 | * @return bool|array |
||
| 785 | */ |
||
| 786 | public function getMatching($key) { |
||
| 790 | |||
| 791 | /** |
||
| 792 | * @param string $key |
||
| 793 | * @param string $value |
||
| 794 | */ |
||
| 795 | public function setMatching($key, $value) { |
||
| 799 | |||
| 800 | /** |
||
| 801 | * @return array |
||
| 802 | */ |
||
| 803 | public function getMetaArray() { |
||
| 807 | |||
| 808 | /** |
||
| 809 | * @param array $metaArray |
||
| 810 | */ |
||
| 811 | public function setMetaArray($metaArray) { |
||
| 819 | |||
| 820 | } |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.