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 AutocompleteDomEventSubscriber extends AbstractSubscriber |
|
|
|||
25 | { |
||
26 | /** |
||
27 | * @var AutocompleteDomEventCollector |
||
28 | */ |
||
29 | private $domEventCollector; |
||
30 | |||
31 | /** |
||
32 | * @var DomEventRenderer |
||
33 | */ |
||
34 | private $domEventRenderer; |
||
35 | |||
36 | /** |
||
37 | * @param Formatter $formatter |
||
38 | * @param AutocompleteDomEventCollector $domEventCollector |
||
39 | * @param DomEventRenderer $domEventRenderer |
||
40 | */ |
||
41 | public function __construct( |
||
51 | |||
52 | /** |
||
53 | * @return AutocompleteDomEventCollector |
||
54 | */ |
||
55 | public function getDomEventCollector() |
||
59 | |||
60 | /** |
||
61 | * @param AutocompleteDomEventCollector $domEventCollector |
||
62 | */ |
||
63 | public function setDomEventCollector(AutocompleteDomEventCollector $domEventCollector) |
||
67 | |||
68 | /** |
||
69 | * @return DomEventRenderer |
||
70 | */ |
||
71 | public function getDomEventRenderer() |
||
75 | |||
76 | /** |
||
77 | * @param DomEventRenderer $domEventRenderer |
||
78 | */ |
||
79 | public function setDomEventRenderer(DomEventRenderer $domEventRenderer) |
||
83 | |||
84 | /** |
||
85 | * @param PlaceAutocompleteEvent $event |
||
86 | */ |
||
87 | public function handleAutocomplete(PlaceAutocompleteEvent $event) |
||
101 | |||
102 | /** |
||
103 | * {@inheritdoc} |
||
104 | */ |
||
105 | 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.