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 |
||
| 26 | class ModuleLoader |
||
| 27 | { |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Define the default main module |
||
| 31 | */ |
||
| 32 | const DEFAULT_MAIN_MODULE = 'content'; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Define the default pid |
||
| 36 | */ |
||
| 37 | const DEFAULT_PID = 0; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * The type of data being listed (which corresponds to a table name in TCA) |
||
| 41 | * |
||
| 42 | * @var string |
||
| 43 | */ |
||
| 44 | protected $dataType; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var string |
||
| 48 | */ |
||
| 49 | protected $defaultPid; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var bool |
||
| 53 | */ |
||
| 54 | protected $showPageTree = false; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var bool |
||
| 58 | */ |
||
| 59 | protected $isShown = true; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var string |
||
| 63 | */ |
||
| 64 | protected $access; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var string |
||
| 68 | */ |
||
| 69 | protected $mainModule; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var string |
||
| 73 | */ |
||
| 74 | protected $position = ''; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var string |
||
| 78 | */ |
||
| 79 | protected $icon; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @var string |
||
| 83 | */ |
||
| 84 | protected $moduleLanguageFile; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * The module key such as m1, m2. |
||
| 88 | * |
||
| 89 | * @var string |
||
| 90 | */ |
||
| 91 | protected $moduleKey = 'm1'; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @var string[] |
||
| 95 | */ |
||
| 96 | protected $additionalJavaScriptFiles = []; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @var string[] |
||
| 100 | */ |
||
| 101 | protected $additionalStyleSheetFiles = []; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @var array |
||
| 105 | */ |
||
| 106 | protected $components = []; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @param string $dataType |
||
| 110 | */ |
||
| 111 | public function __construct($dataType = null) |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Tell whether a module is already registered. |
||
| 157 | * |
||
| 158 | * @param string $dataType |
||
| 159 | * @return bool |
||
| 160 | */ |
||
| 161 | public function isRegistered($dataType) |
||
| 166 | |||
| 167 | /** |
||
| 168 | * @return array |
||
| 169 | */ |
||
| 170 | protected function getExistingInternalConfiguration() |
||
| 177 | |||
| 178 | /** |
||
| 179 | * @return array |
||
| 180 | */ |
||
| 181 | protected function getExistingMainConfiguration() |
||
| 186 | |||
| 187 | /** |
||
| 188 | * @return string |
||
| 189 | */ |
||
| 190 | protected function computeMainModule() |
||
| 203 | |||
| 204 | /** |
||
| 205 | * @return string |
||
| 206 | */ |
||
| 207 | protected function computeDefaultPid() |
||
| 220 | |||
| 221 | /** |
||
| 222 | * @return array |
||
| 223 | */ |
||
| 224 | View Code Duplication | protected function computeAdditionalJavaScriptFiles() |
|
| 236 | |||
| 237 | /** |
||
| 238 | * @return array |
||
| 239 | */ |
||
| 240 | View Code Duplication | protected function computeAdditionalStyleSheetFiles() |
|
| 252 | |||
| 253 | /** |
||
| 254 | * @return array |
||
| 255 | */ |
||
| 256 | protected function computeComponents() |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Register the module in two places: core + vidi internal. |
||
| 264 | * |
||
| 265 | * @return void |
||
| 266 | * @throws \InvalidArgumentException |
||
| 267 | */ |
||
| 268 | public function register() |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Return the module code for a BE module. |
||
| 322 | * |
||
| 323 | * @return string |
||
| 324 | */ |
||
| 325 | public function getSignature() |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Tell whether the current module is the list one. |
||
| 332 | * |
||
| 333 | * @return bool |
||
| 334 | */ |
||
| 335 | public function copeWithPageTree() |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Returns the current pid. |
||
| 342 | * |
||
| 343 | * @return bool |
||
| 344 | */ |
||
| 345 | public function getCurrentPid() |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Return the Vidi module code which is stored in TBE_MODULES_EXT |
||
| 352 | * |
||
| 353 | * @return string |
||
| 354 | */ |
||
| 355 | public function getVidiModuleCode() |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Return the module URL. |
||
| 395 | * |
||
| 396 | * @param array $additionalParameters |
||
| 397 | * @param bool $absoluteUrl |
||
| 398 | * @return string |
||
| 399 | */ |
||
| 400 | public function getModuleUrl(array $additionalParameters = [], $absoluteUrl = false) |
||
| 417 | |||
| 418 | /** |
||
| 419 | * Return the module absolute URL. |
||
| 420 | * |
||
| 421 | * @param array $additionalParameters |
||
| 422 | * @return string |
||
| 423 | */ |
||
| 424 | public function getModuleAbsoluteUrl(array $additionalParameters = []) |
||
| 428 | |||
| 429 | /** |
||
| 430 | * Return the parameter prefix for a BE module. |
||
| 431 | * |
||
| 432 | * @return string |
||
| 433 | */ |
||
| 434 | public function getParameterPrefix() |
||
| 438 | |||
| 439 | /** |
||
| 440 | * Return a configuration key or the entire module configuration array if not key is given. |
||
| 441 | * |
||
| 442 | * @param string $key |
||
| 443 | * @throws InvalidKeyInArrayException |
||
| 444 | * @return mixed |
||
| 445 | */ |
||
| 446 | public function getModuleConfiguration($key = '') |
||
| 470 | |||
| 471 | /** |
||
| 472 | * @param string $icon |
||
| 473 | * @return $this |
||
| 474 | */ |
||
| 475 | public function setIcon($icon) |
||
| 480 | |||
| 481 | /** |
||
| 482 | * @return string |
||
| 483 | */ |
||
| 484 | View Code Duplication | protected function getIcon() |
|
| 499 | |||
| 500 | /** |
||
| 501 | * @param string $mainModule |
||
| 502 | * @return $this |
||
| 503 | */ |
||
| 504 | public function setMainModule($mainModule) |
||
| 509 | |||
| 510 | /** |
||
| 511 | * @return string |
||
| 512 | * @throws \Fab\Vidi\Exception\InvalidKeyInArrayException |
||
| 513 | */ |
||
| 514 | public function getMainModule() |
||
| 521 | |||
| 522 | /** |
||
| 523 | * @param string $moduleLanguageFile |
||
| 524 | * @return $this |
||
| 525 | */ |
||
| 526 | public function setModuleLanguageFile($moduleLanguageFile) |
||
| 531 | |||
| 532 | /** |
||
| 533 | * @return string |
||
| 534 | */ |
||
| 535 | View Code Duplication | protected function getModuleLanguageFile() |
|
| 548 | |||
| 549 | /** |
||
| 550 | * @param string $position |
||
| 551 | * @return $this |
||
| 552 | */ |
||
| 553 | public function setPosition($position) |
||
| 558 | |||
| 559 | /** |
||
| 560 | * @return string |
||
| 561 | */ |
||
| 562 | public function getPosition() |
||
| 566 | |||
| 567 | /** |
||
| 568 | * @param array $files |
||
| 569 | * @return $this |
||
| 570 | */ |
||
| 571 | public function addJavaScriptFiles(array $files) |
||
| 578 | |||
| 579 | /** |
||
| 580 | * @param string $fileNameAndPath |
||
| 581 | * @return $this |
||
| 582 | */ |
||
| 583 | public function addJavaScriptFile($fileNameAndPath) |
||
| 588 | |||
| 589 | /** |
||
| 590 | * @param array $files |
||
| 591 | * @return $this |
||
| 592 | */ |
||
| 593 | public function addStyleSheetFiles(array $files) |
||
| 600 | |||
| 601 | /** |
||
| 602 | * @param string $fileNameAndPath |
||
| 603 | * @return $this |
||
| 604 | */ |
||
| 605 | public function addStyleSheetFile($fileNameAndPath) |
||
| 610 | |||
| 611 | /** |
||
| 612 | * @return string |
||
| 613 | * @throws \Fab\Vidi\Exception\InvalidKeyInArrayException |
||
| 614 | */ |
||
| 615 | public function getDataType() |
||
| 622 | |||
| 623 | /** |
||
| 624 | * @return array |
||
| 625 | */ |
||
| 626 | public function getDataTypes() |
||
| 634 | |||
| 635 | /** |
||
| 636 | * @param string $dataType |
||
| 637 | * @return $this |
||
| 638 | */ |
||
| 639 | public function setDataType($dataType) |
||
| 644 | |||
| 645 | /** |
||
| 646 | * @return string |
||
| 647 | * @throws \Fab\Vidi\Exception\InvalidKeyInArrayException |
||
| 648 | */ |
||
| 649 | public function getDefaultPid() |
||
| 656 | |||
| 657 | /** |
||
| 658 | * @param string $defaultPid |
||
| 659 | * @return $this |
||
| 660 | */ |
||
| 661 | public function setDefaultPid($defaultPid) |
||
| 666 | |||
| 667 | /** |
||
| 668 | * @param bool $isPageTreeShown |
||
| 669 | * @return $this |
||
| 670 | */ |
||
| 671 | public function showPageTree($isPageTreeShown) |
||
| 676 | |||
| 677 | /** |
||
| 678 | * @param string $isShown |
||
| 679 | * @return $this |
||
| 680 | */ |
||
| 681 | public function isShown($isShown) |
||
| 686 | |||
| 687 | /** |
||
| 688 | * @return $array |
||
| 689 | * @throws \Fab\Vidi\Exception\InvalidKeyInArrayException |
||
| 690 | */ |
||
| 691 | public function getDocHeaderTopLeftComponents() |
||
| 696 | |||
| 697 | /** |
||
| 698 | * @param array $components |
||
| 699 | * @return $this |
||
| 700 | */ |
||
| 701 | public function setDocHeaderTopLeftComponents(array $components) |
||
| 706 | |||
| 707 | /** |
||
| 708 | * @param string|array $components |
||
| 709 | * @return $this |
||
| 710 | */ |
||
| 711 | View Code Duplication | public function addDocHeaderTopLeftComponents($components) |
|
| 720 | |||
| 721 | /** |
||
| 722 | * @return $array |
||
| 723 | * @throws \Fab\Vidi\Exception\InvalidKeyInArrayException |
||
| 724 | */ |
||
| 725 | public function getDocHeaderTopRightComponents() |
||
| 730 | |||
| 731 | /** |
||
| 732 | * @param array $components |
||
| 733 | * @return $this |
||
| 734 | */ |
||
| 735 | public function setDocHeaderTopRightComponents(array $components) |
||
| 740 | |||
| 741 | /** |
||
| 742 | * @param string|array $components |
||
| 743 | * @return $this |
||
| 744 | */ |
||
| 745 | View Code Duplication | public function addDocHeaderTopRightComponents($components) |
|
| 754 | |||
| 755 | /** |
||
| 756 | * @return $array |
||
| 757 | * @throws \Fab\Vidi\Exception\InvalidKeyInArrayException |
||
| 758 | */ |
||
| 759 | public function getDocHeaderBottomLeftComponents() |
||
| 764 | |||
| 765 | /** |
||
| 766 | * @param array $components |
||
| 767 | * @return $this |
||
| 768 | */ |
||
| 769 | public function setDocHeaderBottomLeftComponents(array $components) |
||
| 774 | |||
| 775 | /** |
||
| 776 | * @param string|array $components |
||
| 777 | * @return $this |
||
| 778 | */ |
||
| 779 | View Code Duplication | public function addDocHeaderBottomLeftComponents($components) |
|
| 788 | |||
| 789 | /** |
||
| 790 | * @return $array |
||
| 791 | * @throws \Fab\Vidi\Exception\InvalidKeyInArrayException |
||
| 792 | */ |
||
| 793 | public function getDocHeaderBottomRightComponents() |
||
| 798 | |||
| 799 | /** |
||
| 800 | * @param array $components |
||
| 801 | * @return $this |
||
| 802 | */ |
||
| 803 | public function setDocHeaderBottomRightComponents(array $components) |
||
| 808 | |||
| 809 | /** |
||
| 810 | * @param string|array $components |
||
| 811 | * @return $this |
||
| 812 | */ |
||
| 813 | View Code Duplication | public function addDocHeaderBottomRightComponents($components) |
|
| 822 | |||
| 823 | /** |
||
| 824 | * @return $array |
||
| 825 | * @throws \Fab\Vidi\Exception\InvalidKeyInArrayException |
||
| 826 | */ |
||
| 827 | public function getGridTopComponents() |
||
| 832 | |||
| 833 | /** |
||
| 834 | * @param array $components |
||
| 835 | * @return $this |
||
| 836 | */ |
||
| 837 | public function setGridTopComponents(array $components) |
||
| 842 | |||
| 843 | /** |
||
| 844 | * @param string|array $components |
||
| 845 | * @return $this |
||
| 846 | */ |
||
| 847 | View Code Duplication | public function addGridTopComponents($components) |
|
| 856 | |||
| 857 | /** |
||
| 858 | * @return $array |
||
| 859 | * @throws \Fab\Vidi\Exception\InvalidKeyInArrayException |
||
| 860 | */ |
||
| 861 | public function getGridBottomComponents() |
||
| 866 | |||
| 867 | /** |
||
| 868 | * @param array $components |
||
| 869 | * @return $this |
||
| 870 | */ |
||
| 871 | public function setGridBottomComponents(array $components) |
||
| 876 | |||
| 877 | /** |
||
| 878 | * @param string|array $components |
||
| 879 | * @return $this |
||
| 880 | */ |
||
| 881 | View Code Duplication | public function addGridBottomComponents($components) |
|
| 890 | |||
| 891 | /** |
||
| 892 | * @return $array |
||
| 893 | * @throws \Fab\Vidi\Exception\InvalidKeyInArrayException |
||
| 894 | */ |
||
| 895 | public function getGridButtonsComponents() |
||
| 900 | |||
| 901 | /** |
||
| 902 | * @param array $components |
||
| 903 | * @return $this |
||
| 904 | */ |
||
| 905 | public function setGridButtonsComponents(array $components) |
||
| 910 | |||
| 911 | /** |
||
| 912 | * @param string|array $components |
||
| 913 | * @return $this |
||
| 914 | */ |
||
| 915 | View Code Duplication | public function addGridButtonsComponents($components) |
|
| 924 | |||
| 925 | /** |
||
| 926 | * @return $array |
||
| 927 | * @throws \Fab\Vidi\Exception\InvalidKeyInArrayException |
||
| 928 | */ |
||
| 929 | public function getMenuMassActionComponents() |
||
| 934 | |||
| 935 | /** |
||
| 936 | * @param array $components |
||
| 937 | * @return $this |
||
| 938 | */ |
||
| 939 | public function setMenuMassActionComponents(array $components) |
||
| 944 | |||
| 945 | /** |
||
| 946 | * @param string|array $components |
||
| 947 | * @return $this |
||
| 948 | */ |
||
| 949 | public function addMenuMassActionComponents($components) |
||
| 958 | |||
| 959 | /** |
||
| 960 | * @return string |
||
| 961 | */ |
||
| 962 | View Code Duplication | protected function getAccess() |
|
| 975 | |||
| 976 | /** |
||
| 977 | * @param string $access |
||
| 978 | * @return $this |
||
| 979 | */ |
||
| 980 | public function setAccess($access) |
||
| 985 | |||
| 986 | /** |
||
| 987 | * @return \string[] |
||
| 988 | * @throws \Fab\Vidi\Exception\InvalidKeyInArrayException |
||
| 989 | */ |
||
| 990 | public function getAdditionalJavaScriptFiles() |
||
| 997 | |||
| 998 | /** |
||
| 999 | * @return \string[] |
||
| 1000 | * @throws \Fab\Vidi\Exception\InvalidKeyInArrayException |
||
| 1001 | */ |
||
| 1002 | public function getAdditionalStyleSheetFiles() |
||
| 1009 | |||
| 1010 | /** |
||
| 1011 | * @return array |
||
| 1012 | */ |
||
| 1013 | public function getComponents() |
||
| 1017 | |||
| 1018 | /** |
||
| 1019 | * @param string $pluginName |
||
| 1020 | * @return bool |
||
| 1021 | */ |
||
| 1022 | public function hasPlugin($pluginName = '') |
||
| 1033 | |||
| 1034 | /** |
||
| 1035 | * Compute the internal module code |
||
| 1036 | * |
||
| 1037 | * @param null|string $dataType |
||
| 1038 | * @return string |
||
| 1039 | */ |
||
| 1040 | protected function getInternalModuleSignature($dataType = null) |
||
| 1048 | |||
| 1049 | } |
||
| 1050 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: