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 |
||
25 | final class Projectionist implements ProgressStateProvider |
||
26 | { |
||
27 | |||
28 | use ProgressStateProviderMethods; |
||
29 | |||
30 | /** |
||
31 | * @var EventStore |
||
32 | */ |
||
33 | private $eventStore; |
||
34 | |||
35 | /** |
||
36 | * @var ProjectorStateLedger |
||
37 | */ |
||
38 | private $ledger; |
||
39 | |||
40 | /** |
||
41 | * @var EventHandlingStrategy |
||
42 | */ |
||
43 | private $strategy; |
||
44 | |||
45 | /** |
||
46 | * Creates a Projectionist |
||
47 | * |
||
48 | * @param EventStore $eventStore |
||
49 | * @param ProjectorStateLedger $ledger |
||
50 | * @param EventHandlingStrategy $strategy |
||
51 | */ |
||
52 | public function __construct(EventStore $eventStore, ProjectorStateLedger $ledger, EventHandlingStrategy $strategy) |
||
58 | |||
59 | /** |
||
60 | * Plays provided list of projectors |
||
61 | * |
||
62 | * @param Projector[] $projectors |
||
63 | * @throws Exception |
||
64 | */ |
||
65 | View Code Duplication | public function play(array $projectors): void |
|
83 | |||
84 | /** |
||
85 | * Boots provided list of projectors |
||
86 | * |
||
87 | * @param Projector[] $projectors |
||
88 | * @throws Exception |
||
89 | */ |
||
90 | View Code Duplication | public function boot(array $projectors): void |
|
103 | |||
104 | /** |
||
105 | * Retires provided list of projectors |
||
106 | * |
||
107 | * @param Projector[] $projectors |
||
108 | */ |
||
109 | public function retire(array $projectors): void |
||
118 | |||
119 | /** |
||
120 | * Project stream into provided projector |
||
121 | * |
||
122 | * @param Projector $projector |
||
123 | * @param ProjectorState $projectorState |
||
124 | * |
||
125 | * @throws Exception |
||
126 | */ |
||
127 | private function project(Projector $projector, ProjectorState $projectorState): void |
||
152 | |||
153 | /** |
||
154 | * Retrieve the event stream for provided projector state |
||
155 | * |
||
156 | * @param ProjectorState $projectorState |
||
157 | * |
||
158 | * @return EventStream |
||
159 | * @throws Exception |
||
160 | */ |
||
161 | private function eventStream(ProjectorState $projectorState): Stream |
||
184 | |||
185 | /** |
||
186 | * Secure projector to avoid duplications |
||
187 | * |
||
188 | * @param ProjectorState $projectorState |
||
189 | */ |
||
190 | private function secureProjector(ProjectorState $projectorState): void |
||
195 | |||
196 | /** |
||
197 | * Releases the projector so that it can be played by other agent |
||
198 | * |
||
199 | * @param ProjectorState $projectorState |
||
200 | */ |
||
201 | private function releaseProjector(ProjectorState $projectorState): void |
||
206 | } |
||
207 |
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.