Conditions | 11 |
Paths | 100 |
Total Lines | 41 |
Code Lines | 30 |
Lines | 6 |
Ratio | 14.63 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
34 | final public static function runEvents(Event $event): void |
||
35 | { |
||
36 | $rootDir = self::getRootDir($event->getComposer()); |
||
|
|||
37 | |||
38 | $app = file_exists($rootDir.'/bootstrap.php') |
||
39 | ? require_once $rootDir.'/bootstrap.php' : |
||
40 | new App(['settings' => require_once $rootDir.'/app/config/settings.php']); |
||
41 | |||
42 | /** @var EventDispatcher $dispatcher */ |
||
43 | $dispatcher = $app->getContainer()['event_dispatcher']; |
||
44 | |||
45 | foreach (require_once $rootDir.'/var/cache/internal/events_subscribers.cache.php' as $subscriberClass) { |
||
46 | $dispatcher->addSubscriber(new $subscriberClass()); |
||
47 | } |
||
48 | |||
49 | if (preg_match('/-cmd$/', $event->getName())) { |
||
50 | $detailedEventClass = 'Taisiya\\CoreBundle\\Event\\Composer\\CommandEvent\\'.Inflector::classify($event->getName()).'Event'; |
||
51 | View Code Duplication | } elseif (preg_match('/-dependencies-solving$/', $event->getName())) { |
|
52 | $detailedEventClass = 'Taisiya\\CoreBundle\\Event\\Composer\\InstallerEvent\\'.Inflector::classify($event->getName()).'Event'; |
||
53 | } elseif (preg_match('/-package-/', $event->getName())) { |
||
54 | $detailedEventClass = 'Taisiya\\CoreBundle\\Event\\Composer\\PackageEvent\\'.Inflector::classify($event->getName()).'Event'; |
||
55 | View Code Duplication | } elseif (preg_match('/^(init|command|pre-file-download)$/', $event->getName())) { |
|
56 | $detailedEventClass = 'Taisiya\\CoreBundle\\Event\\Composer\\PluginEvent\\'.Inflector::classify($event->getName()).'Event'; |
||
57 | } |
||
58 | $detailedEvent = new $detailedEventClass($app); |
||
59 | $dispatcher->dispatch($detailedEvent::NAME, $detailedEvent); |
||
60 | |||
61 | if (preg_match('/-cmd$/', $event->getName())) { |
||
62 | $commandEvent = new CommandEvent($app); |
||
63 | $dispatcher->dispatch($commandEvent::NAME, $commandEvent); |
||
64 | } elseif (preg_match('/-dependencies-solving$/', $event->getName())) { |
||
65 | $installerEvent = new InstallerEvent($app); |
||
66 | $dispatcher->dispatch($installerEvent::NAME, $installerEvent); |
||
67 | } elseif (preg_match('/-package-/', $event->getName())) { |
||
68 | $packageEvent = new PackageEvent($app); |
||
69 | $dispatcher->dispatch($packageEvent::NAME, $packageEvent); |
||
70 | } elseif (preg_match('/^(init|command|pre-file-download)$/', $event->getName())) { |
||
71 | $pluginEvent = new PluginEvent($app); |
||
72 | $dispatcher->dispatch($pluginEvent::NAME, $pluginEvent); |
||
73 | } |
||
74 | } |
||
75 | } |
||
76 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: