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 | * Configure sources which does not have to be explicitly added to service container. |
||
| 143 | * |
||
| 144 | * @param array $config Configuration parameters. |
||
| 145 | * @param ContainerBuilder $container Service container. |
||
| 146 | * |
||
| 147 | * @return Extension $this Fluent interface. |
||
| 148 | */ |
||
| 149 | 10 | protected function configureSources(array $config, ContainerBuilder $container) |
|
| 150 | { |
||
| 151 | 10 | if (!empty($config['sources'])) { |
|
| 152 | |||
| 153 | 1 | foreach ($config['sources'] as $class) { |
|
| 154 | |||
| 155 | 1 | $definition = new Definition($class); |
|
| 156 | $definition |
||
| 157 | 1 | ->addTag('runopencode.exchange_rate.source') |
|
| 158 | 1 | ->setPublic(false); |
|
| 159 | |||
| 160 | 1 | $container->setDefinition(sprintf('runopencode.exchange_rate.source.%s', str_replace('\\', '_', strtolower($class))), $definition); |
|
| 161 | } |
||
| 162 | } |
||
| 163 | |||
| 164 | 10 | return $this; |
|
| 165 | } |
||
| 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) |
|
| 246 | |||
| 247 | /** |
||
| 248 | * Configure "RunOpenCode\\Bundle\\ExchangeRate\\Form\\Type\\SourceType" default settings. |
||
| 249 | * |
||
| 250 | * @param array $config Configuration parameters. |
||
| 251 | * @param ContainerBuilder $container Service container. |
||
| 252 | * |
||
| 253 | * @return Extension $this Fluent interface. |
||
| 254 | */ |
||
| 255 | 10 | View Code Duplication | protected function configureSourceType(array $config, ContainerBuilder $container) |
| 267 | |||
| 268 | /** |
||
| 269 | * Configure "RunOpenCode\\Bundle\\ExchangeRate\\Form\\Type\\RateTypeType" default settings. |
||
| 270 | * |
||
| 271 | * @param array $config Configuration parameters. |
||
| 272 | * @param ContainerBuilder $container Service container. |
||
| 273 | * |
||
| 274 | * @return Extension $this Fluent interface. |
||
| 275 | */ |
||
| 276 | 10 | View Code Duplication | protected function configureRateTypeType(array $config, ContainerBuilder $container) |
| 288 | |||
| 289 | /** |
||
| 290 | * Configure "RunOpenCode\\Bundle\\ExchangeRate\\Form\\Type\\CurrencyCodeType" default settings. |
||
| 291 | * |
||
| 292 | * @param array $config Configuration parameters. |
||
| 293 | * @param ContainerBuilder $container Service container. |
||
| 294 | * @return Extension $this Fluent interface. |
||
| 295 | */ |
||
| 296 | 10 | protected function configureCurrencyCodeType(array $config, ContainerBuilder $container) |
|
| 309 | |||
| 310 | /** |
||
| 311 | * Configure "RunOpenCode\\Bundle\\ExchangeRate\\Form\\Type\\ForeignCurrencyCodeType" default settings. |
||
| 312 | * |
||
| 313 | * @param array $config Configuration parameters. |
||
| 314 | * @param ContainerBuilder $container Service container. |
||
| 315 | * |
||
| 316 | * @return Extension $this Fluent interface. |
||
| 317 | */ |
||
| 318 | 10 | View Code Duplication | protected function configureForeignCurrencyCodeType(array $config, ContainerBuilder $container) |
| 331 | |||
| 332 | /** |
||
| 333 | * Configure "RunOpenCode\\Bundle\\ExchangeRate\\Form\\Type\\RateType" default settings. |
||
| 334 | * |
||
| 335 | * @param array $config Configuration parameters. |
||
| 336 | * @param ContainerBuilder $container Service container. |
||
| 337 | * |
||
| 338 | * @return Extension $this Fluent interface. |
||
| 339 | */ |
||
| 340 | 10 | View Code Duplication | protected function configureRateType(array $config, ContainerBuilder $container) |
| 352 | |||
| 353 | /** |
||
| 354 | * Configure notifications |
||
| 355 | * |
||
| 356 | * @param array $config Configuration parameters. |
||
| 357 | * @param ContainerBuilder $container Service container. |
||
| 358 | * |
||
| 359 | * @return Extension $this Fluent interface. |
||
| 360 | */ |
||
| 361 | 10 | protected function configureNotifications(array $config, ContainerBuilder $container) |
|
| 369 | } |
||
| 370 |
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: