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 |
||
27 | class Extension extends BaseExtension |
||
28 | { |
||
29 | /** |
||
30 | * {@inheritdoc} |
||
31 | */ |
||
32 | 4 | public function getAlias() |
|
36 | |||
37 | /** |
||
38 | * {@inheritdoc} |
||
39 | */ |
||
40 | 2 | public function load(array $config, ContainerBuilder $container) |
|
41 | { |
||
42 | 2 | $configuration = new TreeConfiguration(); |
|
43 | 2 | $config = $this->processConfiguration($configuration, $config); |
|
44 | |||
45 | 2 | $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
|
46 | 2 | $loader->load('services.xml'); |
|
47 | |||
48 | 2 | $this->configureExchangeRateService($config, $container); |
|
49 | 2 | $this->configureSourcesRegistry($config, $container); |
|
50 | 2 | $this->configureProcessorsRegistry($config, $container); |
|
51 | 2 | $this->configureRatesRegistry($config, $container); |
|
52 | 2 | $this->configureFileRepository($config, $container); |
|
53 | 2 | $this->configureController($config, $container); |
|
54 | 2 | $this->configureDebugCommand($config, $container); |
|
55 | 2 | $this->configureCurrencyType($config, $container); |
|
56 | 2 | } |
|
57 | |||
58 | /** |
||
59 | * Configure exchange rate service. |
||
60 | * |
||
61 | * @param array $config |
||
62 | * @param ContainerBuilder $container |
||
63 | */ |
||
64 | 2 | protected function configureExchangeRateService(array $config, ContainerBuilder $container) |
|
79 | |||
80 | /** |
||
81 | * Configure sources registry. |
||
82 | * |
||
83 | * @param array $config |
||
84 | * @param ContainerBuilder $container |
||
85 | */ |
||
86 | 2 | protected function configureSourcesRegistry(array $config, ContainerBuilder $container) |
|
114 | |||
115 | /** |
||
116 | * Configure processors registry. |
||
117 | * |
||
118 | * @param array $config |
||
119 | * @param ContainerBuilder $container |
||
120 | */ |
||
121 | 2 | protected function configureProcessorsRegistry(array $config, ContainerBuilder $container) |
|
149 | |||
150 | /** |
||
151 | * Configure rates. |
||
152 | * |
||
153 | * @param array $config |
||
154 | * @param ContainerBuilder $container |
||
155 | */ |
||
156 | 2 | protected function configureRatesRegistry(array $config, ContainerBuilder $container) |
|
180 | |||
181 | /** |
||
182 | * Configure file registry, if used. |
||
183 | * |
||
184 | * @param array $config |
||
185 | * @param ContainerBuilder $container |
||
186 | */ |
||
187 | 2 | protected function configureFileRepository(array $config, ContainerBuilder $container) |
|
188 | { |
||
189 | if ( |
||
190 | 2 | $config['repository'] === 'run_open_code.exchange_rate.repository.file_repository' |
|
191 | 2 | && |
|
192 | 2 | $container->hasDefinition('run_open_code.exchange_rate.repository.file_repository') |
|
193 | 2 | ) { |
|
194 | |||
195 | 2 | if (!empty($config['file_repository']) && !empty($config['file_repository']['path'])) { |
|
196 | 2 | $definition = $container->getDefinition('run_open_code.exchange_rate.repository.file_repository'); |
|
197 | 2 | $definition->setArguments(array( |
|
198 | 2 | $config['file_repository']['path'] |
|
199 | 2 | )); |
|
200 | 2 | } else { |
|
201 | throw new InvalidConfigurationException('You must configure location to the file where file repository will store exchange rates.'); |
||
202 | } |
||
203 | |||
204 | 2 | } elseif ($config['repository'] === 'run_open_code.exchange_rate.repository.file_repository') { |
|
205 | throw new InvalidConfigurationException('File repository is used to store exchange rates, but it is not available in container.'); |
||
206 | } else { |
||
207 | $container->removeDefinition('run_open_code.exchange_rate.repository.file_repository'); |
||
208 | } |
||
209 | 2 | } |
|
210 | |||
211 | /** |
||
212 | * Configure controller - view layer. |
||
213 | * |
||
214 | * @param array $config |
||
215 | * @param ContainerBuilder $container |
||
216 | */ |
||
217 | 2 | protected function configureController(array $config, ContainerBuilder $container) |
|
228 | |||
229 | /** |
||
230 | * Configure debug command. |
||
231 | * |
||
232 | * @param array $config |
||
233 | * @param ContainerBuilder $container |
||
234 | */ |
||
235 | 2 | View Code Duplication | protected function configureDebugCommand(array $config, ContainerBuilder $container) |
246 | |||
247 | /** |
||
248 | * Configure currency type. |
||
249 | * |
||
250 | * @param array $config |
||
251 | * @param ContainerBuilder $container |
||
252 | */ |
||
253 | 2 | View Code Duplication | protected function configureCurrencyType(array $config, ContainerBuilder $container) |
264 | |||
265 | /** |
||
266 | * Extract name of only required sources from configuration. |
||
267 | * |
||
268 | * @param array $config |
||
269 | * @return array |
||
270 | */ |
||
271 | 2 | protected function getRequiredSources(array $config) |
|
281 | } |
||
282 |
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.