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 |
||
11 | trait AdvanceEventEmitterTrait |
||
12 | { |
||
13 | /** |
||
14 | * @var callable[] |
||
15 | */ |
||
16 | protected $listeners = []; |
||
17 | |||
18 | /** |
||
19 | * @param string $eventName |
||
20 | * @param callable $listener |
||
21 | * |
||
22 | * @return $this |
||
23 | */ |
||
24 | 14 | public function on(string $eventName, callable $listener) : self |
|
30 | |||
31 | /** |
||
32 | * @param string $eventName |
||
33 | * @param callable $listener |
||
34 | * |
||
35 | * @return $this |
||
36 | */ |
||
37 | View Code Duplication | public function once(string $eventName, callable $listener) : self |
|
49 | |||
50 | /** |
||
51 | * @param string $eventName |
||
52 | * @param callable $listener |
||
53 | * |
||
54 | * @return $this |
||
55 | */ |
||
56 | 4 | View Code Duplication | public function removeListener(string $eventName, callable $listener) : self |
68 | |||
69 | /** |
||
70 | * @param string $eventName |
||
71 | * |
||
72 | * @return $this |
||
73 | */ |
||
74 | 3 | public function removeListeners(string $eventName = null) : self |
|
84 | |||
85 | /** |
||
86 | * @param string $eventName |
||
87 | * |
||
88 | * @return array|callable[] |
||
89 | */ |
||
90 | 12 | public function getListeners(string $eventName) : array |
|
94 | |||
95 | /** |
||
96 | * @param string $eventName |
||
97 | * @param EventInterface $event |
||
98 | * |
||
99 | * @return $this |
||
100 | */ |
||
101 | 12 | public function emit(string $eventName, EventInterface $event = null) : self |
|
115 | } |
||
116 |
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.