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 |
||
5 | abstract class EventSourcedAggregateRoot implements EventSourcedAggregateRootInterface |
||
6 | { |
||
7 | /** |
||
8 | * @var EventStream |
||
9 | */ |
||
10 | private $changes; |
||
11 | |||
12 | /** |
||
13 | * @var int |
||
14 | */ |
||
15 | private $version = 0; |
||
16 | |||
17 | /** |
||
18 | * @param mixed $domainEvent |
||
19 | * @throws DomainEventNotUnderstandableException |
||
20 | */ |
||
21 | 15 | public function applyAndRecord($domainEvent) |
|
27 | |||
28 | /** |
||
29 | * Record a Domain Event |
||
30 | * |
||
31 | * @param mixed $domainEvent |
||
32 | */ |
||
33 | 15 | private function record($domainEvent) |
|
38 | |||
39 | 15 | private function ensureChangesEventStream() |
|
45 | |||
46 | /** |
||
47 | * Apply a Domain Event to the aggregate |
||
48 | * |
||
49 | * @param mixed $domainEvent |
||
50 | */ |
||
51 | 25 | public function apply($domainEvent) |
|
61 | |||
62 | /** |
||
63 | * @param $domainEvent |
||
64 | * @return DomainEvent |
||
65 | */ |
||
66 | 25 | private function ensureDomainEvent($domainEvent) |
|
73 | |||
74 | /** |
||
75 | * @param string $eventHandlerName |
||
76 | * @param mixed $domainEventData |
||
77 | * @throws DomainEventNotUnderstandableException |
||
78 | */ |
||
79 | 3 | private function applyRecursively($eventHandlerName, $domainEventData) |
|
107 | |||
108 | /** |
||
109 | * @param DomainEvent $domainEvent |
||
110 | * @return string |
||
111 | */ |
||
112 | 25 | private function getEventHandlerName($domainEvent) |
|
113 | { |
||
114 | 25 | return $this->eventHandlerPrefix() . (new \ReflectionClass($domainEvent->data()))->getShortName(); |
|
115 | } |
||
116 | |||
117 | /** |
||
118 | * @return string |
||
119 | */ |
||
120 | 25 | protected function eventHandlerPrefix() |
|
121 | { |
||
122 | 25 | return 'when'; |
|
123 | } |
||
124 | |||
125 | /** |
||
126 | * @param object $entity |
||
127 | * @param string $eventHandlerName |
||
128 | * @param DomainEvent $domainEvent |
||
129 | */ |
||
130 | 24 | private function executeEventHandler($entity, $eventHandlerName, $domainEvent) |
|
135 | |||
136 | 24 | private function increaseAggregateVersion() |
|
140 | |||
141 | /** |
||
142 | * Recorded stream of Domain Events |
||
143 | * |
||
144 | * @return EventStream |
||
145 | */ |
||
146 | 18 | public function changes() |
|
150 | |||
151 | /** |
||
152 | * Current version of the aggregate |
||
153 | * |
||
154 | * @return int |
||
155 | */ |
||
156 | 8 | public function version() |
|
160 | |||
161 | /** |
||
162 | * The version of the aggregate before applying changes |
||
163 | * |
||
164 | * @return int |
||
165 | */ |
||
166 | 5 | public function originalVersion() |
|
170 | |||
171 | /** |
||
172 | * Removes recorded domain events |
||
173 | */ |
||
174 | 19 | public function clearChanges() |
|
178 | } |
||
179 |
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.