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 |
||
| 12 | class DummyIncrementingHappened implements Event |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * @var AggregateRootId |
||
| 16 | */ |
||
| 17 | private $aggregateRootId; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @var PointInTime |
||
| 21 | */ |
||
| 22 | private $timeOfRecording; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var int |
||
| 26 | */ |
||
| 27 | private $number; |
||
| 28 | |||
| 29 | public function __construct(AggregateRootId $aggregateRootId, PointInTime $timeOfRecording, int $number) |
||
| 30 | { |
||
| 31 | $this->aggregateRootId = $aggregateRootId; |
||
| 32 | $this->timeOfRecording = $timeOfRecording; |
||
| 33 | $this->number = $number; |
||
| 34 | } |
||
| 35 | |||
| 36 | public function aggregateRootId(): AggregateRootId |
||
| 37 | { |
||
| 38 | return $this->aggregateRootId; |
||
| 39 | } |
||
| 40 | |||
| 41 | public function eventVersion(): int |
||
| 42 | { |
||
| 43 | return 1; |
||
| 44 | } |
||
| 45 | |||
| 46 | public function timeOfRecording(): PointInTime |
||
| 47 | { |
||
| 48 | return $this->timeOfRecording; |
||
| 49 | } |
||
| 50 | |||
| 51 | public function toPayload(): array |
||
| 52 | { |
||
| 53 | return []; |
||
| 54 | } |
||
| 55 | |||
| 56 | public static function fromPayload(array $payload, AggregateRootId $aggregateRootId, PointInTime $timeOfRecording): Event |
||
| 57 | { |
||
| 58 | return new DummyIncrementingHappened($aggregateRootId, $timeOfRecording, $payload['number']); |
||
| 59 | } |
||
| 60 | |||
| 61 | public function number(): int |
||
| 62 | { |
||
| 63 | return $this->number; |
||
| 64 | } |
||
| 65 | } |