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) |
||
| 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() |
||
| 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() |
||
| 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() |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Searches for the installed PHP_CodeSniffer Composer package |
||
| 335 | * |
||
| 336 | * @return PackageInterface|null |
||
| 337 | */ |
||
| 338 | private function getPHPCodeSnifferPackage() |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Returns the path to the PHP_CodeSniffer package installation location |
||
| 351 | * |
||
| 352 | * @return string |
||
| 353 | */ |
||
| 354 | private function getPHPCodeSnifferInstallPath() |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Simple check if PHP_CodeSniffer is installed. |
||
| 361 | * |
||
| 362 | * @return bool Whether PHP_CodeSniffer is installed |
||
| 363 | */ |
||
| 364 | private function isPHPCodeSnifferInstalled() |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Test if composer is running "global" |
||
| 371 | * This check kinda dirty, but it is the "Composer Way" |
||
| 372 | * |
||
| 373 | * @return bool Whether Composer is running "globally" |
||
| 374 | * |
||
| 375 | * @throws \RuntimeException |
||
| 376 | */ |
||
| 377 | private function isRunningGlobally() |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Returns the relative path to PHP_CodeSniffer from any other absolute path |
||
| 384 | * |
||
| 385 | * @param string $to Absolute path |
||
| 386 | * |
||
| 387 | * @return string Relative path |
||
| 388 | */ |
||
| 389 | private function getRelativePath($to) |
||
| 423 | } |
||
| 424 |
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: