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 |
||
| 20 | class MetaManipulator |
||
| 21 | { |
||
|
|
|||
| 22 | const META_TITLE = 1; |
||
| 23 | const META_DESCRIPTION = 2; |
||
| 24 | const META_KEYWORDS = 3; |
||
| 25 | const META_H1 = 4; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Currency |
||
| 29 | * @var string |
||
| 30 | */ |
||
| 31 | protected $CS; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * ID |
||
| 35 | * @var string |
||
| 36 | */ |
||
| 37 | protected $ID; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Brand |
||
| 41 | * @var string |
||
| 42 | */ |
||
| 43 | protected $brand; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Category |
||
| 47 | * @var string |
||
| 48 | */ |
||
| 49 | protected $category; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var int |
||
| 53 | */ |
||
| 54 | protected $descLength; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var string |
||
| 58 | */ |
||
| 59 | protected $description; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var array |
||
| 63 | */ |
||
| 64 | protected $matching = []; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var array |
||
| 68 | */ |
||
| 69 | protected $metaArray = []; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var string |
||
| 73 | */ |
||
| 74 | protected $metaDescription; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var string |
||
| 78 | */ |
||
| 79 | protected $metaH1; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @var string |
||
| 83 | */ |
||
| 84 | protected $metaKeywords; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @var string |
||
| 88 | */ |
||
| 89 | protected $metaTitle; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @var SCategory|SBrands|SProducts|array|null |
||
| 93 | */ |
||
| 94 | protected $model; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @var array |
||
| 98 | */ |
||
| 99 | protected $morph = []; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @var string |
||
| 103 | */ |
||
| 104 | protected $name; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @var int |
||
| 108 | */ |
||
| 109 | protected $number; |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @var string |
||
| 113 | */ |
||
| 114 | protected $pageNumber; |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Price |
||
| 118 | * @var string |
||
| 119 | */ |
||
| 120 | protected $price; |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @var MetaStorage |
||
| 124 | */ |
||
| 125 | protected $storage; |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @var string |
||
| 129 | */ |
||
| 130 | protected $wrapper = '%'; |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @var array |
||
| 134 | */ |
||
| 135 | private $graments = ['ИМ', 'РД', 'ДТ', 'ВН', 'ТВ', 'ПР']; |
||
| 136 | |||
| 137 | /** |
||
| 138 | * method processing meta data |
||
| 139 | * @var |
||
| 140 | */ |
||
| 141 | private $phpMorphy; |
||
| 142 | |||
| 143 | /** |
||
| 144 | * MetaManipulator constructor. |
||
| 145 | * @param SBrands|SCategory|SProducts|array $model |
||
| 146 | * @param MetaStorage $storage |
||
| 147 | * @throws Exception |
||
| 148 | */ |
||
| 149 | public function __construct($model, MetaStorage $storage) { |
||
| 188 | |||
| 189 | /** |
||
| 190 | * @return array |
||
| 191 | */ |
||
| 192 | public function getGraments() { |
||
| 196 | |||
| 197 | /** |
||
| 198 | * @return MetaStorage |
||
| 199 | */ |
||
| 200 | public function getStorage() { |
||
| 204 | |||
| 205 | /** |
||
| 206 | * @param MetaStorage $storage |
||
| 207 | */ |
||
| 208 | public function setStorage($storage) { |
||
| 212 | |||
| 213 | /** |
||
| 214 | * @param string $name |
||
| 215 | * @param string $arguments |
||
| 216 | * @return string |
||
| 217 | */ |
||
| 218 | public function __call($name, $arguments) { |
||
| 226 | |||
| 227 | /** |
||
| 228 | * @param string $string |
||
| 229 | * @param string $prev_string |
||
| 230 | * @return null|string |
||
| 231 | */ |
||
| 232 | public function make($string, $prev_string) { |
||
| 253 | |||
| 254 | /** |
||
| 255 | * @param string $string |
||
| 256 | * @param int $part 1-6 |
||
| 257 | * @return string|null |
||
| 258 | */ |
||
| 259 | protected function morphing($string, $part) { |
||
| 301 | |||
| 302 | /** |
||
| 303 | * @param object $paradigm |
||
| 304 | * @param string $param |
||
| 305 | * @param null|string $gramat |
||
| 306 | * @return array |
||
| 307 | */ |
||
| 308 | private function getGrammensWord($paradigm, $param, $gramat = null) { |
||
| 331 | |||
| 332 | /** |
||
| 333 | * @param string $word |
||
| 334 | * @return array |
||
| 335 | */ |
||
| 336 | private function getTypeMany($word) { |
||
| 351 | |||
| 352 | /** |
||
| 353 | * @param string $word |
||
| 354 | * @param string $param |
||
| 355 | * @return array |
||
| 356 | */ |
||
| 357 | private function getTypeSpeechWord($word, $param) { |
||
| 377 | |||
| 378 | /** |
||
| 379 | * @param object $paradigm |
||
| 380 | * @param array $param |
||
| 381 | * @param string $word |
||
| 382 | * @return string|null |
||
| 383 | */ |
||
| 384 | private function checkGrammat($paradigm, $param, $word) { |
||
| 407 | |||
| 408 | /** |
||
| 409 | * @param phpMorphy $phpMorphy |
||
| 410 | */ |
||
| 411 | private function setPhpMorphy($phpMorphy) { |
||
| 415 | |||
| 416 | /** |
||
| 417 | * @return phpMorphy |
||
| 418 | */ |
||
| 419 | private function getPhpMorphy() { |
||
| 423 | |||
| 424 | /** |
||
| 425 | * @param string $string |
||
| 426 | * @param integer $part |
||
| 427 | * @param bool $ucFirst |
||
| 428 | * @return array |
||
| 429 | */ |
||
| 430 | public function getMorph($string, $part, $ucFirst = false) { |
||
| 437 | |||
| 438 | /** |
||
| 439 | * @param string $string |
||
| 440 | * @param array $morph |
||
| 441 | */ |
||
| 442 | public function setMorph($string, $morph) { |
||
| 446 | |||
| 447 | /** |
||
| 448 | * @param string $string |
||
| 449 | * @return string |
||
| 450 | */ |
||
| 451 | protected function transliteration($string = '') { |
||
| 457 | |||
| 458 | /** |
||
| 459 | * @return string |
||
| 460 | */ |
||
| 461 | public function getBrand() { |
||
| 465 | |||
| 466 | /** |
||
| 467 | * @param string $brand |
||
| 468 | */ |
||
| 469 | public function setBrand($brand) { |
||
| 473 | |||
| 474 | /** |
||
| 475 | * @return string |
||
| 476 | */ |
||
| 477 | public function getCS() { |
||
| 484 | |||
| 485 | /** |
||
| 486 | * @param string $CS |
||
| 487 | */ |
||
| 488 | public function setCS($CS) { |
||
| 492 | |||
| 493 | /** |
||
| 494 | * @return string |
||
| 495 | */ |
||
| 496 | public function getCategory() { |
||
| 500 | |||
| 501 | /** |
||
| 502 | * @param string $category |
||
| 503 | */ |
||
| 504 | public function setCategory($category) { |
||
| 508 | |||
| 509 | /** |
||
| 510 | * @return int |
||
| 511 | */ |
||
| 512 | public function getDescLength() { |
||
| 522 | |||
| 523 | /** |
||
| 524 | * @param int $descLength |
||
| 525 | */ |
||
| 526 | public function setDescLength($descLength) { |
||
| 532 | |||
| 533 | /** |
||
| 534 | * @return string |
||
| 535 | */ |
||
| 536 | public function getDescription() { |
||
| 543 | |||
| 544 | /** |
||
| 545 | * @param string $description |
||
| 546 | */ |
||
| 547 | public function setDescription($description) { |
||
| 559 | |||
| 560 | /** |
||
| 561 | * @return array|null|SBrands|SCategory|SProducts |
||
| 562 | */ |
||
| 563 | public function getModel() { |
||
| 567 | |||
| 568 | /** |
||
| 569 | * @param array|null|SBrands|SCategory|SProducts $model |
||
| 570 | */ |
||
| 571 | public function setModel($model) { |
||
| 575 | |||
| 576 | /** |
||
| 577 | * @return string |
||
| 578 | */ |
||
| 579 | 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() { |
||
| 669 | |||
| 670 | /** |
||
| 671 | * @param string $name |
||
| 672 | */ |
||
| 673 | public function setName($name) { |
||
| 677 | |||
| 678 | /** |
||
| 679 | * @return string |
||
| 680 | */ |
||
| 681 | public function getPageNumber() { |
||
| 685 | |||
| 686 | /** |
||
| 687 | * @param string $pageNumber |
||
| 688 | */ |
||
| 689 | public function setPageNumber($pageNumber) { |
||
| 693 | |||
| 694 | /** |
||
| 695 | * @return string |
||
| 696 | */ |
||
| 697 | public function getNumber() { |
||
| 704 | |||
| 705 | /** |
||
| 706 | * @param string $number |
||
| 707 | */ |
||
| 708 | public function setNumber($number) { |
||
| 712 | |||
| 713 | /** |
||
| 714 | * @return string |
||
| 715 | */ |
||
| 716 | public function getPrice() { |
||
| 720 | |||
| 721 | /** |
||
| 722 | * @param string $price |
||
| 723 | */ |
||
| 724 | public function setPrice($price) { |
||
| 728 | |||
| 729 | /** |
||
| 730 | * @return array<String> |
||
| 731 | */ |
||
| 732 | public function render() { |
||
| 768 | |||
| 769 | /** |
||
| 770 | * @return string |
||
| 771 | */ |
||
| 772 | public function getWrapper() { |
||
| 776 | |||
| 777 | /** |
||
| 778 | * @param string $wrapper |
||
| 779 | */ |
||
| 780 | public function setWrapper($wrapper) { |
||
| 784 | |||
| 785 | /** |
||
| 786 | * @param string $key |
||
| 787 | * @return bool|array |
||
| 788 | */ |
||
| 789 | public function getMatching($key) { |
||
| 793 | |||
| 794 | /** |
||
| 795 | * @param string $key |
||
| 796 | * @param string $value |
||
| 797 | */ |
||
| 798 | public function setMatching($key, $value) { |
||
| 802 | |||
| 803 | /** |
||
| 804 | * @return array |
||
| 805 | */ |
||
| 806 | public function getMetaArray() { |
||
| 810 | |||
| 811 | /** |
||
| 812 | * @param array $metaArray |
||
| 813 | */ |
||
| 814 | public function setMetaArray($metaArray) { |
||
| 822 | } |