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 |
||
7 | class EventUpgrader implements EventUpgraderInterface |
||
8 | { |
||
9 | /** |
||
10 | * @var array |
||
11 | */ |
||
12 | private $upgrades; |
||
13 | |||
14 | /** |
||
15 | * @var EventAdapter |
||
16 | */ |
||
17 | private $eventAdapter; |
||
18 | |||
19 | /** |
||
20 | * @param EventAdapter $eventAdapter |
||
21 | */ |
||
22 | 80 | public function __construct(EventAdapter $eventAdapter) |
|
26 | |||
27 | /** |
||
28 | * @param UpgradeInterface $upgrade |
||
29 | */ |
||
30 | 72 | public function registerUpgrade(UpgradeInterface $upgrade) |
|
34 | |||
35 | /** |
||
36 | * @param StoredEvent $storedEvent |
||
37 | * @param Version $version |
||
38 | */ |
||
39 | 41 | public function migrate(StoredEvent $storedEvent, $version = null) |
|
51 | |||
52 | /** |
||
53 | * @param StoredEvent $storedEvent |
||
54 | * @param Version $version |
||
55 | */ |
||
56 | 37 | View Code Duplication | public function upgrade(StoredEvent $storedEvent, $version = null) |
71 | |||
72 | /** |
||
73 | * @param StoredEvent $storedEvent |
||
74 | * @param Version $version |
||
75 | */ |
||
76 | 4 | View Code Duplication | public function downgrade(StoredEvent $storedEvent, $version = null) |
92 | } |
||
93 |
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.