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 ModuleManager 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 ModuleManager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class ModuleManager implements EventSubscriberInterface { |
||
| 17 | |||
| 18 | /** @var Map */ |
||
| 19 | private $loadedModules; |
||
| 20 | |||
| 21 | /** @var Map */ |
||
| 22 | private $activatedModules; |
||
| 23 | |||
| 24 | /** @var Map */ |
||
| 25 | private $installedModules; |
||
| 26 | |||
| 27 | /** @var ServiceContainer */ |
||
| 28 | private $service; |
||
| 29 | |||
| 30 | public function __construct(ServiceContainer $service) { |
||
| 49 | |||
| 50 | /** |
||
| 51 | * {@inheritDoc} |
||
| 52 | */ |
||
| 53 | public static function getSubscribedEvents() { |
||
| 62 | |||
| 63 | public function moduleUninstalled(ModuleEvent $e) { |
||
| 67 | |||
| 68 | public function moduleUpdated(ModuleEvent $e) { |
||
| 75 | |||
| 76 | public function moduleActivated(ModuleEvent $e) { |
||
| 80 | |||
| 81 | public function moduleDeactivated(ModuleEvent $e) { |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Returns whether the given package name is an installed module |
||
| 88 | * |
||
| 89 | * @param string $packageName |
||
| 90 | * @return boolean |
||
| 91 | */ |
||
| 92 | public function isInstalled($packageName) { |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Returns whether the given package name is an activated module |
||
| 98 | * |
||
| 99 | * @param string $packageName |
||
| 100 | * @return boolean |
||
| 101 | */ |
||
| 102 | public function isActivated($packageName) { |
||
| 105 | |||
| 106 | |||
| 107 | // public function getInstalledModules() { |
||
| 108 | // return $this->installedModules; |
||
| 109 | // } |
||
| 110 | |||
| 111 | // public function getActivatedModules() { |
||
| 112 | // return $this->activatedModules; |
||
| 113 | // } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Loads a module and returns the associated class or returns if already loaded |
||
| 117 | * |
||
| 118 | * @param String $packageName |
||
| 119 | * @throws ModuleException |
||
| 120 | * @return AbstractModule |
||
| 121 | */ |
||
| 122 | public function load($packageName) { |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @TODO still old api |
||
| 155 | * @param string $packageName |
||
| 156 | */ |
||
| 157 | public function loadTranslations($packageName) { |
||
| 179 | |||
| 180 | // public function update($packageName) { |
||
| 181 | // if (!$this->installedModules->has($packageName)) { |
||
| 182 | // throw new ModuleException(sprintf('Module (%s) not installed for activation', $packageName)); |
||
| 183 | // } |
||
| 184 | // $model = $this->installedModules->get($packageName); |
||
| 185 | // $model->setActivatedVersion($model->getInstalledVersion()); |
||
| 186 | // $model->save(); |
||
| 187 | // $module = $this->service->getPackageManager()->getModuleSchema($packageName); |
||
| 188 | |||
| 189 | // // install actions |
||
| 190 | // $extra = $package->getExtra(); |
||
| 191 | // if (isset($extra['keeko']) && isset($extra['keeko']['module'])) { |
||
| 192 | // $actions = $this->installActions($model, $extra['keeko']['module']); |
||
| 193 | // $this->installApi($model, $extra['keeko']['module'], $actions); |
||
| 194 | // } |
||
| 195 | // } |
||
| 196 | |||
| 197 | private function installActions(Module $module, $data) { |
||
| 234 | |||
| 235 | /** |
||
| 236 | * @param string $name |
||
| 237 | * @return Group |
||
| 238 | */ |
||
| 239 | View Code Duplication | private function getGroup($name) { |
|
| 260 | |||
| 261 | private function installApi(Module $module, $data, $actionMap) { |
||
| 302 | |||
| 303 | public function deactivate($packageName) { |
||
| 313 | |||
| 314 | // /** |
||
| 315 | // * Returns wether a module was loaded |
||
| 316 | // * |
||
| 317 | // * @param String $packageName |
||
| 318 | // * @return boolean true if loaded, false if not |
||
| 319 | // */ |
||
| 320 | // public function isLoaded($packageName) { |
||
| 321 | // return array_key_exists($packageName, $this->loadedModules); |
||
| 322 | // } |
||
| 323 | } |
||
| 324 |
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.