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 IconSubscriber extends AbstractSubscriber |
|
|
|||
25 | { |
||
26 | /** |
||
27 | * @var IconCollector |
||
28 | */ |
||
29 | private $iconCollector; |
||
30 | |||
31 | /** |
||
32 | * @var IconRenderer |
||
33 | */ |
||
34 | private $iconRenderer; |
||
35 | |||
36 | /** |
||
37 | * @param Formatter $formatter |
||
38 | * @param IconCollector $iconCollector |
||
39 | * @param IconRenderer $iconRenderer |
||
40 | */ |
||
41 | 224 | public function __construct( |
|
51 | |||
52 | /** |
||
53 | * @return IconCollector |
||
54 | */ |
||
55 | 220 | public function getIconCollector() |
|
59 | |||
60 | /** |
||
61 | * @param IconCollector $iconCollector |
||
62 | */ |
||
63 | 224 | public function setIconCollector(IconCollector $iconCollector) |
|
67 | |||
68 | /** |
||
69 | * @return IconRenderer |
||
70 | */ |
||
71 | public function getIconRenderer() |
||
75 | |||
76 | /** |
||
77 | * @param IconRenderer $iconRenderer |
||
78 | */ |
||
79 | 224 | public function setIconRenderer(IconRenderer $iconRenderer) |
|
83 | |||
84 | /** |
||
85 | * @param MapEvent $event |
||
86 | */ |
||
87 | 220 | public function handleMap(MapEvent $event) |
|
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.