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 |
||
18 | class AggregateAwareDBALEventStore implements EventStoreInterface |
||
19 | { |
||
20 | /** |
||
21 | * @var Connection |
||
22 | */ |
||
23 | private $connection; |
||
24 | |||
25 | /** |
||
26 | * @var SerializerInterface |
||
27 | */ |
||
28 | private $payloadSerializer; |
||
29 | |||
30 | /** |
||
31 | * @var SerializerInterface |
||
32 | */ |
||
33 | private $metadataSerializer; |
||
34 | |||
35 | /** |
||
36 | * @var null |
||
37 | */ |
||
38 | private $loadStatement = null; |
||
39 | |||
40 | /** |
||
41 | * @var string |
||
42 | */ |
||
43 | private $tableName; |
||
44 | |||
45 | /** |
||
46 | * @var string |
||
47 | */ |
||
48 | private $aggregateType; |
||
49 | |||
50 | /** |
||
51 | * @param Connection $connection |
||
52 | * @param SerializerInterface $payloadSerializer |
||
53 | * @param SerializerInterface $metadataSerializer |
||
54 | * @param string $tableName |
||
55 | * @param string $aggregateType |
||
56 | */ |
||
57 | public function __construct( |
||
70 | |||
71 | /** |
||
72 | * {@inheritDoc} |
||
73 | */ |
||
74 | public function load($id) |
||
91 | |||
92 | /** |
||
93 | * {@inheritDoc} |
||
94 | */ |
||
95 | public function append($id, DomainEventStreamInterface $eventStream) |
||
112 | |||
113 | /** |
||
114 | * @param Connection $connection |
||
115 | * @param DomainMessage $domainMessage |
||
116 | */ |
||
117 | private function insertMessage(Connection $connection, DomainMessage $domainMessage) |
||
131 | |||
132 | /** |
||
133 | * @param Schema $schema |
||
134 | * @return Table|null |
||
135 | */ |
||
136 | public function configureSchema(Schema $schema) |
||
144 | |||
145 | /** |
||
146 | * @return mixed |
||
147 | */ |
||
148 | public function configureTable() |
||
168 | |||
169 | /** |
||
170 | * @return \Doctrine\DBAL\Driver\Statement|null |
||
171 | */ |
||
172 | private function prepareLoadStatement() |
||
191 | |||
192 | /** |
||
193 | * @param $row |
||
194 | * @return DomainMessage |
||
195 | */ |
||
196 | View Code Duplication | private function deserializeEvent($row) |
|
206 | |||
207 | /** |
||
208 | * @param DomainEventStreamInterface $eventStream |
||
209 | */ |
||
210 | private function guardStream(DomainEventStreamInterface $eventStream) |
||
217 | } |
||
218 |
PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.
Let’s take a look at an example:
If we look at the
getEmail()
method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:On the hand, if we look at the
setEmail()
, this method _has_ side-effects. In the following case, we could not remove the method call: