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 ModuleLoader 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 ModuleLoader, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | class ModuleLoader |
||
| 21 | { |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Define the default main module |
||
| 25 | */ |
||
| 26 | const DEFAULT_MAIN_MODULE = 'content'; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Define the default pid |
||
| 30 | */ |
||
| 31 | const DEFAULT_PID = 0; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * The type of data being listed (which corresponds to a table name in TCA) |
||
| 35 | * |
||
| 36 | * @var string |
||
| 37 | */ |
||
| 38 | protected $dataType; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var string |
||
| 42 | */ |
||
| 43 | protected $defaultPid; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var bool |
||
| 47 | */ |
||
| 48 | protected $showPageTree = false; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var bool |
||
| 52 | */ |
||
| 53 | protected $isShown = true; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var string |
||
| 57 | */ |
||
| 58 | protected $access; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var string |
||
| 62 | */ |
||
| 63 | protected $mainModule; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var string |
||
| 67 | */ |
||
| 68 | protected $position = ''; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @var string |
||
| 72 | */ |
||
| 73 | protected $icon; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @var string |
||
| 77 | */ |
||
| 78 | protected $moduleLanguageFile; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * The module key such as m1, m2. |
||
| 82 | * |
||
| 83 | * @var string |
||
| 84 | */ |
||
| 85 | protected $moduleKey = 'm1'; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @var string[] |
||
| 89 | */ |
||
| 90 | protected $additionalJavaScriptFiles = []; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @var string[] |
||
| 94 | */ |
||
| 95 | protected $additionalStyleSheetFiles = []; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @var array |
||
| 99 | */ |
||
| 100 | protected $components = []; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @param string $dataType |
||
| 104 | */ |
||
| 105 | public function __construct($dataType = null) |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Tell whether a module is already registered. |
||
| 151 | * |
||
| 152 | * @param string $dataType |
||
| 153 | * @return bool |
||
| 154 | */ |
||
| 155 | public function isRegistered($dataType): bool |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @return array |
||
| 163 | */ |
||
| 164 | protected function getExistingInternalConfiguration(): array |
||
| 171 | |||
| 172 | /** |
||
| 173 | * @return array |
||
| 174 | */ |
||
| 175 | protected function getExistingMainConfiguration(): array |
||
| 180 | |||
| 181 | /** |
||
| 182 | * @return string |
||
| 183 | */ |
||
| 184 | protected function computeMainModule(): string |
||
| 197 | |||
| 198 | /** |
||
| 199 | * @return string |
||
| 200 | */ |
||
| 201 | protected function computeDefaultPid(): string |
||
| 214 | |||
| 215 | /** |
||
| 216 | * @return array |
||
| 217 | */ |
||
| 218 | View Code Duplication | protected function computeAdditionalJavaScriptFiles(): array |
|
| 230 | |||
| 231 | /** |
||
| 232 | * @return array |
||
| 233 | */ |
||
| 234 | View Code Duplication | protected function computeAdditionalStyleSheetFiles(): array |
|
| 246 | |||
| 247 | /** |
||
| 248 | * @return array |
||
| 249 | */ |
||
| 250 | protected function computeComponents(): array |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Register the module in two places: core + vidi internal. |
||
| 258 | * |
||
| 259 | * @return void |
||
| 260 | * @throws \InvalidArgumentException |
||
| 261 | */ |
||
| 262 | public function register(): void |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Return the module code for a BE module. |
||
| 316 | * |
||
| 317 | * @return string |
||
| 318 | */ |
||
| 319 | public function getSignature(): string |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Returns the current pid. |
||
| 328 | * |
||
| 329 | * @return bool |
||
| 330 | */ |
||
| 331 | public function getCurrentPid(): bool |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Return the module URL. |
||
| 338 | * |
||
| 339 | * @param array $additionalParameters |
||
| 340 | * @return string |
||
| 341 | */ |
||
| 342 | public function getModuleUrl(array $additionalParameters = []): string |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Return the parameter prefix for a BE module. |
||
| 356 | * |
||
| 357 | * @return string |
||
| 358 | */ |
||
| 359 | public function getParameterPrefix(): string |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Return a configuration key or the entire module configuration array if not key is given. |
||
| 366 | * |
||
| 367 | * @param string $key |
||
| 368 | * @throws InvalidKeyInArrayException |
||
| 369 | * @return mixed |
||
| 370 | */ |
||
| 371 | public function getModuleConfiguration($key = '') |
||
| 395 | |||
| 396 | /** |
||
| 397 | * @param string $icon |
||
| 398 | * @return $this |
||
| 399 | */ |
||
| 400 | public function setIcon($icon): self |
||
| 405 | |||
| 406 | /** |
||
| 407 | * @return string |
||
| 408 | */ |
||
| 409 | View Code Duplication | protected function getIcon(): string |
|
| 424 | |||
| 425 | /** |
||
| 426 | * @param string $mainModule |
||
| 427 | * @return $this |
||
| 428 | */ |
||
| 429 | public function setMainModule($mainModule): self |
||
| 434 | |||
| 435 | /** |
||
| 436 | * @return string |
||
| 437 | * @throws \Fab\Vidi\Exception\InvalidKeyInArrayException |
||
| 438 | */ |
||
| 439 | public function getMainModule(): string |
||
| 446 | |||
| 447 | /** |
||
| 448 | * @param string $moduleLanguageFile |
||
| 449 | * @return $this |
||
| 450 | */ |
||
| 451 | public function setModuleLanguageFile($moduleLanguageFile): self |
||
| 456 | |||
| 457 | /** |
||
| 458 | * @param string $component |
||
| 459 | * @return $this |
||
| 460 | */ |
||
| 461 | public function removeComponentFromDocHeader(string $component): self |
||
| 476 | |||
| 477 | /** |
||
| 478 | * @param string $component |
||
| 479 | * @return bool |
||
| 480 | */ |
||
| 481 | public function hasComponentInDocHeader(string $component): bool |
||
| 494 | |||
| 495 | /** |
||
| 496 | * @return string |
||
| 497 | */ |
||
| 498 | protected function getModuleLanguageFile(): string |
||
| 513 | |||
| 514 | /** |
||
| 515 | * @param string $position |
||
| 516 | * @return $this |
||
| 517 | */ |
||
| 518 | public function setPosition($position): self |
||
| 523 | |||
| 524 | /** |
||
| 525 | * @return string |
||
| 526 | */ |
||
| 527 | public function getPosition(): string |
||
| 531 | |||
| 532 | /** |
||
| 533 | * @param array $files |
||
| 534 | * @return $this |
||
| 535 | */ |
||
| 536 | public function addJavaScriptFiles(array $files): self |
||
| 543 | |||
| 544 | /** |
||
| 545 | * @param string $fileNameAndPath |
||
| 546 | * @return $this |
||
| 547 | */ |
||
| 548 | public function addJavaScriptFile($fileNameAndPath): self |
||
| 553 | |||
| 554 | /** |
||
| 555 | * @param array $files |
||
| 556 | * @return $this |
||
| 557 | */ |
||
| 558 | public function addStyleSheetFiles(array $files): self |
||
| 565 | |||
| 566 | /** |
||
| 567 | * @param string $fileNameAndPath |
||
| 568 | * @return $this |
||
| 569 | */ |
||
| 570 | public function addStyleSheetFile($fileNameAndPath): self |
||
| 575 | |||
| 576 | /** |
||
| 577 | * @return string |
||
| 578 | * @throws \Fab\Vidi\Exception\InvalidKeyInArrayException |
||
| 579 | */ |
||
| 580 | public function getDataType(): string |
||
| 587 | |||
| 588 | /** |
||
| 589 | * @return array |
||
| 590 | */ |
||
| 591 | public function getDataTypes(): array |
||
| 599 | |||
| 600 | /** |
||
| 601 | * @param string $dataType |
||
| 602 | * @return $this |
||
| 603 | */ |
||
| 604 | public function setDataType($dataType): self |
||
| 609 | |||
| 610 | /** |
||
| 611 | * @return string |
||
| 612 | * @throws \Fab\Vidi\Exception\InvalidKeyInArrayException |
||
| 613 | */ |
||
| 614 | public function getDefaultPid(): string |
||
| 621 | |||
| 622 | /** |
||
| 623 | * @param string $defaultPid |
||
| 624 | * @return $this |
||
| 625 | */ |
||
| 626 | public function setDefaultPid($defaultPid): self |
||
| 631 | |||
| 632 | /** |
||
| 633 | * @param bool $isPageTreeShown |
||
| 634 | * @return $this |
||
| 635 | */ |
||
| 636 | public function showPageTree($isPageTreeShown): self |
||
| 641 | |||
| 642 | /** |
||
| 643 | * @param string $isShown |
||
| 644 | * @return $this |
||
| 645 | */ |
||
| 646 | public function isShown($isShown): self |
||
| 651 | |||
| 652 | /** |
||
| 653 | * @return $array |
||
| 654 | * @throws \Fab\Vidi\Exception\InvalidKeyInArrayException |
||
| 655 | */ |
||
| 656 | public function getDocHeaderTopLeftComponents() |
||
| 661 | |||
| 662 | /** |
||
| 663 | * @param array $components |
||
| 664 | * @return $this |
||
| 665 | */ |
||
| 666 | public function setDocHeaderTopLeftComponents(array $components): self |
||
| 671 | |||
| 672 | /** |
||
| 673 | * @param string|array $components |
||
| 674 | * @return $this |
||
| 675 | */ |
||
| 676 | View Code Duplication | public function addDocHeaderTopLeftComponents($components): self |
|
| 685 | |||
| 686 | /** |
||
| 687 | * @return $array |
||
| 688 | * @throws \Fab\Vidi\Exception\InvalidKeyInArrayException |
||
| 689 | */ |
||
| 690 | public function getDocHeaderTopRightComponents() |
||
| 695 | |||
| 696 | /** |
||
| 697 | * @param array $components |
||
| 698 | * @return $this |
||
| 699 | */ |
||
| 700 | public function setDocHeaderTopRightComponents(array $components): self |
||
| 705 | |||
| 706 | /** |
||
| 707 | * @param string|array $components |
||
| 708 | * @return $this |
||
| 709 | */ |
||
| 710 | View Code Duplication | public function addDocHeaderTopRightComponents($components): self |
|
| 719 | |||
| 720 | /** |
||
| 721 | * @return $array |
||
| 722 | * @throws \Fab\Vidi\Exception\InvalidKeyInArrayException |
||
| 723 | */ |
||
| 724 | public function getDocHeaderBottomLeftComponents() |
||
| 729 | |||
| 730 | /** |
||
| 731 | * @param array $components |
||
| 732 | * @return $this |
||
| 733 | */ |
||
| 734 | public function setDocHeaderBottomLeftComponents(array $components): self |
||
| 739 | |||
| 740 | /** |
||
| 741 | * @param string|array $components |
||
| 742 | * @return $this |
||
| 743 | */ |
||
| 744 | View Code Duplication | public function addDocHeaderBottomLeftComponents($components): self |
|
| 753 | |||
| 754 | /** |
||
| 755 | * @return $array |
||
| 756 | * @throws \Fab\Vidi\Exception\InvalidKeyInArrayException |
||
| 757 | */ |
||
| 758 | public function getDocHeaderBottomRightComponents() |
||
| 763 | |||
| 764 | /** |
||
| 765 | * @param array $components |
||
| 766 | * @return $this |
||
| 767 | */ |
||
| 768 | public function setDocHeaderBottomRightComponents(array $components): self |
||
| 773 | |||
| 774 | /** |
||
| 775 | * @param string|array $components |
||
| 776 | * @return $this |
||
| 777 | */ |
||
| 778 | View Code Duplication | public function addDocHeaderBottomRightComponents($components): self |
|
| 787 | |||
| 788 | /** |
||
| 789 | * @return $array |
||
| 790 | * @throws \Fab\Vidi\Exception\InvalidKeyInArrayException |
||
| 791 | */ |
||
| 792 | public function getGridTopComponents() |
||
| 797 | |||
| 798 | /** |
||
| 799 | * @param array $components |
||
| 800 | * @return $this |
||
| 801 | */ |
||
| 802 | public function setGridTopComponents(array $components): self |
||
| 807 | |||
| 808 | /** |
||
| 809 | * @param string|array $components |
||
| 810 | * @return $this |
||
| 811 | */ |
||
| 812 | View Code Duplication | public function addGridTopComponents($components): self |
|
| 821 | |||
| 822 | /** |
||
| 823 | * @return $array |
||
| 824 | * @throws \Fab\Vidi\Exception\InvalidKeyInArrayException |
||
| 825 | */ |
||
| 826 | public function getGridBottomComponents() |
||
| 831 | |||
| 832 | /** |
||
| 833 | * @param array $components |
||
| 834 | * @return $this |
||
| 835 | */ |
||
| 836 | public function setGridBottomComponents(array $components): self |
||
| 841 | |||
| 842 | /** |
||
| 843 | * @param string|array $components |
||
| 844 | * @return $this |
||
| 845 | */ |
||
| 846 | View Code Duplication | public function addGridBottomComponents($components): self |
|
| 855 | |||
| 856 | /** |
||
| 857 | * @return $array |
||
| 858 | * @throws \Fab\Vidi\Exception\InvalidKeyInArrayException |
||
| 859 | */ |
||
| 860 | public function getGridButtonsComponents() |
||
| 865 | |||
| 866 | /** |
||
| 867 | * @param array $components |
||
| 868 | * @return $this |
||
| 869 | */ |
||
| 870 | public function setGridButtonsComponents(array $components): self |
||
| 875 | |||
| 876 | /** |
||
| 877 | * @param string|array $components |
||
| 878 | * @return $this |
||
| 879 | */ |
||
| 880 | View Code Duplication | public function addGridButtonsComponents($components): self |
|
| 889 | |||
| 890 | /** |
||
| 891 | * @return $array |
||
| 892 | * @throws \Fab\Vidi\Exception\InvalidKeyInArrayException |
||
| 893 | */ |
||
| 894 | public function getMenuMassActionComponents() |
||
| 899 | |||
| 900 | /** |
||
| 901 | * @param array $components |
||
| 902 | * @return $this |
||
| 903 | */ |
||
| 904 | public function setMenuMassActionComponents(array $components): self |
||
| 909 | |||
| 910 | /** |
||
| 911 | * @param string|array $components |
||
| 912 | * @return $this |
||
| 913 | */ |
||
| 914 | public function addMenuMassActionComponents($components): self |
||
| 923 | |||
| 924 | /** |
||
| 925 | * @return string |
||
| 926 | */ |
||
| 927 | View Code Duplication | protected function getAccess(): string |
|
| 940 | |||
| 941 | /** |
||
| 942 | * @param string $access |
||
| 943 | * @return $this |
||
| 944 | */ |
||
| 945 | public function setAccess($access): self |
||
| 950 | |||
| 951 | /** |
||
| 952 | * @return \string[] |
||
| 953 | * @throws \Fab\Vidi\Exception\InvalidKeyInArrayException |
||
| 954 | */ |
||
| 955 | public function getAdditionalJavaScriptFiles(): array |
||
| 962 | |||
| 963 | /** |
||
| 964 | * @return \string[] |
||
| 965 | * @throws \Fab\Vidi\Exception\InvalidKeyInArrayException |
||
| 966 | */ |
||
| 967 | public function getAdditionalStyleSheetFiles(): array |
||
| 974 | |||
| 975 | /** |
||
| 976 | * @return array |
||
| 977 | */ |
||
| 978 | public function getComponents(): array |
||
| 982 | |||
| 983 | /** |
||
| 984 | * @param string $pluginName |
||
| 985 | * @return bool |
||
| 986 | */ |
||
| 987 | public function hasPlugin($pluginName = ''): bool |
||
| 998 | |||
| 999 | /** |
||
| 1000 | * Compute the internal module code |
||
| 1001 | * |
||
| 1002 | * @param null|string $dataType |
||
| 1003 | * @return string |
||
| 1004 | */ |
||
| 1005 | protected function getInternalModuleSignature($dataType = null): string |
||
| 1016 | |||
| 1017 | } |
||
| 1018 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.