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 |
||
| 11 | class EloquentStoredEventRepository implements StoredEventRepository |
||
| 12 | { |
||
| 13 | public static function retrieveAll(string $uuid = null, int $startingFrom = null): Collection |
||
| 29 | |||
| 30 | public static function persist(ShouldBeStored $event, string $uuid = null, string $model = null): StoredEvent |
||
| 31 | { |
||
| 32 | /** @var EloquentStoredEvent $storedEvent */ |
||
| 33 | $storedEvent = self::getStoredEventModel($model)::make(); |
||
| 34 | |||
| 35 | $storedEvent->setRawAttributes([ |
||
| 36 | 'event_properties' => app(EventSerializer::class)->serialize(clone $event), |
||
| 37 | 'aggregate_uuid' => $uuid, |
||
| 38 | 'event_class' => self::getEventClass(get_class($event)), |
||
| 39 | 'meta_data' => json_encode([]), |
||
| 40 | 'created_at' => Carbon::now(), |
||
| 41 | ]); |
||
| 42 | |||
| 43 | $storedEvent->save(); |
||
| 44 | |||
| 45 | return $storedEvent->toStoredEventData(); |
||
| 46 | } |
||
| 47 | |||
| 48 | public static function persistMany(array $events, string $uuid = null, string $model = null): Collection |
||
| 49 | { |
||
| 50 | $storedEvents = []; |
||
| 51 | |||
| 52 | foreach ($events as $event) { |
||
| 53 | $storedEvents[] = self::persist($event, $uuid, $model); |
||
| 54 | } |
||
| 55 | |||
| 56 | return collect($storedEvents); |
||
| 57 | } |
||
| 58 | |||
| 59 | public static function update(StoredEvent $storedEventData): StoredEvent |
||
| 67 | |||
| 68 | View Code Duplication | private static function getEventClass(string $class): string |
|
| 78 | |||
| 79 | private static function getStoredEventModel(string $model = null): string |
||
| 80 | { |
||
| 83 | } |
||
| 84 |
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.