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 EventProjectorServiceProvider extends ServiceProvider |
||
24 | { |
||
25 | public function boot() |
||
26 | { |
||
27 | if ($this->app->runningInConsole()) { |
||
28 | $this->publishes([ |
||
29 | __DIR__.'/../config/event-projector.php' => config_path('event-projector.php'), |
||
30 | ], 'config'); |
||
31 | } |
||
32 | |||
33 | View Code Duplication | if (! class_exists('CreateStoredEventsTable')) { |
|
|
|||
34 | $this->publishes([ |
||
35 | __DIR__.'/../stubs/create_stored_events_table.php.stub' => database_path('migrations/'.date('Y_m_d_His', time()).'_create_stored_events_table.php'), |
||
36 | ], 'migrations'); |
||
37 | } |
||
38 | |||
39 | View Code Duplication | if (! class_exists('CreateProjectorStatusesTable')) { |
|
40 | $this->publishes([ |
||
41 | __DIR__.'/../stubs/create_projector_statuses_table.php.stub' => database_path('migrations/'.date('Y_m_d_His', time()).'_create_projector_statuses_table.php'), |
||
42 | ], 'migrations'); |
||
43 | } |
||
44 | |||
45 | Event::subscribe(EventSubscriber::class); |
||
46 | } |
||
47 | |||
48 | public function register() |
||
49 | { |
||
50 | $this->mergeConfigFrom(__DIR__.'/../config/event-projector.php', 'event-projector'); |
||
51 | |||
52 | $this->app->singleton(EventProjectionist::class, function () { |
||
53 | $config = config('event-projector'); |
||
54 | |||
55 | $projectionist = new EventProjectionist($config); |
||
56 | |||
57 | $projectionist |
||
58 | ->addProjectors($config['projectors'] ?? []) |
||
59 | ->addReactors($config['reactors'] ?? []); |
||
60 | |||
61 | return $projectionist; |
||
62 | }); |
||
63 | |||
64 | $this->app->alias(EventProjectionist::class, 'event-projector'); |
||
65 | |||
66 | View Code Duplication | $this->app->bind(SnapshotFactory::class, function () { |
|
67 | $eventProjectionist = app(EventProjectionist::class); |
||
68 | |||
69 | $config = config('event-projector'); |
||
70 | |||
71 | $diskName = $config['snapshots_disk']; |
||
72 | $disk = Storage::disk($diskName); |
||
73 | |||
74 | return new SnapshotFactory($eventProjectionist, $disk, $config); |
||
75 | }); |
||
76 | |||
77 | View Code Duplication | $this->app->bind(SnapshotRepository::class, function () { |
|
78 | $eventProjectionist = app(EventProjectionist::class); |
||
79 | |||
80 | $config = config('event-projector'); |
||
81 | |||
82 | $diskName = $config['snapshots_disk']; |
||
83 | $disk = Storage::disk($diskName); |
||
84 | |||
85 | return new SnapshotRepository($eventProjectionist, $disk, $config); |
||
86 | }); |
||
87 | |||
88 | $this->app->singleton(EventSubscriber::class, function () { |
||
89 | $eventProjectionist = app(EventProjectionist::class); |
||
90 | $config = config('event-projector'); |
||
91 | |||
92 | return new EventSubscriber($eventProjectionist, $config); |
||
93 | }); |
||
94 | |||
95 | $this->app |
||
96 | ->when(ReplayEventsCommand::class) |
||
97 | ->needs('$storedEventModelClass') |
||
98 | ->give(config('event-projector.stored_event_model')); |
||
99 | |||
100 | $this->app->bind(EventSerializer::class, config('event-projector.event_serializer')); |
||
101 | |||
102 | $this->bindCommands(); |
||
103 | } |
||
104 | |||
105 | protected function bindCommands() |
||
137 | } |
||
138 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.