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 |
||
| 33 | class Plugin implements PluginInterface, EventSubscriberInterface |
||
| 34 | { |
||
| 35 | const MESSAGE_RUNNING_INSTALLER = 'Running PHPCodeSniffer Composer Installer'; |
||
| 36 | const MESSAGE_NOTHING_TO_INSTALL = 'Nothing to install or update'; |
||
| 37 | const MESSAGE_NOT_INSTALLED = 'PHPCodeSniffer is not installed'; |
||
| 38 | |||
| 39 | const PACKAGE_NAME = 'squizlabs/php_codesniffer'; |
||
| 40 | const PACKAGE_TYPE = 'phpcodesniffer-standard'; |
||
| 41 | |||
| 42 | const PHPCS_CONFIG_KEY = 'installed_paths'; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var Composer |
||
| 46 | */ |
||
| 47 | private $composer; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var IOInterface |
||
| 51 | */ |
||
| 52 | private $io; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var array |
||
| 56 | */ |
||
| 57 | private $installedPaths; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var ProcessBuilder |
||
| 61 | */ |
||
| 62 | private $processBuilder; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Triggers the plugin's main functionality. |
||
| 66 | * |
||
| 67 | * Makes it possible to run the plugin as a custom command. |
||
| 68 | * |
||
| 69 | * @param Event $event |
||
| 70 | * |
||
| 71 | * @throws \InvalidArgumentException |
||
| 72 | * @throws \RuntimeException |
||
| 73 | * @throws LogicException |
||
| 74 | * @throws ProcessFailedException |
||
| 75 | * @throws RuntimeException |
||
| 76 | */ |
||
| 77 | public static function run(Event $event) |
||
| 89 | |||
| 90 | /** |
||
| 91 | * {@inheritDoc} |
||
| 92 | * |
||
| 93 | * @throws \RuntimeException |
||
| 94 | * @throws LogicException |
||
| 95 | * @throws RuntimeException |
||
| 96 | * @throws ProcessFailedException |
||
| 97 | */ |
||
| 98 | public function activate(Composer $composer, IOInterface $io) |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Prepares the plugin so it's main functionality can be run. |
||
| 108 | * |
||
| 109 | * @throws \RuntimeException |
||
| 110 | * @throws LogicException |
||
| 111 | * @throws ProcessFailedException |
||
| 112 | * @throws RuntimeException |
||
| 113 | */ |
||
| 114 | private function init() |
||
| 123 | |||
| 124 | /** |
||
| 125 | * {@inheritDoc} |
||
| 126 | */ |
||
| 127 | public static function getSubscribedEvents() |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Entry point for post install and post update events. |
||
| 141 | * |
||
| 142 | * @throws \InvalidArgumentException |
||
| 143 | * @throws RuntimeException |
||
| 144 | * @throws LogicException |
||
| 145 | * @throws ProcessFailedException |
||
| 146 | */ |
||
| 147 | public function onDependenciesChangedEvent() |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Load all paths from PHP_CodeSniffer into an array. |
||
| 172 | * |
||
| 173 | * @throws RuntimeException |
||
| 174 | * @throws LogicException |
||
| 175 | * @throws ProcessFailedException |
||
| 176 | */ |
||
| 177 | private function loadInstalledPaths() |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Save all coding standard paths back into PHP_CodeSniffer |
||
| 197 | * |
||
| 198 | * @throws RuntimeException |
||
| 199 | * @throws LogicException |
||
| 200 | * @throws ProcessFailedException |
||
| 201 | */ |
||
| 202 | private function saveInstalledPaths() |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Iterate trough all known paths and check if they are still valid. |
||
| 238 | * |
||
| 239 | * If path does not exists, is not an directory or isn't readable, the path |
||
| 240 | * is removed from the list. |
||
| 241 | * |
||
| 242 | * @return bool True if changes where made, false otherwise |
||
| 243 | */ |
||
| 244 | private function cleanInstalledPaths() |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Check all installed packages (including the root package) against |
||
| 263 | * the installed paths from PHP_CodeSniffer and add the missing ones. |
||
| 264 | * |
||
| 265 | * @return bool True if changes where made, false otherwise |
||
| 266 | * |
||
| 267 | * @throws \InvalidArgumentException |
||
| 268 | * @throws \RuntimeException |
||
| 269 | */ |
||
| 270 | private function updateInstalledPaths() |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Iterates through Composers' local repository looking for valid Coding |
||
| 318 | * Standard packages. |
||
| 319 | * |
||
| 320 | * If the package is the RootPackage (the one the plugin is installed into), |
||
| 321 | * the package is ignored for now since it needs a different install path logic. |
||
| 322 | * |
||
| 323 | * @return array Composer packages containing coding standard(s) |
||
| 324 | */ |
||
| 325 | private function getPHPCodingStandardPackages() |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Searches for the installed PHP_CodeSniffer Composer package |
||
| 348 | * |
||
| 349 | * @param null|string|\Composer\Semver\Constraint\ConstraintInterface $versionConstraint to match against |
||
| 350 | * |
||
| 351 | * @return PackageInterface|null |
||
| 352 | */ |
||
| 353 | private function getPHPCodeSnifferPackage($versionConstraint = null) |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Returns the path to the PHP_CodeSniffer package installation location |
||
| 366 | * |
||
| 367 | * @return string |
||
| 368 | */ |
||
| 369 | private function getPHPCodeSnifferInstallPath() |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Simple check if PHP_CodeSniffer is installed. |
||
| 376 | * |
||
| 377 | * @param null|string|\Composer\Semver\Constraint\ConstraintInterface $versionConstraint to match against |
||
| 378 | * |
||
| 379 | * @return bool Whether PHP_CodeSniffer is installed |
||
| 380 | */ |
||
| 381 | private function isPHPCodeSnifferInstalled($versionConstraint = null) |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Test if composer is running "global" |
||
| 388 | * This check kinda dirty, but it is the "Composer Way" |
||
| 389 | * |
||
| 390 | * @return bool Whether Composer is running "globally" |
||
| 391 | * |
||
| 392 | * @throws \RuntimeException |
||
| 393 | */ |
||
| 394 | private function isRunningGlobally() |
||
| 398 | |||
| 399 | /** |
||
| 400 | * Returns the relative path to PHP_CodeSniffer from any other absolute path |
||
| 401 | * |
||
| 402 | * @param string $to Absolute path |
||
| 403 | * |
||
| 404 | * @return string Relative path |
||
| 405 | */ |
||
| 406 | private function getRelativePath($to) |
||
| 440 | } |
||
| 441 |
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: