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 |
||
23 | View Code Duplication | class InfoWindowCloseSubscriber extends AbstractInfoWindowSubscriber |
|
|
|||
24 | { |
||
25 | /** |
||
26 | * @var InfoWindowCloseRenderer |
||
27 | */ |
||
28 | private $infoWindowCloseRenderer; |
||
29 | |||
30 | /** |
||
31 | * @param Formatter $formatter |
||
32 | * @param InfoWindowCollector $infoWindowCollector |
||
33 | * @param InfoWindowCloseRenderer $infoWindowCloseRenderer |
||
34 | */ |
||
35 | 4 | public function __construct( |
|
44 | |||
45 | /** |
||
46 | * @return InfoWindowCloseRenderer |
||
47 | */ |
||
48 | public function getInfoWindowCloseRenderer() |
||
52 | |||
53 | /** |
||
54 | * @param InfoWindowCloseRenderer $infoWindowCloseRenderer |
||
55 | */ |
||
56 | 4 | public function setInfoWindowCloseRenderer(InfoWindowCloseRenderer $infoWindowCloseRenderer) |
|
60 | |||
61 | /** |
||
62 | * @param MapEvent $event |
||
63 | */ |
||
64 | public function handleMap(MapEvent $event) |
||
65 | { |
||
66 | $formatter = $this->getFormatter(); |
||
67 | $map = $event->getMap(); |
||
68 | $codes = []; |
||
69 | |||
70 | foreach ($this->getInfoWindowCollector()->collect($map) as $infoWindow) { |
||
71 | if ($infoWindow->isAutoClose()) { |
||
72 | $codes[] = $formatter->renderCode($this->infoWindowCloseRenderer->render($infoWindow), true, false); |
||
73 | } |
||
74 | } |
||
75 | |||
76 | $event->addCode($formatter->renderContainerAssignment( |
||
77 | $map, |
||
78 | $formatter->renderClosure($formatter->renderLines($codes)), |
||
79 | 'functions.info_windows_close' |
||
80 | )); |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * {@inheritdoc} |
||
85 | */ |
||
86 | 4 | public static function getSubscribedEvents() |
|
90 | } |
||
91 |
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.