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 |
||
16 | class MySqlJsonEventStore extends AbstractEventStore implements InitializableInterface |
||
17 | { |
||
18 | const MAX_UNSIGNED_BIG_INT = 9223372036854775807; |
||
19 | const STREAMS_TABLE = 'streams'; |
||
20 | const EVENTS_TABLE = 'events'; |
||
21 | |||
22 | /** |
||
23 | * @var \PDO |
||
24 | */ |
||
25 | private $connection; |
||
26 | |||
27 | /** |
||
28 | * @param \PDO $connection |
||
29 | * @param SerializerInterface $serializer |
||
30 | * @param EventUpgrader $eventUpgrader |
||
31 | */ |
||
32 | 22 | public function __construct( |
|
40 | |||
41 | /** |
||
42 | * @param string $streamId |
||
43 | * @param int $start |
||
44 | * @param int $count |
||
45 | * @return EventStreamInterface |
||
46 | */ |
||
47 | 4 | View Code Duplication | public function readStreamEvents($streamId, $start = 1, $count = null) |
79 | |||
80 | /** |
||
81 | * @param string $streamId |
||
82 | * @return EventStreamInterface |
||
83 | */ |
||
84 | 7 | View Code Duplication | public function readFullStream($streamId) |
109 | |||
110 | /** |
||
111 | * @return EventStreamInterface[] |
||
112 | */ |
||
113 | 1 | View Code Duplication | public function readAllStreams() |
127 | |||
128 | /** |
||
129 | * @return EventStreamInterface |
||
130 | */ |
||
131 | 1 | View Code Duplication | public function readAllEvents() |
154 | |||
155 | /** |
||
156 | * @param string $streamId |
||
157 | * @param StoredEvent[] $storedEvents |
||
158 | * @param int $expectedVersion |
||
159 | * @throws \Exception |
||
160 | */ |
||
161 | 12 | protected function appendStoredEvents($streamId, $storedEvents, $expectedVersion) |
|
162 | { |
||
163 | 12 | $this->connection->beginTransaction(); |
|
164 | try { |
||
165 | 12 | View Code Duplication | if (!$this->streamExists($streamId)) { |
166 | 12 | $stmt = $this->connection |
|
167 | 12 | ->prepare('INSERT INTO streams (id) VALUES (:streamId)'); |
|
168 | 12 | $stmt->bindValue(':streamId', $streamId); |
|
169 | 12 | $stmt->execute(); |
|
170 | } |
||
171 | 12 | View Code Duplication | foreach ($storedEvents as $storedEvent) { |
172 | 12 | $stmt = $this->connection->prepare( |
|
173 | 'INSERT INTO events (stream_id, type, event, metadata, occurred_on, version) |
||
174 | 12 | VALUES (:streamId, :type, :event, :metadata, :occurredOn, :version)' |
|
175 | ); |
||
176 | 12 | $stmt->bindValue(':streamId', $streamId); |
|
177 | 12 | $stmt->bindValue(':type', $storedEvent->type()); |
|
178 | 12 | $stmt->bindValue(':event', $storedEvent->data()); |
|
179 | 12 | $stmt->bindValue(':metadata', $storedEvent->metadata()); |
|
180 | 12 | $stmt->bindValue(':occurredOn', $storedEvent->occurredOn()->format('Y-m-d H:i:s')); |
|
181 | 12 | $stmt->bindValue(':version', $storedEvent->version()); |
|
182 | 12 | $stmt->execute(); |
|
183 | } |
||
184 | 12 | $streamFinalVersion = $this->streamVersion($streamId); |
|
185 | 12 | if (count($storedEvents) !== $streamFinalVersion - $expectedVersion) { |
|
186 | 1 | throw ConcurrencyException::fromVersions( |
|
187 | 1 | $this->streamVersion($streamId), |
|
188 | $expectedVersion |
||
189 | ); |
||
190 | } |
||
191 | 11 | $this->connection->commit(); |
|
192 | 1 | } catch (\Exception $e) { |
|
193 | 1 | $this->connection->rollBack(); |
|
194 | 1 | throw $e; |
|
195 | } |
||
196 | 11 | } |
|
197 | |||
198 | /** |
||
199 | * @param string $streamId |
||
200 | * @return bool |
||
201 | */ |
||
202 | 14 | View Code Duplication | protected function streamExists($streamId) |
210 | /** |
||
211 | * @param string $streamId |
||
212 | * @return int |
||
213 | */ |
||
214 | 11 | View Code Duplication | protected function streamVersion($streamId) |
222 | |||
223 | /** |
||
224 | * @param string $type |
||
225 | * @param Version $version |
||
226 | * @return EventStreamInterface |
||
227 | */ |
||
228 | 1 | View Code Duplication | protected function readStoredEventsOfTypeAndVersion($type, $version) |
255 | |||
256 | 22 | public function initialize() |
|
257 | { |
||
258 | try { |
||
259 | 22 | $this->connection->beginTransaction(); |
|
260 | |||
261 | 22 | $this->connection->exec( |
|
262 | 22 | 'CREATE TABLE `'.self::STREAMS_TABLE.'` ( |
|
263 | `id` varchar(255) NOT NULL, |
||
264 | PRIMARY KEY (`id`) |
||
265 | 22 | )' |
|
266 | ); |
||
267 | |||
268 | 22 | $this->connection->exec( |
|
269 | 22 | 'CREATE TABLE `'.self::EVENTS_TABLE.'` ( |
|
270 | `id` int(11) NOT NULL AUTO_INCREMENT, |
||
271 | `stream_id` varchar(255) NOT NULL, |
||
272 | `type` varchar(255) NOT NULL, |
||
273 | `event` json NOT NULL, |
||
274 | `metadata` json NOT NULL, |
||
275 | `occurred_on` datetime NOT NULL, |
||
276 | `version` varchar(255) NOT NULL, |
||
277 | PRIMARY KEY (`id`), |
||
278 | KEY `stream_id` (`stream_id`), |
||
279 | CONSTRAINT `events_ibfk_1` FOREIGN KEY (`stream_id`) REFERENCES `streams` (`id`) |
||
280 | 22 | )' |
|
281 | ); |
||
282 | |||
283 | 22 | $this->connection->commit(); |
|
284 | 1 | } catch (\Exception $e) { |
|
285 | 1 | $this->connection->rollBack(); |
|
286 | 1 | throw $e; |
|
287 | } |
||
288 | 22 | } |
|
289 | |||
290 | /** |
||
291 | * @return bool |
||
292 | */ |
||
293 | 3 | View Code Duplication | public function initialized() |
302 | |||
303 | /** |
||
304 | * @param string $streamId |
||
305 | * @param \DateTimeImmutable $datetime |
||
306 | * @return int |
||
307 | * @throws EventStreamDoesNotExistException |
||
308 | */ |
||
309 | 3 | View Code Duplication | public function getStreamVersionAt($streamId, \DateTimeImmutable $datetime) |
325 | } |
||
326 |
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.