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() |
||
| 189 | |||
| 190 | /** |
||
| 191 | * @param $row |
||
| 192 | * @return DomainMessage |
||
| 193 | */ |
||
| 194 | View Code Duplication | private function deserializeEvent($row) |
|
| 204 | } |
||
| 205 |
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: