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:
| 1 | <?php |
||
| 18 | class PluginManager |
||
| 19 | { |
||
| 20 | /** @var PluginDescription[] List of all the plugins adescriptions. */ |
||
| 21 | protected $plugins = []; |
||
| 22 | |||
| 23 | /** @var PluginDescription[] Current List of enabled plugins */ |
||
| 24 | protected $enabledPlugins = []; |
||
| 25 | |||
| 26 | /** @var PluginDescriptionFactory */ |
||
| 27 | protected $pluginDescriptionFactory; |
||
| 28 | |||
| 29 | /** @var ContainerInterface */ |
||
| 30 | protected $container; |
||
| 31 | |||
| 32 | /** @var DataProviderManager */ |
||
| 33 | protected $dataProviderManager; |
||
| 34 | |||
| 35 | /** @var GameDataStorage */ |
||
| 36 | protected $gameDataStorage; |
||
| 37 | |||
| 38 | /** @var Console */ |
||
| 39 | protected $console; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * PluginManager constructor. |
||
| 43 | * |
||
| 44 | * @param ContainerInterface $container |
||
| 45 | * @param PluginDescriptionFactory $pluginDescriptionFactory |
||
| 46 | * @param DataProviderManager $dataProviderManager |
||
| 47 | * @param GameDataStorage $gameDataStorage |
||
| 48 | * @param Console $console |
||
| 49 | */ |
||
| 50 | 48 | public function __construct( |
|
| 63 | |||
| 64 | /** |
||
| 65 | * Initialize plugins. |
||
| 66 | */ |
||
| 67 | public function init() |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Do a reset |
||
| 74 | */ |
||
| 75 | public function reset() |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Enable all possible plugins. |
||
| 86 | * |
||
| 87 | * @param string $title |
||
| 88 | * @param string $mode |
||
| 89 | * @param string $script |
||
| 90 | */ |
||
| 91 | protected function enableDisablePlugins($title, $mode, $script) |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Check if a plugin is compatible or not. |
||
| 122 | * |
||
| 123 | * @param PluginDescription $plugin |
||
| 124 | * @param $enabledPlugins |
||
| 125 | * @param $title |
||
| 126 | * @param $mode |
||
| 127 | * @param $script |
||
| 128 | * |
||
| 129 | * @return bool |
||
| 130 | */ |
||
| 131 | protected function isPluginCompatible(PluginDescription $plugin, $enabledPlugins, $title, $mode, $script) |
||
| 165 | |||
| 166 | |||
| 167 | /** |
||
| 168 | * Enable a plugin for a certain game mode. |
||
| 169 | * |
||
| 170 | * @param PluginDescription $plugin |
||
| 171 | * @param $title |
||
| 172 | * @param $mode |
||
| 173 | * @param $script |
||
| 174 | */ |
||
| 175 | protected function enablePlugin(PluginDescription $plugin, $title, $mode, $script) |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Disable a plugin. |
||
| 195 | * |
||
| 196 | * @param PluginDescription $plugin |
||
| 197 | * |
||
| 198 | */ |
||
| 199 | protected function disablePlugin(PluginDescription $plugin) |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Check if a plugin is enabled or not. |
||
| 219 | * |
||
| 220 | * @param $pluginId |
||
| 221 | * |
||
| 222 | * @return bool |
||
| 223 | */ |
||
| 224 | public function isPluginEnabled($pluginId) |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Register a plugin. |
||
| 231 | * |
||
| 232 | * @param string $id The service id of the plugin to register. |
||
| 233 | * @param string[] $dataProviders The data providers it needs to work. |
||
| 234 | * @param string[] $parents The parent plugins. |
||
| 235 | */ |
||
| 236 | 48 | public function registerPlugin($id, $dataProviders, $parents, $dataProviderName = null) |
|
| 246 | } |
||
| 247 |
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.