Complex classes like Plugin 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 Plugin, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
32 | class Plugin implements PluginInterface, EventSubscriberInterface |
||
33 | { |
||
34 | const MESSAGE_RUNNING_INSTALLER = 'Running PHPCodeSniffer Composer Installer'; |
||
35 | const MESSAGE_NOTHING_TO_INSTALL = 'Nothing to install or update'; |
||
36 | const MESSAGE_NOT_INSTALLED = 'PHPCodeSniffer is not installed'; |
||
37 | |||
38 | const PACKAGE_NAME = 'squizlabs/php_codesniffer'; |
||
39 | const PACKAGE_TYPE = 'phpcodesniffer-standard'; |
||
40 | |||
41 | const PHPCS_CONFIG_KEY = 'installed_paths'; |
||
42 | |||
43 | /** |
||
44 | * @var Composer |
||
45 | */ |
||
46 | private $composer; |
||
47 | |||
48 | /** |
||
49 | * @var IOInterface |
||
50 | */ |
||
51 | private $io; |
||
52 | |||
53 | /** |
||
54 | * @var array |
||
55 | */ |
||
56 | private $installedPaths; |
||
57 | |||
58 | /** |
||
59 | * @var ProcessBuilder |
||
60 | */ |
||
61 | private $processBuilder; |
||
62 | |||
63 | /** |
||
64 | * Triggers the plugin's main functionality. |
||
65 | * |
||
66 | * Makes it possible to run the plugin as a custom command. |
||
67 | * |
||
68 | * @param Event $event |
||
69 | * |
||
70 | * @throws \InvalidArgumentException |
||
71 | * @throws \RuntimeException |
||
72 | * @throws LogicException |
||
73 | * @throws ProcessFailedException |
||
74 | * @throws RuntimeException |
||
75 | */ |
||
76 | public static function run(Event $event) |
||
88 | |||
89 | /** |
||
90 | * {@inheritDoc} |
||
91 | * |
||
92 | * @throws \RuntimeException |
||
93 | * @throws LogicException |
||
94 | * @throws RuntimeException |
||
95 | * @throws ProcessFailedException |
||
96 | */ |
||
97 | public function activate(Composer $composer, IOInterface $io) |
||
98 | { |
||
99 | $this->composer = $composer; |
||
100 | $this->io = $io; |
||
101 | |||
102 | $this->init(); |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * Prepares the plugin so it's main functionality can be run. |
||
107 | * |
||
108 | * @throws \RuntimeException |
||
109 | * @throws LogicException |
||
110 | * @throws ProcessFailedException |
||
111 | * @throws RuntimeException |
||
112 | */ |
||
113 | private function init() |
||
122 | |||
123 | /** |
||
124 | * {@inheritDoc} |
||
125 | */ |
||
126 | public static function getSubscribedEvents() |
||
127 | { |
||
128 | return [ |
||
129 | ScriptEvents::POST_INSTALL_CMD => [ |
||
130 | ['onDependenciesChangedEvent', 0], |
||
131 | ], |
||
132 | ScriptEvents::POST_UPDATE_CMD => [ |
||
133 | ['onDependenciesChangedEvent', 0], |
||
134 | ], |
||
135 | ]; |
||
136 | } |
||
137 | |||
138 | /** |
||
139 | * Entry point for post install and post update events. |
||
140 | * |
||
141 | * @throws \InvalidArgumentException |
||
142 | * @throws RuntimeException |
||
143 | * @throws LogicException |
||
144 | * @throws ProcessFailedException |
||
145 | */ |
||
146 | public function onDependenciesChangedEvent() |
||
168 | |||
169 | /** |
||
170 | * Load all paths from PHP_CodeSniffer into an array. |
||
171 | * |
||
172 | * @throws RuntimeException |
||
173 | * @throws LogicException |
||
174 | * @throws ProcessFailedException |
||
175 | */ |
||
176 | private function loadInstalledPaths() |
||
177 | { |
||
178 | if ($this->isPHPCodeSnifferInstalled() === true) { |
||
179 | $output = $this->processBuilder |
||
180 | ->setArguments(['--config-show', self::PHPCS_CONFIG_KEY]) |
||
181 | ->getProcess() |
||
182 | ->mustRun() |
||
183 | ->getOutput(); |
||
184 | |||
185 | $phpcsInstalledPaths = str_replace(self::PHPCS_CONFIG_KEY . ': ', '', $output); |
||
186 | $phpcsInstalledPaths = trim($phpcsInstalledPaths); |
||
187 | |||
188 | if ($phpcsInstalledPaths !== '') { |
||
189 | $this->installedPaths = explode(',', $phpcsInstalledPaths); |
||
190 | } |
||
191 | } |
||
192 | } |
||
193 | |||
194 | /** |
||
195 | * Save all coding standard paths back into PHP_CodeSniffer |
||
196 | * |
||
197 | * @throws RuntimeException |
||
198 | * @throws LogicException |
||
199 | * @throws ProcessFailedException |
||
200 | */ |
||
201 | private function saveInstalledPaths() |
||
234 | |||
235 | /** |
||
236 | * Iterate trough all known paths and check if they are still valid. |
||
237 | * |
||
238 | * If path does not exists, is not an directory or isn't readable, the path |
||
239 | * is removed from the list. |
||
240 | * |
||
241 | * @return bool True if changes where made, false otherwise |
||
242 | */ |
||
243 | private function cleanInstalledPaths() |
||
259 | |||
260 | /** |
||
261 | * Check all installed packages (including the root package) against |
||
262 | * the installed paths from PHP_CodeSniffer and add the missing ones. |
||
263 | * |
||
264 | * @return bool True if changes where made, false otherwise |
||
265 | * |
||
266 | * @throws \InvalidArgumentException |
||
267 | * @throws \RuntimeException |
||
268 | */ |
||
269 | private function updateInstalledPaths() |
||
304 | |||
305 | /** |
||
306 | * Iterates through Composers' local repository looking for valid Coding |
||
307 | * Standard packages. |
||
308 | * |
||
309 | * If the package is the RootPackage (the one the plugin is installed into), |
||
310 | * the package is ignored for now since it needs a different install path logic. |
||
311 | * |
||
312 | * @return array Composer packages containing coding standard(s) |
||
313 | */ |
||
314 | private function getPHPCodingStandardPackages() |
||
334 | |||
335 | /** |
||
336 | * Searches for the installed PHP_CodeSniffer Composer package |
||
337 | * |
||
338 | * @return PackageInterface|null |
||
339 | */ |
||
340 | private function getPHPCodeSnifferPackage() |
||
350 | |||
351 | /** |
||
352 | * Returns the path to the PHP_CodeSniffer package installation location |
||
353 | * |
||
354 | * @return string |
||
355 | */ |
||
356 | private function getPHPCodeSnifferInstallPath() |
||
360 | |||
361 | /** |
||
362 | * Simple check if PHP_CodeSniffer is installed. |
||
363 | * |
||
364 | * @return bool Whether PHP_CodeSniffer is installed |
||
365 | */ |
||
366 | private function isPHPCodeSnifferInstalled() |
||
370 | |||
371 | /** |
||
372 | * Test if composer is running "global" |
||
373 | * This check kinda dirty, but it is the "Composer Way" |
||
374 | * |
||
375 | * @return bool Whether Composer is running "globally" |
||
376 | * |
||
377 | * @throws \RuntimeException |
||
378 | */ |
||
379 | private function isRunningGlobally() |
||
383 | |||
384 | /** |
||
385 | * Returns the relative path to PHP_CodeSniffer from any other absolute path |
||
386 | * |
||
387 | * @param string $to Absolute path |
||
388 | * |
||
389 | * @return string Relative path |
||
390 | */ |
||
391 | private function getRelativePath($to) |
||
425 | } |
||
426 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: