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 | ||
| 23 | class AggregateAwareDBALEventStore implements EventStoreInterface | ||
| 24 | { | ||
| 25 | /** | ||
| 26 | * @var Connection | ||
| 27 | */ | ||
| 28 | private $connection; | ||
| 29 | |||
| 30 | /** | ||
| 31 | * @var SerializerInterface | ||
| 32 | */ | ||
| 33 | private $payloadSerializer; | ||
| 34 | |||
| 35 | /** | ||
| 36 | * @var SerializerInterface | ||
| 37 | */ | ||
| 38 | private $metadataSerializer; | ||
| 39 | |||
| 40 | /** | ||
| 41 | * @var null | ||
| 42 | */ | ||
| 43 | private $loadStatement = null; | ||
| 44 | |||
| 45 | /** | ||
| 46 | * @var string | ||
| 47 | */ | ||
| 48 | private $tableName; | ||
| 49 | |||
| 50 | /** | ||
| 51 | * @var string | ||
| 52 | */ | ||
| 53 | private $aggregateType; | ||
| 54 | |||
| 55 | /** | ||
| 56 | * @param Connection $connection | ||
| 57 | * @param SerializerInterface $payloadSerializer | ||
| 58 | * @param SerializerInterface $metadataSerializer | ||
| 59 | * @param string $tableName | ||
| 60 | * @param mixed $aggregateType | ||
| 61 | */ | ||
| 62 | public function __construct( | ||
| 75 | |||
| 76 | /** | ||
| 77 |      * {@inheritDoc} | ||
| 78 | */ | ||
| 79 | public function load($id) | ||
| 96 | |||
| 97 | /** | ||
| 98 |      * {@inheritDoc} | ||
| 99 | */ | ||
| 100 | public function append($id, DomainEventStreamInterface $eventStream) | ||
| 118 | |||
| 119 | /** | ||
| 120 | * @param Connection $connection | ||
| 121 | * @param DomainMessage $domainMessage | ||
| 122 | */ | ||
| 123 | private function insertMessage(Connection $connection, DomainMessage $domainMessage) | ||
| 137 | |||
| 138 | /** | ||
| 139 | * @param Schema $schema | ||
| 140 | * @return Table|null | ||
| 141 | */ | ||
| 142 | public function configureSchema(Schema $schema) | ||
| 150 | |||
| 151 | /** | ||
| 152 | * @return mixed | ||
| 153 | */ | ||
| 154 | public function configureTable() | ||
| 178 | |||
| 179 | /** | ||
| 180 | * @return \Doctrine\DBAL\Driver\Statement|null | ||
| 181 | */ | ||
| 182 | private function prepareLoadStatement() | ||
| 201 | |||
| 202 | /** | ||
| 203 | * @param $row | ||
| 204 | * @return DomainMessage | ||
| 205 | */ | ||
| 206 | View Code Duplication | private function deserializeEvent($row) | |
| 216 | |||
| 217 | /** | ||
| 218 | * Ensure that an error will be thrown if the ID in the domain messages is | ||
| 219 | * not something that can be converted to a string. | ||
| 220 | * | ||
| 221 | * If we let this move on without doing this DBAL will eventually | ||
| 222 | * give us a hard time but the true reason for the problem will be | ||
| 223 | * obfuscated. | ||
| 224 | * | ||
| 225 | * @param DomainEventStreamInterface $eventStream | ||
| 226 | */ | ||
| 227 | private function guardStream(DomainEventStreamInterface $eventStream) | ||
| 234 | } | ||
| 235 | 
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: