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 |
||
| 24 | View Code Duplication | class KmlLayerSubscriber extends AbstractSubscriber |
|
|
|
|||
| 25 | { |
||
| 26 | /** |
||
| 27 | * @var KmlLayerCollector |
||
| 28 | */ |
||
| 29 | private $kmlLayerCollector; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var KmlLayerRenderer |
||
| 33 | */ |
||
| 34 | private $kmlLayerRenderer; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @param Formatter $formatter |
||
| 38 | * @param KmlLayerCollector $kmlLayerCollector |
||
| 39 | * @param KmlLayerRenderer $kmlLayerRenderer |
||
| 40 | */ |
||
| 41 | 224 | public function __construct( |
|
| 51 | |||
| 52 | /** |
||
| 53 | * @return KmlLayerCollector |
||
| 54 | */ |
||
| 55 | public function getKmlLayerCollector() |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @param KmlLayerCollector $kmlLayerCollector |
||
| 62 | */ |
||
| 63 | 224 | public function setKmlLayerCollector(KmlLayerCollector $kmlLayerCollector) |
|
| 67 | |||
| 68 | /** |
||
| 69 | * @return KmlLayerRenderer |
||
| 70 | */ |
||
| 71 | public function getKmlLayerRenderer() |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @param KmlLayerRenderer $kmlLayerRenderer |
||
| 78 | */ |
||
| 79 | 224 | public function setKmlLayerRenderer(KmlLayerRenderer $kmlLayerRenderer) |
|
| 83 | |||
| 84 | /** |
||
| 85 | * @param MapEvent $event |
||
| 86 | */ |
||
| 87 | 220 | public function handleMap(MapEvent $event) |
|
| 88 | { |
||
| 89 | 220 | $formatter = $this->getFormatter(); |
|
| 90 | 220 | $map = $event->getMap(); |
|
| 91 | |||
| 92 | 220 | foreach ($this->kmlLayerCollector->collect($map) as $kmlLayer) { |
|
| 93 | 8 | $event->addCode($formatter->renderContainerAssignment( |
|
| 94 | 4 | $map, |
|
| 95 | 8 | $this->kmlLayerRenderer->render($kmlLayer, $map), |
|
| 96 | 8 | 'layers.kml_layers', |
|
| 97 | 4 | $kmlLayer |
|
| 98 | 4 | )); |
|
| 99 | 110 | } |
|
| 100 | 220 | } |
|
| 101 | |||
| 102 | /** |
||
| 103 | * {@inheritdoc} |
||
| 104 | */ |
||
| 105 | 224 | public static function getSubscribedEvents() |
|
| 109 | } |
||
| 110 |
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.