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 |
||
| 14 | class AdditionalReporterCompilerPassTest extends AbstractCompilerPassTestCase |
||
| 15 | { |
||
| 16 | View Code Duplication | public function testProcessWithAlias() |
|
|
|
|||
| 17 | { |
||
| 18 | $runner = new Definition(); |
||
| 19 | $this->setDefinition('liip_monitor.runner_default', $runner); |
||
| 20 | $this->setParameter('liip_monitor.runners', ['liip_monitor.runner_default']); |
||
| 21 | |||
| 22 | $reporter = new Definition(); |
||
| 23 | $reporter->addTag('liip_monitor.additional_reporter', ['alias' => 'foo']); |
||
| 24 | $this->setDefinition('foo_reporter', $reporter); |
||
| 25 | |||
| 26 | $this->compile(); |
||
| 27 | |||
| 28 | $this->assertContainerBuilderHasServiceDefinitionWithMethodCall( |
||
| 29 | 'liip_monitor.runner_default', |
||
| 30 | 'addAdditionalReporter', |
||
| 31 | [ |
||
| 32 | 'foo', |
||
| 33 | new Reference('foo_reporter'), |
||
| 34 | ] |
||
| 35 | ); |
||
| 36 | } |
||
| 37 | |||
| 38 | View Code Duplication | public function testProcessWithoutAlias() |
|
| 39 | { |
||
| 40 | $runner = new Definition(); |
||
| 41 | $this->setDefinition('liip_monitor.runner_default', $runner); |
||
| 42 | $this->setParameter('liip_monitor.runners', ['liip_monitor.runner_default']); |
||
| 43 | |||
| 44 | $reporter = new Definition(); |
||
| 45 | $reporter->addTag('liip_monitor.additional_reporter'); |
||
| 46 | $this->setDefinition('foo_reporter', $reporter); |
||
| 47 | |||
| 48 | $this->compile(); |
||
| 49 | |||
| 50 | $this->assertContainerBuilderHasServiceDefinitionWithMethodCall( |
||
| 51 | 'liip_monitor.runner_default', |
||
| 52 | 'addAdditionalReporter', |
||
| 53 | [ |
||
| 54 | 'foo_reporter', |
||
| 55 | new Reference('foo_reporter'), |
||
| 56 | ] |
||
| 57 | ); |
||
| 58 | } |
||
| 59 | |||
| 60 | protected function registerCompilerPass(ContainerBuilder $container): void |
||
| 64 | } |
||
| 65 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.