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 | 8 | public function getAlias() |
|
| 39 | |||
| 40 | /** |
||
| 41 | * {@inheritdoc} |
||
| 42 | */ |
||
| 43 | 8 | public function getNamespace() |
|
| 47 | |||
| 48 | /** |
||
| 49 | * {@inheritdoc} |
||
| 50 | */ |
||
| 51 | public function getXsdValidationBasePath() |
||
| 55 | |||
| 56 | /** |
||
| 57 | * {@inheritdoc} |
||
| 58 | */ |
||
| 59 | 8 | public function load(array $config, ContainerBuilder $container) |
|
| 90 | |||
| 91 | /** |
||
| 92 | * Configure base currency. |
||
| 93 | * |
||
| 94 | * @param array $config Configuration parameters. |
||
| 95 | * @param ContainerBuilder $container Service container. |
||
| 96 | * |
||
| 97 | * @return Extension $this Fluent interface. |
||
| 98 | * @throws \RunOpenCode\ExchangeRate\Exception\UnknownCurrencyCodeException |
||
| 99 | */ |
||
| 100 | 8 | protected function configureBaseCurrency(array $config, ContainerBuilder $container) |
|
| 107 | |||
| 108 | /** |
||
| 109 | * Configure rates. |
||
| 110 | * |
||
| 111 | * @param array $config Configuration parameters. |
||
| 112 | * @param ContainerBuilder $container Service container. |
||
| 113 | * |
||
| 114 | * @return Extension $this Fluent interface. |
||
| 115 | */ |
||
| 116 | 8 | protected function configureRates(array $config, ContainerBuilder $container) |
|
| 143 | |||
| 144 | |||
| 145 | /** |
||
| 146 | * Configure sources which does not have to be explicitly added to service container. |
||
| 147 | * |
||
| 148 | * @param array $config Configuration parameters. |
||
| 149 | * @param ContainerBuilder $container Service container. |
||
| 150 | * |
||
| 151 | * @return Extension $this Fluent interface. |
||
| 152 | */ |
||
| 153 | 8 | protected function configureSources(array $config, ContainerBuilder $container) |
|
| 169 | |||
| 170 | /** |
||
| 171 | * Configure required processors. |
||
| 172 | * |
||
| 173 | * @param array $config Configuration parameters. |
||
| 174 | * @param ContainerBuilder $container Service container. |
||
| 175 | * |
||
| 176 | * @return Extension $this Fluent interface. |
||
| 177 | */ |
||
| 178 | 8 | protected function configureRepository(array $config, ContainerBuilder $container) |
|
| 183 | |||
| 184 | /** |
||
| 185 | * Configure file repository. |
||
| 186 | * |
||
| 187 | * @param array $config Configuration parameters. |
||
| 188 | * @param ContainerBuilder $container Service container. |
||
| 189 | * |
||
| 190 | * @return Extension $this Fluent interface. |
||
| 191 | */ |
||
| 192 | 8 | protected function configureFileRepository(array $config, ContainerBuilder $container) |
|
| 203 | |||
| 204 | /** |
||
| 205 | * Configure Doctrine Dbal repository. |
||
| 206 | * |
||
| 207 | * @param array $config Configuration parameters. |
||
| 208 | * @param ContainerBuilder $container Service container. |
||
| 209 | * |
||
| 210 | * @return Extension $this Fluent interface. |
||
| 211 | */ |
||
| 212 | 8 | protected function configureDoctrineDbalRepository(array $config, ContainerBuilder $container) |
|
| 224 | |||
| 225 | /** |
||
| 226 | * Configure access voter. |
||
| 227 | * |
||
| 228 | * @param array $config Configuration parameters. |
||
| 229 | * @param ContainerBuilder $container Service container. |
||
| 230 | * |
||
| 231 | * @return Extension $this Fluent interface. |
||
| 232 | */ |
||
| 233 | 8 | protected function configureAccessVoter(array $config, ContainerBuilder $container) |
|
| 254 | |||
| 255 | /** |
||
| 256 | * Configure "RunOpenCode\\Bundle\\ExchangeRate\\Form\\Type\\SourceType" default settings. |
||
| 257 | * |
||
| 258 | * @param array $config Configuration parameters. |
||
| 259 | * @param ContainerBuilder $container Service container. |
||
| 260 | * @return Extension $this Fluent interface. |
||
| 261 | */ |
||
| 262 | 8 | View Code Duplication | protected function configureSourceType(array $config, ContainerBuilder $container) |
| 274 | |||
| 275 | /** |
||
| 276 | * Configure "RunOpenCode\\Bundle\\ExchangeRate\\Form\\Type\\RateTypeType" default settings. |
||
| 277 | * |
||
| 278 | * @param array $config Configuration parameters. |
||
| 279 | * @param ContainerBuilder $container Service container. |
||
| 280 | * @return Extension $this Fluent interface. |
||
| 281 | */ |
||
| 282 | 8 | protected function configureRateTypeType(array $config, ContainerBuilder $container) |
|
| 296 | |||
| 297 | /** |
||
| 298 | * Configure "RunOpenCode\\Bundle\\ExchangeRate\\Form\\Type\\CurrencyCodeType" default settings. |
||
| 299 | * |
||
| 300 | * @param array $config Configuration parameters. |
||
| 301 | * @param ContainerBuilder $container Service container. |
||
| 302 | * @return Extension $this Fluent interface. |
||
| 303 | */ |
||
| 304 | 8 | protected function configureCurrencyCodeType(array $config, ContainerBuilder $container) |
|
| 320 | |||
| 321 | /** |
||
| 322 | * Configure "RunOpenCode\\Bundle\\ExchangeRate\\Form\\Type\\ForeignCurrencyCodeType" default settings. |
||
| 323 | * |
||
| 324 | * @param array $config Configuration parameters. |
||
| 325 | * @param ContainerBuilder $container Service container. |
||
| 326 | * @return Extension $this Fluent interface. |
||
| 327 | */ |
||
| 328 | 8 | View Code Duplication | protected function configureForeignCurrencyCodeType(array $config, ContainerBuilder $container) |
| 342 | |||
| 343 | /** |
||
| 344 | * Configure "RunOpenCode\\Bundle\\ExchangeRate\\Form\\Type\\RateType" default settings. |
||
| 345 | * |
||
| 346 | * @param array $config Configuration parameters. |
||
| 347 | * @param ContainerBuilder $container Service container. |
||
| 348 | * @return Extension $this Fluent interface. |
||
| 349 | */ |
||
| 350 | 8 | protected function configureRateType(array $config, ContainerBuilder $container) |
|
| 356 | } |
||
| 357 |
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: