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 | 12 | public function getAlias() |
|
| 39 | |||
| 40 | /** |
||
| 41 | * {@inheritdoc} |
||
| 42 | */ |
||
| 43 | 12 | public function getNamespace() |
|
| 47 | |||
| 48 | /** |
||
| 49 | * {@inheritdoc} |
||
| 50 | */ |
||
| 51 | 2 | public function getXsdValidationBasePath() |
|
| 55 | |||
| 56 | /** |
||
| 57 | * {@inheritdoc} |
||
| 58 | */ |
||
| 59 | 10 | public function load(array $config, ContainerBuilder $container) |
|
| 91 | |||
| 92 | /** |
||
| 93 | * Configure base currency. |
||
| 94 | * |
||
| 95 | * @param array $config Configuration parameters. |
||
| 96 | * @param ContainerBuilder $container Service container. |
||
| 97 | * |
||
| 98 | * @return Extension $this Fluent interface. |
||
| 99 | * @throws \RunOpenCode\ExchangeRate\Exception\UnknownCurrencyCodeException |
||
| 100 | */ |
||
| 101 | 10 | protected function configureBaseCurrency(array $config, ContainerBuilder $container) |
|
| 108 | |||
| 109 | /** |
||
| 110 | * Configure rates. |
||
| 111 | * |
||
| 112 | * @param array $config Configuration parameters. |
||
| 113 | * @param ContainerBuilder $container Service container. |
||
| 114 | * |
||
| 115 | * @return Extension $this Fluent interface. |
||
| 116 | */ |
||
| 117 | 10 | protected function configureRates(array $config, ContainerBuilder $container) |
|
| 140 | |||
| 141 | |||
| 142 | /** |
||
| 143 | * Configure sources which does not have to be explicitly added to service container. |
||
| 144 | * |
||
| 145 | * @param array $config Configuration parameters. |
||
| 146 | * @param ContainerBuilder $container Service container. |
||
| 147 | * |
||
| 148 | * @return Extension $this Fluent interface. |
||
| 149 | */ |
||
| 150 | 10 | protected function configureSources(array $config, ContainerBuilder $container) |
|
| 166 | |||
| 167 | /** |
||
| 168 | * Configure required processors. |
||
| 169 | * |
||
| 170 | * @param array $config Configuration parameters. |
||
| 171 | * @param ContainerBuilder $container Service container. |
||
| 172 | * |
||
| 173 | * @return Extension $this Fluent interface. |
||
| 174 | */ |
||
| 175 | 10 | protected function configureRepository(array $config, ContainerBuilder $container) |
|
| 180 | |||
| 181 | /** |
||
| 182 | * Configure file repository. |
||
| 183 | * |
||
| 184 | * @param array $config Configuration parameters. |
||
| 185 | * @param ContainerBuilder $container Service container. |
||
| 186 | * |
||
| 187 | * @return Extension $this Fluent interface. |
||
| 188 | */ |
||
| 189 | 10 | protected function configureFileRepository(array $config, ContainerBuilder $container) |
|
| 200 | |||
| 201 | /** |
||
| 202 | * Configure Doctrine Dbal repository. |
||
| 203 | * |
||
| 204 | * @param array $config Configuration parameters. |
||
| 205 | * @param ContainerBuilder $container Service container. |
||
| 206 | * |
||
| 207 | * @return Extension $this Fluent interface. |
||
| 208 | */ |
||
| 209 | 10 | protected function configureDoctrineDbalRepository(array $config, ContainerBuilder $container) |
|
| 221 | |||
| 222 | /** |
||
| 223 | * Configure access voter. |
||
| 224 | * |
||
| 225 | * @param array $config Configuration parameters. |
||
| 226 | * @param ContainerBuilder $container Service container. |
||
| 227 | * |
||
| 228 | * @return Extension $this Fluent interface. |
||
| 229 | */ |
||
| 230 | 10 | protected function configureAccessVoter(array $config, ContainerBuilder $container) |
|
| 255 | |||
| 256 | /** |
||
| 257 | * Configure "RunOpenCode\\Bundle\\ExchangeRate\\Form\\Type\\SourceType" default settings. |
||
| 258 | * |
||
| 259 | * @param array $config Configuration parameters. |
||
| 260 | * @param ContainerBuilder $container Service container. |
||
| 261 | * |
||
| 262 | * @return Extension $this Fluent interface. |
||
| 263 | */ |
||
| 264 | 10 | View Code Duplication | protected function configureSourceType(array $config, ContainerBuilder $container) |
| 276 | |||
| 277 | /** |
||
| 278 | * Configure "RunOpenCode\\Bundle\\ExchangeRate\\Form\\Type\\RateTypeType" default settings. |
||
| 279 | * |
||
| 280 | * @param array $config Configuration parameters. |
||
| 281 | * @param ContainerBuilder $container Service container. |
||
| 282 | * |
||
| 283 | * @return Extension $this Fluent interface. |
||
| 284 | */ |
||
| 285 | 10 | View Code Duplication | protected function configureRateTypeType(array $config, ContainerBuilder $container) |
| 297 | |||
| 298 | /** |
||
| 299 | * Configure "RunOpenCode\\Bundle\\ExchangeRate\\Form\\Type\\CurrencyCodeType" default settings. |
||
| 300 | * |
||
| 301 | * @param array $config Configuration parameters. |
||
| 302 | * @param ContainerBuilder $container Service container. |
||
| 303 | * @return Extension $this Fluent interface. |
||
| 304 | */ |
||
| 305 | 10 | protected function configureCurrencyCodeType(array $config, ContainerBuilder $container) |
|
| 318 | |||
| 319 | /** |
||
| 320 | * Configure "RunOpenCode\\Bundle\\ExchangeRate\\Form\\Type\\ForeignCurrencyCodeType" default settings. |
||
| 321 | * |
||
| 322 | * @param array $config Configuration parameters. |
||
| 323 | * @param ContainerBuilder $container Service container. |
||
| 324 | * |
||
| 325 | * @return Extension $this Fluent interface. |
||
| 326 | */ |
||
| 327 | 10 | View Code Duplication | protected function configureForeignCurrencyCodeType(array $config, ContainerBuilder $container) |
| 340 | |||
| 341 | /** |
||
| 342 | * Configure "RunOpenCode\\Bundle\\ExchangeRate\\Form\\Type\\RateType" default settings. |
||
| 343 | * |
||
| 344 | * @param array $config Configuration parameters. |
||
| 345 | * @param ContainerBuilder $container Service container. |
||
| 346 | * |
||
| 347 | * @return Extension $this Fluent interface. |
||
| 348 | */ |
||
| 349 | 10 | View Code Duplication | protected function configureRateType(array $config, ContainerBuilder $container) |
| 361 | |||
| 362 | /** |
||
| 363 | * Configure notifications |
||
| 364 | * |
||
| 365 | * @param array $config Configuration parameters. |
||
| 366 | * @param ContainerBuilder $container Service container. |
||
| 367 | * |
||
| 368 | * @return Extension $this Fluent interface. |
||
| 369 | */ |
||
| 370 | 10 | protected function configureNotifications(array $config, ContainerBuilder $container) |
|
| 378 | } |
||
| 379 |
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: