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 |
||
29 | class Extension extends BaseExtension |
||
30 | { |
||
31 | /** |
||
32 | * {@inheritdoc} |
||
33 | */ |
||
34 | 5 | public function getAlias() |
|
38 | |||
39 | /** |
||
40 | * {@inheritdoc} |
||
41 | */ |
||
42 | 5 | public function getNamespace() |
|
46 | |||
47 | /** |
||
48 | * {@inheritdoc} |
||
49 | */ |
||
50 | public function getXsdValidationBasePath() |
||
54 | |||
55 | /** |
||
56 | * {@inheritdoc} |
||
57 | */ |
||
58 | 5 | public function load(array $config, ContainerBuilder $container) |
|
89 | |||
90 | /** |
||
91 | * Configure base currency. |
||
92 | * |
||
93 | * @param array $config Configuration parameters. |
||
94 | * @param ContainerBuilder $container Service container. |
||
95 | * |
||
96 | * @return Extension $this Fluent interface. |
||
97 | * @throws \RunOpenCode\ExchangeRate\Exception\UnknownCurrencyCodeException |
||
98 | */ |
||
99 | 5 | protected function configureBaseCurrency(array $config, ContainerBuilder $container) |
|
106 | |||
107 | /** |
||
108 | * Configure rates. |
||
109 | * |
||
110 | * @param array $config Configuration parameters. |
||
111 | * @param ContainerBuilder $container Service container. |
||
112 | * |
||
113 | * @return Extension $this Fluent interface. |
||
114 | */ |
||
115 | 5 | protected function configureRates(array $config, ContainerBuilder $container) |
|
142 | |||
143 | /** |
||
144 | * Configure required processors. |
||
145 | * |
||
146 | * @param array $config Configuration parameters. |
||
147 | * @param ContainerBuilder $container Service container. |
||
148 | * |
||
149 | * @return Extension $this Fluent interface. |
||
150 | */ |
||
151 | 5 | protected function configureRepository(array $config, ContainerBuilder $container) |
|
156 | |||
157 | /** |
||
158 | * Configure file repository. |
||
159 | * |
||
160 | * @param array $config Configuration parameters. |
||
161 | * @param ContainerBuilder $container Service container. |
||
162 | * |
||
163 | * @return Extension $this Fluent interface. |
||
164 | */ |
||
165 | 5 | protected function configureFileRepository(array $config, ContainerBuilder $container) |
|
176 | |||
177 | /** |
||
178 | * Configure Doctrine Dbal repository. |
||
179 | * |
||
180 | * @param array $config Configuration parameters. |
||
181 | * @param ContainerBuilder $container Service container. |
||
182 | * |
||
183 | * @return Extension $this Fluent interface. |
||
184 | */ |
||
185 | 5 | protected function configureDoctrineDbalRepository(array $config, ContainerBuilder $container) |
|
197 | |||
198 | /** |
||
199 | * Configure controller. |
||
200 | * |
||
201 | * @param array $config Configuration parameters. |
||
202 | * @param ContainerBuilder $container Service container. |
||
203 | * @return Extension $this Fluent interface. |
||
204 | */ |
||
205 | 5 | protected function configureController(array $config, ContainerBuilder $container) |
|
214 | |||
215 | /** |
||
216 | * Configure simple sources which does not have to be explicitly added to service container. |
||
217 | * |
||
218 | * @param array $config Configuration parameters. |
||
219 | * @param ContainerBuilder $container Service container. |
||
220 | * @return Extension $this Fluent interface. |
||
221 | */ |
||
222 | 5 | protected function configureSimpleSources(array $config, ContainerBuilder $container) |
|
230 | |||
231 | /** |
||
232 | * Configure "RunOpenCode\\Bundle\\ExchangeRate\\Form\\Type\\SourceType" default settings. |
||
233 | * |
||
234 | * @param array $config Configuration parameters. |
||
235 | * @param ContainerBuilder $container Service container. |
||
236 | * @return Extension $this Fluent interface. |
||
237 | */ |
||
238 | 5 | View Code Duplication | protected function configureSourceType(array $config, ContainerBuilder $container) |
250 | |||
251 | /** |
||
252 | * Configure "RunOpenCode\\Bundle\\ExchangeRate\\Form\\Type\\RateTypeType" default settings. |
||
253 | * |
||
254 | * @param array $config Configuration parameters. |
||
255 | * @param ContainerBuilder $container Service container. |
||
256 | * @return Extension $this Fluent interface. |
||
257 | */ |
||
258 | 5 | protected function configureRateTypeType(array $config, ContainerBuilder $container) |
|
272 | |||
273 | /** |
||
274 | * Configure "RunOpenCode\\Bundle\\ExchangeRate\\Form\\Type\\CurrencyCodeType" default settings. |
||
275 | * |
||
276 | * @param array $config Configuration parameters. |
||
277 | * @param ContainerBuilder $container Service container. |
||
278 | * @return Extension $this Fluent interface. |
||
279 | */ |
||
280 | 5 | protected function configureCurrencyCodeType(array $config, ContainerBuilder $container) |
|
296 | |||
297 | /** |
||
298 | * Configure "RunOpenCode\\Bundle\\ExchangeRate\\Form\\Type\\ForeignCurrencyCodeType" default settings. |
||
299 | * |
||
300 | * @param array $config Configuration parameters. |
||
301 | * @param ContainerBuilder $container Service container. |
||
302 | * @return Extension $this Fluent interface. |
||
303 | */ |
||
304 | 5 | View Code Duplication | protected function configureForeignCurrencyCodeType(array $config, ContainerBuilder $container) |
318 | |||
319 | /** |
||
320 | * Configure "RunOpenCode\\Bundle\\ExchangeRate\\Form\\Type\\RateType" default settings. |
||
321 | * |
||
322 | * @param array $config Configuration parameters. |
||
323 | * @param ContainerBuilder $container Service container. |
||
324 | * @return Extension $this Fluent interface. |
||
325 | */ |
||
326 | 5 | protected function configureRateType(array $config, ContainerBuilder $container) |
|
332 | } |
||
333 |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: