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 |
||
26 | class EventBus extends Bus |
||
27 | { |
||
28 | /** |
||
29 | * @var EventDispatcherMiddleware |
||
30 | */ |
||
31 | protected $dispatcherMiddleware; |
||
32 | |||
33 | /** |
||
34 | * @return EventBus |
||
35 | */ |
||
36 | public static function create() |
||
43 | |||
44 | /** |
||
45 | * {@inheritdoc} |
||
46 | */ |
||
47 | public function dispatch(MessageInterface $event) |
||
63 | |||
64 | /** |
||
65 | * Ensure that exists an dispatcher middleware. |
||
66 | * |
||
67 | * @throws InvalidArgumentException |
||
68 | */ |
||
69 | protected function ensureEventDispatcherMiddleware() |
||
87 | |||
88 | /** |
||
89 | * @return EventDispatcherMiddleware |
||
90 | */ |
||
91 | public function dispatcherMiddleware() |
||
97 | |||
98 | /** |
||
99 | * Adds an event listener that listens on the specified events. The higher priority value, the earlier an event |
||
100 | * listener will be triggered in the chain (defaults to 0). |
||
101 | * |
||
102 | * @param string $eventName |
||
103 | * @param callable $listener |
||
104 | * @param int $priority |
||
105 | * |
||
106 | * @return $this |
||
107 | */ |
||
108 | public function addListener($eventName, callable $listener, $priority = 0) |
||
114 | |||
115 | /** |
||
116 | * Removes an event listener from the specified events. |
||
117 | * |
||
118 | * @param string $eventName |
||
119 | * @param callable $listener |
||
120 | * |
||
121 | * @return $this |
||
122 | */ |
||
123 | public function removeListener($eventName, callable $listener) |
||
129 | |||
130 | /** |
||
131 | * Adds an event subscriber. The subscriber is asked for all the events he is |
||
132 | * interested in and added as a listener for these events. |
||
133 | * |
||
134 | * @param EventSubscriberInterface $subscriber |
||
135 | * |
||
136 | * @return $this |
||
137 | */ |
||
138 | public function addSubscriber(EventSubscriberInterface $subscriber) |
||
144 | |||
145 | /** |
||
146 | * Removes an event subscriber. |
||
147 | * |
||
148 | * @param EventSubscriberInterface $subscriber |
||
149 | * |
||
150 | * @return $this |
||
151 | */ |
||
152 | public function removeSubscriber(EventSubscriberInterface $subscriber) |
||
158 | |||
159 | /** |
||
160 | * Gets the list of event listeners. |
||
161 | * |
||
162 | * @return array |
||
163 | */ |
||
164 | public function listeners() |
||
170 | |||
171 | /** |
||
172 | * Checks whether an event has any registered listeners. |
||
173 | * |
||
174 | * @param string $eventName |
||
175 | * |
||
176 | * @return bool |
||
177 | */ |
||
178 | public function hasEventListeners($eventName) |
||
184 | |||
185 | /** |
||
186 | * Checks whether has any registered listener. |
||
187 | * |
||
188 | * @return bool |
||
189 | */ |
||
190 | public function hasListeners() |
||
196 | } |
||
197 |
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.