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 AjaxUpgradeController 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 AjaxUpgradeController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 26 | class AjaxUpgradeController extends AbstractController |
||
| 27 | { |
||
| 28 | /** |
||
| 29 | * @var YamlDumper |
||
| 30 | */ |
||
| 31 | private $yamlManager; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var string the currently installed core version |
||
| 35 | */ |
||
| 36 | private $currentVersion; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * AjaxUpgradeController constructor. |
||
| 40 | * @param ContainerInterface $container |
||
| 41 | */ |
||
| 42 | public function __construct(ContainerInterface $container) |
||
| 51 | |||
| 52 | public function ajaxAction(Request $request) |
||
| 64 | |||
| 65 | public function commandLineAction($stage) |
||
| 71 | |||
| 72 | private function executeStage($stageName) |
||
| 73 | { |
||
| 74 | switch ($stageName) { |
||
| 75 | case "loginadmin": |
||
| 76 | $params = $this->decodeParameters($this->yamlManager->getParameters()); |
||
| 77 | |||
| 78 | return $this->loginAdmin($params); |
||
| 79 | case "upgrademodules": |
||
| 80 | $result = $this->upgradeModules(); |
||
| 81 | if (count($result) === 0) { |
||
| 82 | return true; |
||
| 83 | } |
||
| 84 | |||
| 85 | return $result; |
||
| 86 | case "installroutes": |
||
| 87 | if (version_compare(\ZikulaKernel::VERSION, '1.4.0', '>') && version_compare($this->currentVersion, '1.4.0', '>=')) { |
||
| 88 | // this stage is not necessary to upgrade from 1.4.0 -> 1.4.x |
||
| 89 | return true; |
||
| 90 | } |
||
| 91 | $this->installModule('ZikulaRoutesModule'); |
||
| 92 | $this->reSyncAndActivateModules(); |
||
| 93 | $this->setModuleCategory('ZikulaRoutesModule', $this->translator->__('System')); |
||
| 94 | |||
| 95 | return true; |
||
| 96 | case "regenthemes": |
||
| 97 | return $this->regenerateThemes(); |
||
| 98 | case "from140to141": |
||
| 99 | return $this->from140to141(); |
||
| 100 | case "from141to142": |
||
| 101 | return $this->from141to142(); |
||
| 102 | case "from142to143": |
||
| 103 | return $this->from142to143(); |
||
| 104 | case "from143to144": |
||
| 105 | return $this->from143to144(); |
||
| 106 | case "from144to145": |
||
| 107 | return $this->from144to145(); |
||
| 108 | case "from145to146": |
||
| 109 | return $this->from145to146(); |
||
| 110 | case "finalizeparameters": |
||
| 111 | return $this->finalizeParameters(); |
||
| 112 | case "clearcaches": |
||
| 113 | return $this->clearCaches(); |
||
| 114 | } |
||
| 115 | |||
| 116 | return true; |
||
| 117 | } |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Attempt to upgrade ALL the core modules. Some will need it, some will not. |
||
| 121 | * Modules that do not need upgrading return TRUE as a result of the upgrade anyway. |
||
| 122 | * @return array |
||
| 123 | */ |
||
| 124 | private function upgradeModules() |
||
| 125 | { |
||
| 126 | $coreModulesInPriorityUpgradeOrder = [ |
||
| 127 | 'ZikulaExtensionsModule', |
||
| 128 | 'ZikulaUsersModule', |
||
| 129 | 'ZikulaZAuthModule', |
||
| 130 | 'ZikulaGroupsModule', |
||
| 131 | 'ZikulaPermissionsModule', |
||
| 132 | 'ZikulaAdminModule', |
||
| 133 | 'ZikulaBlocksModule', |
||
| 134 | 'ZikulaThemeModule', |
||
| 135 | 'ZikulaSettingsModule', |
||
| 136 | 'ZikulaCategoriesModule', |
||
| 137 | 'ZikulaSecurityCenterModule', |
||
| 138 | 'ZikulaRoutesModule', |
||
| 139 | 'ZikulaMailerModule', |
||
| 140 | 'ZikulaSearchModule', |
||
| 141 | 'ZikulaMenuModule', |
||
| 142 | ]; |
||
| 143 | $result = []; |
||
| 144 | foreach ($coreModulesInPriorityUpgradeOrder as $moduleName) { |
||
| 145 | $extensionEntity = $this->container->get('zikula_extensions_module.extension_repository')->get($moduleName); |
||
| 146 | if (isset($extensionEntity)) { |
||
| 147 | $result[$moduleName] = $this->container->get('zikula_extensions_module.extension_helper')->upgrade($extensionEntity); |
||
| 148 | } |
||
| 149 | } |
||
| 150 | |||
| 151 | return $result; |
||
| 152 | } |
||
| 153 | |||
| 154 | private function regenerateThemes() |
||
| 168 | |||
| 169 | private function from140to141() |
||
| 191 | |||
| 192 | private function from141to142() |
||
| 207 | |||
| 208 | View Code Duplication | private function from142to143() |
|
| 219 | |||
| 220 | View Code Duplication | private function from143to144() |
|
| 231 | |||
| 232 | private function from144to145() |
||
| 240 | |||
| 241 | private function from145to146() |
||
| 242 | { |
||
| 249 | |||
| 250 | private function finalizeParameters() |
||
| 296 | |||
| 297 | private function clearCaches() |
||
| 316 | } |
||
| 317 |