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 |
||
8 | trait EmitsEvents |
||
9 | { |
||
10 | /** |
||
11 | * @var Emitter |
||
12 | */ |
||
13 | private $eventEmitter; |
||
14 | |||
15 | /** |
||
16 | * @param EventInterface|string $event |
||
17 | * @throws \InvalidArgumentException |
||
18 | * @return bool true if emitted, false otherwise |
||
19 | */ |
||
20 | 6 | View Code Duplication | private function emitEvent($event) |
34 | |||
35 | /** |
||
36 | * @param EventInterface[]|string[] $events |
||
37 | * @throws \InvalidArgumentException |
||
38 | * @return bool true if all emitted, false otherwise |
||
39 | */ |
||
40 | View Code Duplication | private function emitBatchOfEvents(array $events) |
|
56 | |||
57 | /** |
||
58 | * @return Emitter |
||
59 | */ |
||
60 | public function getEventEmitter() |
||
64 | |||
65 | /** |
||
66 | * @param Emitter $eventEmitter |
||
67 | */ |
||
68 | public function setEventEmitter($eventEmitter) |
||
72 | } |
||
73 |
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.