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 |
||
17 | class PluginManager |
||
18 | { |
||
19 | /** @var PluginDescription[] List of all the plugins adescriptions. */ |
||
20 | protected $plugins = []; |
||
21 | |||
22 | /** @var PluginDescription[] Current List of enabled plugins */ |
||
23 | protected $enabledPlugins = []; |
||
24 | |||
25 | /** @var PluginDescriptionFactory */ |
||
26 | protected $pluginDescriptionFactory; |
||
27 | |||
28 | /** @var ContainerInterface */ |
||
29 | protected $container; |
||
30 | |||
31 | /** @var DataProviderManager */ |
||
32 | protected $dataProviderManager; |
||
33 | |||
34 | /** @var GameDataStorage */ |
||
35 | protected $gameDataStorage; |
||
36 | |||
37 | /** @var Console */ |
||
38 | protected $console; |
||
39 | |||
40 | /** |
||
41 | * PluginManager constructor. |
||
42 | * |
||
43 | * @param ContainerInterface $container |
||
44 | * @param PluginDescriptionFactory $pluginDescriptionFactory |
||
45 | * @param DataProviderManager $dataProviderManager |
||
46 | * @param GameDataStorage $gameDataStorage |
||
47 | * @param Console $console |
||
48 | */ |
||
49 | 44 | View Code Duplication | public function __construct( |
62 | |||
63 | /** |
||
64 | * Initialize. |
||
65 | * |
||
66 | * @throws \eXpansion\Framework\Core\Exceptions\DataProvider\UncompatibleException |
||
67 | */ |
||
68 | public function init(Map $map) |
||
72 | |||
73 | /** |
||
74 | * Do a reset to plugins/ |
||
75 | * |
||
76 | * @param Map $map |
||
77 | * |
||
78 | * @throws \eXpansion\Framework\Core\Exceptions\DataProvider\UncompatibleException |
||
79 | */ |
||
80 | public function reset(Map $map) |
||
88 | |||
89 | /** |
||
90 | * @param $title |
||
91 | * @param $mode |
||
92 | * @param $script |
||
93 | * @param Map $map |
||
94 | * |
||
95 | * @throws \eXpansion\Framework\Core\Exceptions\DataProvider\UncompatibleException |
||
96 | */ |
||
97 | protected function enableDisablePlugins($title, $mode, $script, Map $map) |
||
146 | |||
147 | /** |
||
148 | * Check if a plugin is compatible or not. |
||
149 | * |
||
150 | * @param PluginDescription $plugin |
||
151 | * @param $enabledPlugins |
||
152 | * @param $title |
||
153 | * @param $mode |
||
154 | * @param $script |
||
155 | * @param $map |
||
156 | * |
||
157 | * @return bool |
||
158 | */ |
||
159 | protected function isPluginCompatible(PluginDescription $plugin, $enabledPlugins, $title, $mode, $script, Map $map) |
||
160 | { |
||
161 | |||
162 | // first check for other plugins. |
||
163 | foreach ($plugin->getParents() as $parentPluginId) { |
||
164 | if (!isset($enabledPlugins[$parentPluginId])) { |
||
165 | // A parent plugin is missing. Can't enable plugin. |
||
166 | return false; |
||
167 | } |
||
168 | } |
||
169 | |||
170 | // Now check for data providers. |
||
171 | foreach ($plugin->getDataProviders() as $dataProvider) { |
||
172 | $dataProviders = explode("|", $dataProvider); |
||
173 | $foundOne = false; |
||
174 | |||
175 | foreach ($dataProviders as $provider) { |
||
176 | $providerId = $this->dataProviderManager->getCompatibleProviderId($provider, $title, $mode, $script, $map); |
||
177 | if (!is_null($providerId) && isset($enabledPlugins[$providerId])) { |
||
178 | // Either there are no data providers compatible or the only one compatible |
||
179 | $foundOne = true; |
||
180 | break; |
||
181 | } |
||
182 | } |
||
183 | |||
184 | if (!$foundOne) { |
||
185 | return false; |
||
186 | } |
||
187 | } |
||
188 | |||
189 | // If data provider need to check if it was "the chosen one". |
||
190 | if ($plugin->isIsDataProvider()) { |
||
191 | $selectedProvider = $this->dataProviderManager->getCompatibleProviderId( |
||
192 | $plugin->getDataProviderName(), |
||
193 | $title, |
||
194 | $mode, |
||
195 | $script, |
||
196 | $map |
||
197 | ); |
||
198 | |||
199 | if ($plugin->getPluginId() != $selectedProvider) { |
||
200 | // This data provider wasn't the one selected and therefore the plugin isn't compatible. |
||
201 | return false; |
||
202 | } |
||
203 | } |
||
204 | |||
205 | return true; |
||
206 | } |
||
207 | |||
208 | /** |
||
209 | * Enable a certain plugin. |
||
210 | * |
||
211 | * @param PluginDescription $plugin |
||
212 | * @param $title |
||
213 | * @param $mode |
||
214 | * @param $script |
||
215 | * @param Map $map |
||
216 | * |
||
217 | * @return mixed |
||
218 | * |
||
219 | * @throws \eXpansion\Framework\Core\Exceptions\DataProvider\UncompatibleException |
||
220 | */ |
||
221 | protected function enablePlugin(PluginDescription $plugin, $title, $mode, $script, Map $map) |
||
242 | |||
243 | /** |
||
244 | * Disable a plugin |
||
245 | * |
||
246 | * @param PluginDescription $plugin |
||
247 | * |
||
248 | * @return mixed |
||
249 | */ |
||
250 | protected function disablePlugin(PluginDescription $plugin) |
||
271 | |||
272 | /** |
||
273 | * Check if a plugin is enabled or not. |
||
274 | * |
||
275 | * @param $pluginId |
||
276 | * |
||
277 | * @return bool |
||
278 | */ |
||
279 | public function isPluginEnabled($pluginId) |
||
283 | |||
284 | /** |
||
285 | * Register a plugin. |
||
286 | * |
||
287 | * @param string $id The service id of the plugin to register. |
||
288 | * @param string[] $dataProviders The data providers it needs to work. |
||
289 | * @param string[] $parents The parent plugins. |
||
290 | */ |
||
291 | 44 | public function registerPlugin($id, $dataProviders, $parents, $dataProviderName = null) |
|
301 | } |
||
302 |
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.