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 |
||
9 | class BufferedEventDispatcher implements EventDispatcherInterface, ListenerRegistry |
||
10 | { |
||
11 | use EventGeneratorBehavior { |
||
12 | recordEvent as protected; |
||
13 | releaseEvents as protected; |
||
14 | } |
||
15 | |||
16 | /** |
||
17 | * @var EventDispatcherInterface |
||
18 | */ |
||
19 | private $dispatcher; |
||
20 | |||
21 | public function __construct(EventDispatcherInterface $dispatcher) |
||
25 | |||
26 | public function dispatch(object $event): object |
||
32 | |||
33 | /** |
||
34 | * @return object[] |
||
35 | */ |
||
36 | public function dispatchBufferedEvents(): array |
||
46 | |||
47 | View Code Duplication | public function subscribeTo(string $event, callable $listener, int $priority = ListenerPriority::NORMAL): void |
|
55 | |||
56 | View Code Duplication | public function subscribeOnceTo(string $event, callable $listener, int $priority = ListenerPriority::NORMAL): void |
|
64 | |||
65 | public function subscribeListenersFrom(ListenerSubscriber $subscriber): void |
||
73 | } |
||
74 |
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.