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 PointSubscriber extends AbstractSubscriber |
|
|
|||
25 | { |
||
26 | /** |
||
27 | * @var PointCollector |
||
28 | */ |
||
29 | private $pointCollector; |
||
30 | |||
31 | /** |
||
32 | * @var PointRenderer |
||
33 | */ |
||
34 | private $pointRenderer; |
||
35 | |||
36 | /** |
||
37 | * @param Formatter $formatter |
||
38 | * @param PointCollector $pointCollector |
||
39 | * @param PointRenderer $pointRenderer |
||
40 | */ |
||
41 | 220 | public function __construct( |
|
51 | |||
52 | /** |
||
53 | * @return PointCollector |
||
54 | */ |
||
55 | public function getPointCollector() |
||
59 | |||
60 | /** |
||
61 | * @param PointCollector $pointCollector |
||
62 | */ |
||
63 | 220 | public function setPointCollector(PointCollector $pointCollector) |
|
67 | |||
68 | /** |
||
69 | * @return PointRenderer |
||
70 | */ |
||
71 | public function getPointRenderer() |
||
75 | |||
76 | /** |
||
77 | * @param PointRenderer $pointRenderer |
||
78 | */ |
||
79 | 220 | public function setPointRenderer(PointRenderer $pointRenderer) |
|
83 | |||
84 | /*** |
||
85 | * @param MapEvent $event |
||
86 | */ |
||
87 | 216 | public function handleMap(MapEvent $event) |
|
88 | { |
||
89 | 216 | $formatter = $this->getFormatter(); |
|
90 | 216 | $map = $event->getMap(); |
|
91 | |||
92 | 216 | foreach ($this->pointCollector->collect($map) as $point) { |
|
93 | $event->addCode($formatter->renderContainerAssignment( |
||
94 | $map, |
||
95 | $this->pointRenderer->render($point), |
||
96 | 'base.points', |
||
97 | $point |
||
98 | )); |
||
99 | 108 | } |
|
100 | 216 | } |
|
101 | |||
102 | /** |
||
103 | * {@inheritdoc} |
||
104 | */ |
||
105 | 220 | 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.