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 QueryBuilder |
||
38 | */ |
||
39 | protected $queryBuilder; |
||
40 | |||
41 | /** |
||
42 | * @var int |
||
43 | */ |
||
44 | protected $previousId; |
||
45 | |||
46 | /** |
||
47 | * @var string |
||
48 | */ |
||
49 | protected $aggregateType; |
||
50 | |||
51 | /** |
||
52 | * @var EventStreamDecoratorInterface |
||
53 | */ |
||
54 | private $domainEventStreamDecorator; |
||
55 | |||
56 | /** |
||
57 | * @param Connection $connection |
||
58 | * @param SerializerInterface $payloadSerializer |
||
59 | * @param SerializerInterface $metadataSerializer |
||
60 | * @param string $tableName |
||
61 | */ |
||
62 | public function __construct( |
||
77 | |||
78 | /** |
||
79 | * @param int $startId |
||
80 | * @return EventStream |
||
81 | */ |
||
82 | public function withStartId($startId) |
||
88 | |||
89 | /** |
||
90 | * @param string $aggregateType |
||
91 | * @return EventStream |
||
92 | */ |
||
93 | public function withAggregateType($aggregateType) |
||
99 | |||
100 | /** |
||
101 | * @param EventStreamDecoratorInterface $domainEventStreamDecorator |
||
102 | * @return EventStream |
||
103 | */ |
||
104 | public function withDomainEventStreamDecorator(EventStreamDecoratorInterface $domainEventStreamDecorator) |
||
110 | |||
111 | public function __invoke() |
||
147 | |||
148 | /** |
||
149 | * @return int |
||
150 | */ |
||
151 | public function getPreviousId() |
||
155 | |||
156 | /** |
||
157 | * @return QueryBuilder |
||
158 | * @throws DBALException |
||
159 | */ |
||
160 | protected function prepareLoadQuery() |
||
161 | { |
||
162 | if (null === $this->queryBuilder) { |
||
163 | $this->queryBuilder = $this->connection->createQueryBuilder(); |
||
164 | |||
165 | $this->queryBuilder->select( |
||
166 | [ |
||
167 | $this->primaryKey, |
||
168 | 'uuid', |
||
169 | 'playhead', |
||
170 | 'payload', |
||
171 | 'metadata', |
||
172 | 'recorded_on' |
||
173 | ] |
||
174 | ) |
||
175 | ->from($this->tableName) |
||
176 | ->where($this->primaryKey . ' > :previousId') |
||
177 | ->orderBy($this->primaryKey, 'ASC') |
||
178 | ->setMaxResults(1); |
||
179 | |||
180 | if (!empty($this->aggregateType)) { |
||
181 | $this->queryBuilder->andWhere('aggregate_type = :aggregate_type'); |
||
182 | $this->queryBuilder->setParameter('aggregate_type', $this->aggregateType); |
||
183 | } |
||
184 | } |
||
185 | |||
186 | return $this->queryBuilder; |
||
187 | } |
||
188 | |||
189 | /** |
||
190 | * @param $row |
||
191 | * @return DomainMessage |
||
192 | */ |
||
193 | View Code Duplication | private function deserializeEvent($row) |
|
203 | } |
||
204 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: