Complex classes like ArtisanServiceProvider often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ArtisanServiceProvider, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class ArtisanServiceProvider extends ServiceProvider |
||
15 | { |
||
16 | /** |
||
17 | * Indicates if loading of the provider is deferred. |
||
18 | * |
||
19 | * @var bool |
||
20 | */ |
||
21 | protected $defer = true; |
||
22 | |||
23 | /** |
||
24 | * The commands to be registered. |
||
25 | * |
||
26 | * @var array |
||
27 | */ |
||
28 | protected $commands = [ |
||
29 | 'CacheClear' => 'command.cache.clear', |
||
30 | 'CacheForget' => 'command.cache.forget', |
||
31 | 'ClearCompiled' => 'command.clear-compiled', |
||
32 | 'ClearResets' => 'command.auth.resets.clear', |
||
33 | 'ConfigCache' => 'command.config.cache', |
||
34 | 'ConfigClear' => 'command.config.clear', |
||
35 | 'Down' => 'command.down', |
||
36 | 'Environment' => 'command.environment', |
||
37 | 'KeyGenerate' => 'command.key.generate', |
||
38 | // 'Migrate' => 'command.migrate', |
||
39 | // 'MigrateFresh' => 'command.migrate.fresh', |
||
40 | // 'MigrateInstall' => 'command.migrate.install', |
||
41 | // 'MigrateRefresh' => 'command.migrate.refresh', |
||
42 | // 'MigrateReset' => 'command.migrate.reset', |
||
43 | // 'MigrateRollback' => 'command.migrate.rollback', |
||
44 | // 'MigrateStatus' => 'command.migrate.status', |
||
45 | 'Optimize' => 'command.optimize', |
||
46 | 'PackageDiscover' => 'command.package.discover', |
||
47 | 'QueueFailed' => 'command.queue.failed', |
||
48 | 'QueueFlush' => 'command.queue.flush', |
||
49 | 'QueueForget' => 'command.queue.forget', |
||
50 | 'QueueListen' => 'command.queue.listen', |
||
51 | 'QueueRestart' => 'command.queue.restart', |
||
52 | 'QueueRetry' => 'command.queue.retry', |
||
53 | 'QueueWork' => 'command.queue.work', |
||
54 | 'RouteCache' => 'command.route.cache', |
||
55 | 'RouteClear' => 'command.route.clear', |
||
56 | 'RouteList' => 'command.route.list', |
||
57 | // 'Seed' => 'command.seed', |
||
58 | 'ScheduleFinish' => ScheduleFinishCommand::class, |
||
59 | 'ScheduleRun' => ScheduleRunCommand::class, |
||
60 | 'StorageLink' => 'command.storage.link', |
||
61 | 'Up' => 'command.up', |
||
62 | 'ViewClear' => 'command.view.clear', |
||
63 | |||
64 | 'Route' => 'command+.route', |
||
65 | 'Tail' => 'command+.tail', |
||
66 | 'AddonList' => 'command+.addon.list', |
||
67 | 'AddonStatus' => 'command+.addon.status', |
||
68 | 'DatabaseStatus' => 'command+.database.status', |
||
69 | 'DatabaseUpgrade' => 'command+.database.upgrade', |
||
70 | 'DatabaseClean' => 'command+.database.clean', |
||
71 | 'DatabaseRefresh' => 'command+.database.refresh', |
||
72 | 'DatabaseRollback' => 'command+.database.rollback', |
||
73 | 'DatabaseAgain' => 'command+.database.again', |
||
74 | 'DatabaseSeed' => 'command+.database.seed', |
||
75 | 'HashMake' => 'command+.hash.make', |
||
76 | 'HashCheck' => 'command+.hash.check', |
||
77 | ]; |
||
78 | |||
79 | /** |
||
80 | * The commands to be registered. |
||
81 | * |
||
82 | * @var array |
||
83 | */ |
||
84 | protected $devCommands = [ |
||
85 | 'AppName' => 'command.app.name', |
||
86 | // 'AuthMake' => 'command.auth.make', |
||
87 | // 'CacheTable' => 'command.cache.table', |
||
88 | // 'ConsoleMake' => 'command.console.make', |
||
89 | // 'ControllerMake' => 'command.controller.make', |
||
90 | 'EventGenerate' => 'command.event.generate', |
||
91 | // 'EventMake' => 'command.event.make', |
||
92 | // 'FactoryMake' => 'command.factory.make', |
||
93 | // 'JobMake' => 'command.job.make', |
||
94 | // 'ListenerMake' => 'command.listener.make', |
||
95 | // 'MailMake' => 'command.mail.make', |
||
96 | // 'MiddlewareMake' => 'command.middleware.make', |
||
97 | // 'MigrateMake' => 'command.migrate.make', |
||
98 | // 'ModelMake' => 'command.model.make', |
||
99 | // 'NotificationMake' => 'command.notification.make', |
||
100 | // 'NotificationTable' => 'command.notification.table', |
||
101 | // 'PolicyMake' => 'command.policy.make', |
||
102 | // 'ProviderMake' => 'command.provider.make', |
||
103 | // 'QueueFailedTable' => 'command.queue.failed-table', |
||
104 | // 'QueueTable' => 'command.queue.table', |
||
105 | // 'RequestMake' => 'command.request.make', |
||
106 | // 'ResourceMake' => 'command.resource.make', |
||
107 | // 'RuletMake' => 'command.rule.make', |
||
108 | // 'SeederMake' => 'command.seeder.make', |
||
109 | // 'SessionTable' => 'command.session.table', |
||
110 | // 'TestMake' => 'command.test.make', |
||
111 | 'Preset' => 'command.preset', |
||
112 | 'Serve' => 'command.serve', |
||
113 | 'VendorPublish' => 'command.vendor.publish', |
||
114 | |||
115 | 'AppContainer' => 'command+.app.container', |
||
116 | 'AddonName' => 'command+.addon.name', |
||
117 | 'AddonRemove' => 'command+.addon.remove', |
||
118 | |||
119 | 'MakeAddon' => 'command+.addon.make', |
||
120 | 'MakeCommand' => 'command+.command.make', |
||
121 | 'MakeController' => 'command+.controller.make', |
||
122 | 'MakeEvent' => 'command+.event.make', |
||
123 | 'MakeJob' => 'command+.job.make', |
||
124 | 'MakeListener' => 'command+.listener.make', |
||
125 | 'MakeMail' => 'command+.mail.make', |
||
126 | 'MakeMiddleware' => 'command+.middleware.make', |
||
127 | 'MakeMigration' => 'command+.migration.make', |
||
128 | 'MakeModel' => 'command+.model.make', |
||
129 | 'MakeNotification' => 'command+.notification.make', |
||
130 | 'MakePolicy' => 'command+.policy.make', |
||
131 | 'MakeProvider' => 'command+.provider.make', |
||
132 | 'MakeRequest' => 'command+.request.make', |
||
133 | 'MakeResource' => 'command+.resource.make', |
||
134 | 'MakeRule' => 'command+.rule.make', |
||
135 | 'MakeSeeder' => 'command+.seeder.make', |
||
136 | 'MakeTest' => 'command+.test.make', |
||
137 | ]; |
||
138 | |||
139 | /** |
||
140 | * Register the service provider. |
||
141 | * |
||
142 | * @return void |
||
143 | */ |
||
144 | 12 | public function register() |
|
148 | |||
149 | /** |
||
150 | * Get the services provided by the provider. |
||
151 | * |
||
152 | * @return array |
||
153 | */ |
||
154 | public function provides() |
||
158 | |||
159 | /** |
||
160 | * @return array |
||
161 | */ |
||
162 | 12 | protected function availableCommands() |
|
172 | |||
173 | /** |
||
174 | * Register the given commands. |
||
175 | * |
||
176 | * @param array $commands |
||
177 | * @return void |
||
178 | */ |
||
179 | 12 | protected function registerCommands(array $commands) |
|
187 | |||
188 | /** |
||
189 | * Register the command. |
||
190 | * |
||
191 | * @param string $command |
||
192 | * @return void |
||
193 | */ |
||
194 | protected function registerRouteCommand($command) |
||
200 | |||
201 | /** |
||
202 | * Register the command. |
||
203 | * |
||
204 | * @param string $command |
||
205 | * @return void |
||
206 | */ |
||
207 | protected function registerTailCommand($command) |
||
213 | |||
214 | /** |
||
215 | * Register the command. |
||
216 | * |
||
217 | * @param string $command |
||
218 | * @return void |
||
219 | */ |
||
220 | protected function registerAppContainerCommand($command) |
||
226 | |||
227 | /** |
||
228 | * Register the command. |
||
229 | * |
||
230 | * @param string $command |
||
231 | * @return void |
||
232 | */ |
||
233 | protected function registerAddonListCommand($command) |
||
239 | |||
240 | /** |
||
241 | * Register the command. |
||
242 | * |
||
243 | * @param string $command |
||
244 | * @return void |
||
245 | */ |
||
246 | protected function registerAddonStatusCommand($command) |
||
252 | |||
253 | /** |
||
254 | * Register the command. |
||
255 | * |
||
256 | * @param string $command |
||
257 | * @return void |
||
258 | */ |
||
259 | protected function registerAddonNameCommand($command) |
||
265 | |||
266 | /** |
||
267 | * Register the command. |
||
268 | * |
||
269 | * @param string $command |
||
270 | * @return void |
||
271 | */ |
||
272 | protected function registerAddonRemoveCommand($command) |
||
278 | |||
279 | /** |
||
280 | * Register the command. |
||
281 | * |
||
282 | * @param string $command |
||
283 | * @return void |
||
284 | */ |
||
285 | protected function registerDatabaseStatusCommand($command) |
||
291 | |||
292 | /** |
||
293 | * Register the command. |
||
294 | * |
||
295 | * @param string $command |
||
296 | * @return void |
||
297 | */ |
||
298 | protected function registerDatabaseUpgradeCommand($command) |
||
304 | |||
305 | /** |
||
306 | * Register the command. |
||
307 | * |
||
308 | * @param string $command |
||
309 | * @return void |
||
310 | */ |
||
311 | protected function registerDatabaseCleanCommand($command) |
||
317 | |||
318 | /** |
||
319 | * Register the command. |
||
320 | * |
||
321 | * @param string $command |
||
322 | * @return void |
||
323 | */ |
||
324 | protected function registerDatabaseRefreshCommand($command) |
||
330 | |||
331 | /** |
||
332 | * Register the command. |
||
333 | * |
||
334 | * @param string $command |
||
335 | * @return void |
||
336 | */ |
||
337 | protected function registerDatabaseRollbackCommand($command) |
||
343 | |||
344 | /** |
||
345 | * Register the command. |
||
346 | * |
||
347 | * @param string $command |
||
348 | * @return void |
||
349 | */ |
||
350 | protected function registerDatabaseAgainCommand($command) |
||
356 | |||
357 | /** |
||
358 | * Register the command. |
||
359 | * |
||
360 | * @param string $command |
||
361 | * @return void |
||
362 | */ |
||
363 | protected function registerDatabaseSeedCommand($command) |
||
369 | |||
370 | /** |
||
371 | * Register the command. |
||
372 | * |
||
373 | * @param string $command |
||
374 | * @return void |
||
375 | */ |
||
376 | protected function registerHashMakeCommand($command) |
||
382 | |||
383 | /** |
||
384 | * Register the command. |
||
385 | * |
||
386 | * @param string $command |
||
387 | * @return void |
||
388 | */ |
||
389 | protected function registerHashCheckCommand($command) |
||
395 | |||
396 | /** |
||
397 | * Register the command. |
||
398 | * |
||
399 | * @param string $command |
||
400 | * @return void |
||
401 | */ |
||
402 | protected function registerMakeAddonCommand($command) |
||
408 | |||
409 | /** |
||
410 | * Register the command. |
||
411 | * |
||
412 | * @param string $command |
||
413 | * @return void |
||
414 | */ |
||
415 | protected function registerMakeCommandCommand($command) |
||
421 | |||
422 | /** |
||
423 | * Register the command. |
||
424 | * |
||
425 | * @param string $command |
||
426 | * @return void |
||
427 | */ |
||
428 | protected function registerMakeControllerCommand($command) |
||
434 | |||
435 | /** |
||
436 | * Register the command. |
||
437 | * |
||
438 | * @param string $command |
||
439 | * @return void |
||
440 | */ |
||
441 | protected function registerMakeEventCommand($command) |
||
447 | |||
448 | /** |
||
449 | * Register the command. |
||
450 | * |
||
451 | * @param string $command |
||
452 | * @return void |
||
453 | */ |
||
454 | protected function registerMakeJobCommand($command) |
||
460 | |||
461 | /** |
||
462 | * Register the command. |
||
463 | * |
||
464 | * @param string $command |
||
465 | * @return void |
||
466 | */ |
||
467 | protected function registerMakeListenerCommand($command) |
||
473 | |||
474 | /** |
||
475 | * Register the command. |
||
476 | * |
||
477 | * @param string $command |
||
478 | * @return void |
||
479 | */ |
||
480 | protected function registerMakeMailCommand($command) |
||
486 | |||
487 | |||
488 | /** |
||
489 | * Register the command. |
||
490 | * |
||
491 | * @param string $command |
||
492 | * @return void |
||
493 | */ |
||
494 | protected function registerMakeMiddlewareCommand($command) |
||
500 | |||
501 | /** |
||
502 | * Register the command. |
||
503 | * |
||
504 | * @param string $command |
||
505 | * @return void |
||
506 | */ |
||
507 | protected function registerMakeMigrationCommand($command) |
||
513 | |||
514 | /** |
||
515 | * Register the command. |
||
516 | * |
||
517 | * @param string $command |
||
518 | * @return void |
||
519 | */ |
||
520 | protected function registerMakeModelCommand($command) |
||
526 | |||
527 | /** |
||
528 | * Register the command. |
||
529 | * |
||
530 | * @param string $command |
||
531 | * @return void |
||
532 | */ |
||
533 | protected function registerMakeNotificationCommand($command) |
||
539 | |||
540 | /** |
||
541 | * Register the command. |
||
542 | * |
||
543 | * @param string $command |
||
544 | * @return void |
||
545 | */ |
||
546 | protected function registerMakePolicyCommand($command) |
||
552 | |||
553 | /** |
||
554 | * Register the command. |
||
555 | * |
||
556 | * @param string $command |
||
557 | * @return void |
||
558 | */ |
||
559 | protected function registerMakeProviderCommand($command) |
||
565 | |||
566 | /** |
||
567 | * Register the command. |
||
568 | * |
||
569 | * @param string $command |
||
570 | * @return void |
||
571 | */ |
||
572 | protected function registerMakeRequestCommand($command) |
||
578 | |||
579 | /** |
||
580 | * Register the command. |
||
581 | * |
||
582 | * @param string $command |
||
583 | * @return void |
||
584 | */ |
||
585 | protected function registerMakeResourceCommand($command) |
||
591 | |||
592 | /** |
||
593 | * Register the command. |
||
594 | * |
||
595 | * @param string $command |
||
596 | * @return void |
||
597 | */ |
||
598 | protected function registerMakeRuleCommand($command) |
||
604 | |||
605 | /** |
||
606 | * Register the command. |
||
607 | * |
||
608 | * @param string $command |
||
609 | * @return void |
||
610 | */ |
||
611 | protected function registerMakeSeederCommand($command) |
||
617 | |||
618 | /** |
||
619 | * Register the command. |
||
620 | * |
||
621 | * @param string $command |
||
622 | * @return void |
||
623 | */ |
||
624 | protected function registerMakeTestCommand($command) |
||
630 | } |
||
631 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.