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 AutocompleteSimpleEventSubscriber extends AbstractSubscriber |
|
|
|||
25 | { |
||
26 | /** |
||
27 | * @var AutocompleteEventCollector |
||
28 | */ |
||
29 | private $eventCollector; |
||
30 | |||
31 | /** |
||
32 | * @var EventRenderer |
||
33 | */ |
||
34 | private $eventRenderer; |
||
35 | |||
36 | /** |
||
37 | * @param Formatter $formatter |
||
38 | * @param AutocompleteEventCollector $eventCollector |
||
39 | * @param EventRenderer $eventRenderer |
||
40 | */ |
||
41 | public function __construct( |
||
51 | |||
52 | /** |
||
53 | * @return AutocompleteEventCollector |
||
54 | */ |
||
55 | public function getEventCollector() |
||
59 | |||
60 | /** |
||
61 | * @param AutocompleteEventCollector $eventCollector |
||
62 | */ |
||
63 | public function setEventCollector(AutocompleteEventCollector $eventCollector) |
||
67 | |||
68 | /** |
||
69 | * @return EventRenderer |
||
70 | */ |
||
71 | public function getEventRenderer() |
||
75 | |||
76 | /** |
||
77 | * @param EventRenderer $eventRenderer |
||
78 | */ |
||
79 | public function setEventRenderer(EventRenderer $eventRenderer) |
||
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.