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 |
||
| 30 | class Extension extends BaseExtension |
||
| 31 | { |
||
| 32 | /** |
||
| 33 | * {@inheritdoc} |
||
| 34 | */ |
||
| 35 | 11 | public function getAlias() |
|
| 39 | |||
| 40 | /** |
||
| 41 | * {@inheritdoc} |
||
| 42 | */ |
||
| 43 | 11 | public function getNamespace() |
|
| 47 | |||
| 48 | /** |
||
| 49 | * {@inheritdoc} |
||
| 50 | */ |
||
| 51 | 2 | public function getXsdValidationBasePath() |
|
| 55 | |||
| 56 | /** |
||
| 57 | * {@inheritdoc} |
||
| 58 | */ |
||
| 59 | 9 | public function load(array $config, ContainerBuilder $container) |
|
| 92 | |||
| 93 | /** |
||
| 94 | * Configure base currency. |
||
| 95 | * |
||
| 96 | * @param array $config Configuration parameters. |
||
| 97 | * @param ContainerBuilder $container Service container. |
||
| 98 | * |
||
| 99 | * @return Extension $this Fluent interface. |
||
| 100 | * @throws \RunOpenCode\ExchangeRate\Exception\UnknownCurrencyCodeException |
||
| 101 | */ |
||
| 102 | 9 | protected function configureBaseCurrency(array $config, ContainerBuilder $container) |
|
| 109 | |||
| 110 | /** |
||
| 111 | * Configure rates. |
||
| 112 | * |
||
| 113 | * @param array $config Configuration parameters. |
||
| 114 | * @param ContainerBuilder $container Service container. |
||
| 115 | * |
||
| 116 | * @return Extension $this Fluent interface. |
||
| 117 | */ |
||
| 118 | 9 | protected function configureRates(array $config, ContainerBuilder $container) |
|
| 141 | |||
| 142 | |||
| 143 | /** |
||
| 144 | * Configure sources which does not have to be explicitly added to service container. |
||
| 145 | * |
||
| 146 | * @param array $config Configuration parameters. |
||
| 147 | * @param ContainerBuilder $container Service container. |
||
| 148 | * |
||
| 149 | * @return Extension $this Fluent interface. |
||
| 150 | */ |
||
| 151 | 9 | protected function configureSources(array $config, ContainerBuilder $container) |
|
| 167 | |||
| 168 | /** |
||
| 169 | * Configure required processors. |
||
| 170 | * |
||
| 171 | * @param array $config Configuration parameters. |
||
| 172 | * @param ContainerBuilder $container Service container. |
||
| 173 | * |
||
| 174 | * @return Extension $this Fluent interface. |
||
| 175 | */ |
||
| 176 | 9 | protected function configureRepository(array $config, ContainerBuilder $container) |
|
| 181 | |||
| 182 | /** |
||
| 183 | * Configure file repository. |
||
| 184 | * |
||
| 185 | * @param array $config Configuration parameters. |
||
| 186 | * @param ContainerBuilder $container Service container. |
||
| 187 | * |
||
| 188 | * @return Extension $this Fluent interface. |
||
| 189 | */ |
||
| 190 | 9 | protected function configureFileRepository(array $config, ContainerBuilder $container) |
|
| 201 | |||
| 202 | /** |
||
| 203 | * Configure Doctrine Dbal repository. |
||
| 204 | * |
||
| 205 | * @param array $config Configuration parameters. |
||
| 206 | * @param ContainerBuilder $container Service container. |
||
| 207 | * |
||
| 208 | * @return Extension $this Fluent interface. |
||
| 209 | */ |
||
| 210 | 9 | protected function configureDoctrineDbalRepository(array $config, ContainerBuilder $container) |
|
| 222 | |||
| 223 | /** |
||
| 224 | * Configure access voter. |
||
| 225 | * |
||
| 226 | * @param array $config Configuration parameters. |
||
| 227 | * @param ContainerBuilder $container Service container. |
||
| 228 | * |
||
| 229 | * @return Extension $this Fluent interface. |
||
| 230 | */ |
||
| 231 | 9 | protected function configureAccessVoter(array $config, ContainerBuilder $container) |
|
| 252 | |||
| 253 | /** |
||
| 254 | * Configure "RunOpenCode\\Bundle\\ExchangeRate\\Form\\Type\\SourceType" default settings. |
||
| 255 | * |
||
| 256 | * @param array $config Configuration parameters. |
||
| 257 | * @param ContainerBuilder $container Service container. |
||
| 258 | * |
||
| 259 | * @return Extension $this Fluent interface. |
||
| 260 | */ |
||
| 261 | 9 | View Code Duplication | protected function configureSourceType(array $config, ContainerBuilder $container) |
| 273 | |||
| 274 | /** |
||
| 275 | * Configure "RunOpenCode\\Bundle\\ExchangeRate\\Form\\Type\\RateTypeType" default settings. |
||
| 276 | * |
||
| 277 | * @param array $config Configuration parameters. |
||
| 278 | * @param ContainerBuilder $container Service container. |
||
| 279 | * |
||
| 280 | * @return Extension $this Fluent interface. |
||
| 281 | */ |
||
| 282 | 9 | View Code Duplication | protected function configureRateTypeType(array $config, ContainerBuilder $container) |
| 294 | |||
| 295 | /** |
||
| 296 | * Configure "RunOpenCode\\Bundle\\ExchangeRate\\Form\\Type\\CurrencyCodeType" default settings. |
||
| 297 | * |
||
| 298 | * @param array $config Configuration parameters. |
||
| 299 | * @param ContainerBuilder $container Service container. |
||
| 300 | * @return Extension $this Fluent interface. |
||
| 301 | */ |
||
| 302 | 9 | protected function configureCurrencyCodeType(array $config, ContainerBuilder $container) |
|
| 315 | |||
| 316 | /** |
||
| 317 | * Configure "RunOpenCode\\Bundle\\ExchangeRate\\Form\\Type\\ForeignCurrencyCodeType" default settings. |
||
| 318 | * |
||
| 319 | * @param array $config Configuration parameters. |
||
| 320 | * @param ContainerBuilder $container Service container. |
||
| 321 | * |
||
| 322 | * @return Extension $this Fluent interface. |
||
| 323 | */ |
||
| 324 | 9 | View Code Duplication | protected function configureForeignCurrencyCodeType(array $config, ContainerBuilder $container) |
| 337 | |||
| 338 | /** |
||
| 339 | * Configure "RunOpenCode\\Bundle\\ExchangeRate\\Form\\Type\\RateType" default settings. |
||
| 340 | * |
||
| 341 | * @param array $config Configuration parameters. |
||
| 342 | * @param ContainerBuilder $container Service container. |
||
| 343 | * |
||
| 344 | * @return Extension $this Fluent interface. |
||
| 345 | */ |
||
| 346 | 9 | View Code Duplication | protected function configureRateType(array $config, ContainerBuilder $container) |
| 358 | |||
| 359 | /** |
||
| 360 | * Configure notifications |
||
| 361 | * |
||
| 362 | * @param array $config Configuration parameters. |
||
| 363 | * @param ContainerBuilder $container Service container. |
||
| 364 | * |
||
| 365 | * @return Extension $this Fluent interface. |
||
| 366 | */ |
||
| 367 | 9 | protected function configureNotifications(array $config, ContainerBuilder $container) |
|
| 375 | } |
||
| 376 |
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare 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.phpHowever, as
OtherDir/Foo.phpdoes 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: