Total Complexity | 41 |
Total Lines | 163 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like Installer 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.
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 Installer, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
34 | final class Installer implements PluginInterface, EventSubscriberInterface |
||
35 | { |
||
36 | /** @return array<string, array<string|int>> */ |
||
37 | public static function getSubscribedEvents(): array |
||
38 | { |
||
39 | return [ScriptEvents::PRE_AUTOLOAD_DUMP => ['findEventListeners', PHP_INT_MAX]]; |
||
40 | } |
||
41 | |||
42 | public function activate(Composer $composer, IOInterface $io): void |
||
43 | { |
||
44 | // does nothing, see getSubscribedEvents() instead. |
||
45 | } |
||
46 | |||
47 | public function deactivate(Composer $composer, IOInterface $io): void |
||
48 | { |
||
49 | // does nothing, see getSubscribedEvents() instead. |
||
50 | } |
||
51 | |||
52 | public function uninstall(Composer $composer, IOInterface $io): void |
||
53 | { |
||
54 | // does nothing, see getSubscribedEvents() instead. |
||
55 | } |
||
56 | |||
57 | /** |
||
58 | * Called before every dump autoload, generates a fresh PHP class. |
||
59 | * |
||
60 | * @phpstan-ignore shipmonk.deadMethod |
||
61 | */ |
||
62 | public static function findEventListeners(Event $event): void |
||
63 | { |
||
64 | $rootPackagePath = dirname(self::getVendorDir($event->getComposer())) . DIRECTORY_SEPARATOR; |
||
65 | if (! file_exists($rootPackagePath . '/composer.json')) { |
||
66 | return; |
||
67 | } |
||
68 | |||
69 | $jsonRaw = file_get_contents($rootPackagePath . '/composer.json'); |
||
70 | if (! is_string($jsonRaw)) { |
||
|
|||
71 | return; |
||
72 | } |
||
73 | |||
74 | $json = json_decode($jsonRaw, true); |
||
75 | if (! is_array($json)) { |
||
76 | return; |
||
77 | } |
||
78 | |||
79 | if (array_key_exists('name', $json) && $json['name'] === 'wyrihaximus/test-utilities') { |
||
80 | self::addMakeOnInstallOrUpdate($event->getIO(), $rootPackagePath); |
||
81 | |||
82 | return; |
||
83 | } |
||
84 | |||
85 | if (! array_key_exists('require-dev', $json)) { |
||
86 | return; |
||
87 | } |
||
88 | |||
89 | if (! is_array($json['require-dev'])) { |
||
90 | return; |
||
91 | } |
||
92 | |||
93 | foreach (array_keys($json['require-dev']) as $package) { |
||
94 | if ($package === 'wyrihaximus/test-utilities') { |
||
95 | self::addMakeOnInstallOrUpdate($event->getIO(), $rootPackagePath); |
||
96 | |||
97 | return; |
||
98 | } |
||
99 | } |
||
100 | } |
||
101 | |||
102 | private static function addMakeOnInstallOrUpdate(IOInterface $io, string $rootPackagePath): void |
||
159 | } |
||
160 | |||
161 | /** |
||
162 | * @param array<int, string> $scriptsSection |
||
163 | * |
||
164 | * @return array<int, string> |
||
165 | */ |
||
166 | private static function addMakeOnInstallOrUpdateToScriptsSectionAndRemoveCommandsItReplaces(array $scriptsSection): array |
||
167 | { |
||
168 | foreach ($scriptsSection as $script) { |
||
169 | if ($script === 'make on-install-or-update || true') { |
||
170 | return $scriptsSection; |
||
171 | } |
||
172 | } |
||
173 | |||
174 | $scripts = []; |
||
175 | foreach ($scriptsSection as $script) { |
||
176 | if ($script === 'composer normalize' || $script === 'composer update --lock --no-scripts') { |
||
177 | continue; |
||
178 | } |
||
179 | |||
180 | $scripts[] = $script; |
||
181 | } |
||
182 | |||
183 | $scripts[] = 'make on-install-or-update || true'; |
||
184 | |||
185 | return $scripts; |
||
186 | } |
||
187 | |||
188 | /** @return non-empty-string */ |
||
189 | private static function getVendorDir(Composer $composer): string |
||
197 | } |
||
198 | } |
||
199 |