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 |
||
27 | class DefaultEventHandler implements EventSubscriberInterface, EventHandlerInterface |
||
28 | { |
||
29 | /** |
||
30 | * @var callback |
||
31 | */ |
||
32 | protected $preDispatchHandler; |
||
33 | |||
34 | /** |
||
35 | * @var callback |
||
36 | */ |
||
37 | protected $postDispatchHandler; |
||
38 | |||
39 | /** |
||
40 | * @var IO |
||
41 | */ |
||
42 | protected $io; |
||
43 | |||
44 | /** |
||
45 | * @param IO $io |
||
46 | */ |
||
47 | public function setIo(IO $io) |
||
51 | |||
52 | /** |
||
53 | * @param callable $handler |
||
54 | * |
||
55 | * @return $this |
||
56 | */ |
||
57 | View Code Duplication | public function addPreDispatchEventHandler($handler) |
|
70 | |||
71 | /** |
||
72 | * @param callable $handler |
||
73 | * |
||
74 | * @return $this |
||
75 | */ |
||
76 | View Code Duplication | public function addPostDispatchEventHandler($handler) |
|
89 | |||
90 | /** |
||
91 | * @return $this |
||
92 | */ |
||
93 | public function clearEventHandlers() |
||
100 | |||
101 | /** |
||
102 | * @param PreDispatchEvent $preDispatchEvent |
||
103 | */ |
||
104 | public function onPreDispatchEvent(PreDispatchEvent $preDispatchEvent) |
||
123 | |||
124 | /** |
||
125 | * @param PostDispatchEvent $postDispatchEvent |
||
126 | */ |
||
127 | public function onPostDispatchEvent(PostDispatchEvent $postDispatchEvent) |
||
136 | |||
137 | /** |
||
138 | * @param EventInterface $event |
||
139 | * |
||
140 | * @return array |
||
141 | */ |
||
142 | protected function objectToPropertyArray(EventInterface $event) |
||
163 | |||
164 | /** |
||
165 | * @param EventInterface $event |
||
166 | * |
||
167 | * @return array |
||
168 | */ |
||
169 | protected function eventToString(EventInterface $event) |
||
180 | |||
181 | /** |
||
182 | * {@inheritdoc} |
||
183 | */ |
||
184 | public static function getSubscribedEvents() |
||
191 | } |
||
192 |
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.