Total Complexity | 46 |
Total Lines | 202 |
Duplicated Lines | 0 % |
Changes | 4 | ||
Bugs | 0 | Features | 1 |
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 |
||
36 | final class Installer implements PluginInterface, EventSubscriberInterface |
||
37 | { |
||
38 | /** @return array<string, array<string|int>> */ |
||
39 | public static function getSubscribedEvents(): array |
||
40 | { |
||
41 | return [ScriptEvents::PRE_AUTOLOAD_DUMP => ['findEventListeners', PHP_INT_MAX]]; |
||
42 | } |
||
43 | |||
44 | public function activate(Composer $composer, IOInterface $io): void |
||
45 | { |
||
46 | // does nothing, see getSubscribedEvents() instead. |
||
47 | } |
||
48 | |||
49 | public function deactivate(Composer $composer, IOInterface $io): void |
||
50 | { |
||
51 | // does nothing, see getSubscribedEvents() instead. |
||
52 | } |
||
53 | |||
54 | public function uninstall(Composer $composer, IOInterface $io): void |
||
55 | { |
||
56 | // does nothing, see getSubscribedEvents() instead. |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * Called before every dump autoload, generates a fresh PHP class. |
||
61 | * |
||
62 | * @phpstan-ignore shipmonk.deadMethod |
||
63 | */ |
||
64 | public static function findEventListeners(Event $event): void |
||
65 | { |
||
66 | $rootPackagePath = dirname(self::getVendorDir($event->getComposer())) . DIRECTORY_SEPARATOR; |
||
67 | if (! file_exists($rootPackagePath . '/composer.json')) { |
||
68 | return; |
||
69 | } |
||
70 | |||
71 | $jsonRaw = file_get_contents($rootPackagePath . '/composer.json'); |
||
72 | if (! is_string($jsonRaw)) { |
||
|
|||
73 | return; |
||
74 | } |
||
75 | |||
76 | $json = json_decode($jsonRaw, true); |
||
77 | if (! is_array($json)) { |
||
78 | return; |
||
79 | } |
||
80 | |||
81 | if (! array_key_exists('require-dev', $json)) { |
||
82 | return; |
||
83 | } |
||
84 | |||
85 | if (! is_array($json['require-dev'])) { |
||
86 | return; |
||
87 | } |
||
88 | |||
89 | $hasMakefiles = false; |
||
90 | foreach (array_keys($json['require-dev']) as $package) { |
||
91 | if ($package === 'wyrihaximus/makefiles') { |
||
92 | $hasMakefiles = true; |
||
93 | break; |
||
94 | } |
||
95 | } |
||
96 | |||
97 | if (! $hasMakefiles) { |
||
98 | return; |
||
99 | } |
||
100 | |||
101 | unset($hasMakefiles); |
||
102 | |||
103 | if (array_key_exists('name', $json) && $json['name'] === 'wyrihaximus/test-utilities') { |
||
104 | self::addMakeOnInstallOrUpdate($event->getIO(), $rootPackagePath); |
||
105 | |||
106 | return; |
||
107 | } |
||
108 | |||
109 | foreach (['require', 'require-dev'] as $key) { |
||
110 | if (! is_array($json[$key])) { |
||
111 | continue; |
||
112 | } |
||
113 | |||
114 | foreach (array_keys($json[$key]) as $package) { |
||
115 | if ( |
||
116 | in_array( |
||
117 | strtolower((string) $package), |
||
118 | [ |
||
119 | 'wyrihaximus/test-utilities', |
||
120 | 'wyrihaximus/async-test-utilities', |
||
121 | 'wyrihaximus/compress-test-utilities', |
||
122 | 'wyrihaximus/react-mutex-test-utilities', |
||
123 | ], |
||
124 | true, |
||
125 | ) |
||
126 | ) { |
||
127 | self::addMakeOnInstallOrUpdate($event->getIO(), $rootPackagePath); |
||
128 | |||
129 | break; |
||
130 | } |
||
131 | } |
||
132 | } |
||
133 | } |
||
134 | |||
135 | private static function addMakeOnInstallOrUpdate(IOInterface $io, string $rootPackagePath): void |
||
200 | } |
||
201 | |||
202 | /** |
||
203 | * @param array<int, string> $scriptsSection |
||
204 | * |
||
205 | * @return array<int, string> |
||
206 | */ |
||
207 | private static function addMakeOnInstallOrUpdateToScriptsSectionAndRemoveCommandsItReplaces(array $scriptsSection): array |
||
208 | { |
||
209 | foreach ($scriptsSection as $script) { |
||
210 | if ($script === 'make on-install-or-update || true') { |
||
211 | return $scriptsSection; |
||
212 | } |
||
213 | } |
||
214 | |||
215 | $scripts = []; |
||
216 | foreach ($scriptsSection as $script) { |
||
217 | if ($script === 'composer normalize' || $script === 'composer update --lock --no-scripts') { |
||
218 | continue; |
||
219 | } |
||
220 | |||
221 | $scripts[] = $script; |
||
222 | } |
||
223 | |||
224 | $scripts[] = 'make on-install-or-update || true'; |
||
225 | |||
226 | return $scripts; |
||
227 | } |
||
228 | |||
229 | /** @return non-empty-string */ |
||
230 | private static function getVendorDir(Composer $composer): string |
||
238 | } |
||
239 | } |
||
240 |