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 |
||
14 | class EventStream |
||
15 | { |
||
16 | /** |
||
17 | * @var Connection |
||
18 | */ |
||
19 | protected $connection; |
||
20 | |||
21 | /** |
||
22 | * @var SerializerInterface |
||
23 | */ |
||
24 | protected $payloadSerializer; |
||
25 | |||
26 | /** |
||
27 | * @var SerializerInterface |
||
28 | */ |
||
29 | protected $metadataSerializer; |
||
30 | |||
31 | /** |
||
32 | * @var string |
||
33 | */ |
||
34 | protected $tableName; |
||
35 | |||
36 | /** |
||
37 | * @var int |
||
38 | */ |
||
39 | protected $startId; |
||
40 | |||
41 | /** |
||
42 | * @var int |
||
43 | */ |
||
44 | protected $lastProcessedId; |
||
45 | |||
46 | /** |
||
47 | * @var string[] |
||
48 | */ |
||
49 | protected $cdbids; |
||
50 | |||
51 | /** |
||
52 | * @var EventStreamDecoratorInterface |
||
53 | */ |
||
54 | private $domainEventStreamDecorator; |
||
55 | |||
56 | /** |
||
57 | * @var string |
||
58 | */ |
||
59 | private $aggregateType; |
||
60 | |||
61 | /** |
||
62 | * @param Connection $connection |
||
63 | * @param SerializerInterface $payloadSerializer |
||
64 | * @param SerializerInterface $metadataSerializer |
||
65 | * @param string $tableName |
||
66 | */ |
||
67 | View Code Duplication | public function __construct( |
|
80 | |||
81 | /** |
||
82 | * @param int $startId |
||
83 | * @return EventStream |
||
84 | */ |
||
85 | public function withStartId($startId) |
||
99 | |||
100 | /** |
||
101 | * @param string $aggregateType |
||
102 | * @return EventStream |
||
103 | */ |
||
104 | public function withAggregateType($aggregateType) |
||
110 | |||
111 | |||
112 | /** |
||
113 | * @param string[] $cdbids |
||
114 | * @return EventStream |
||
115 | */ |
||
116 | public function withCdbids($cdbids) |
||
130 | |||
131 | /** |
||
132 | * @param EventStreamDecoratorInterface $domainEventStreamDecorator |
||
133 | * @return EventStream |
||
134 | */ |
||
135 | public function withDomainEventStreamDecorator(EventStreamDecoratorInterface $domainEventStreamDecorator) |
||
141 | |||
142 | public function __invoke() |
||
177 | |||
178 | /** |
||
179 | * @return int |
||
180 | */ |
||
181 | public function getLastProcessedId() |
||
185 | |||
186 | /** |
||
187 | * The load statement can no longer be 'cached' because of using the query |
||
188 | * builder. The query builder requires all parameters to be set before |
||
189 | * using the execute command. The previous solution used the prepare |
||
190 | * statement on the connection, this did not require all parameters to be |
||
191 | * set up front. |
||
192 | * |
||
193 | * @return Statement |
||
194 | * @throws DBALException |
||
195 | */ |
||
196 | protected function prepareLoadStatement() |
||
219 | |||
220 | /** |
||
221 | * @param $row |
||
222 | * @return DomainMessage |
||
223 | */ |
||
224 | View Code Duplication | private function deserializeEvent($row) |
|
234 | } |
||
235 |
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.