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 |
||
| 35 | class Plugin implements PluginInterface, EventSubscriberInterface |
||
| 36 | { |
||
| 37 | |||
| 38 | const KEY_MAX_DEPTH = 'phpcodesniffer-search-depth'; |
||
| 39 | |||
| 40 | const MESSAGE_ERROR_WRONG_MAX_DEPTH = |
||
| 41 | 'The value of "%s" (in the composer.json "extra".section) must be an integer larger then %d, %s given.'; |
||
| 42 | const MESSAGE_NOT_INSTALLED = 'PHPCodeSniffer is not installed'; |
||
| 43 | const MESSAGE_NOTHING_TO_INSTALL = 'Nothing to install or update'; |
||
| 44 | const MESSAGE_RUNNING_INSTALLER = 'Running PHPCodeSniffer Composer Installer'; |
||
| 45 | |||
| 46 | const PACKAGE_NAME = 'squizlabs/php_codesniffer'; |
||
| 47 | const PACKAGE_TYPE = 'phpcodesniffer-standard'; |
||
| 48 | |||
| 49 | const PHPCS_CONFIG_KEY = 'installed_paths'; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var Composer |
||
| 53 | */ |
||
| 54 | private $composer; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var string |
||
| 58 | */ |
||
| 59 | private $cwd; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var Filesystem |
||
| 63 | */ |
||
| 64 | private $filesystem; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var array |
||
| 68 | */ |
||
| 69 | private $installedPaths; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var IOInterface |
||
| 73 | */ |
||
| 74 | private $io; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var ProcessExecutor |
||
| 78 | */ |
||
| 79 | private $processExecutor; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Triggers the plugin's main functionality. |
||
| 83 | * |
||
| 84 | * Makes it possible to run the plugin as a custom command. |
||
| 85 | * |
||
| 86 | * @param Event $event |
||
| 87 | * |
||
| 88 | * @throws \InvalidArgumentException |
||
| 89 | * @throws \RuntimeException |
||
| 90 | * @throws LogicException |
||
| 91 | * @throws ProcessFailedException |
||
| 92 | * @throws RuntimeException |
||
| 93 | */ |
||
| 94 | public static function run(Event $event) |
||
| 106 | |||
| 107 | /** |
||
| 108 | * {@inheritDoc} |
||
| 109 | * |
||
| 110 | * @throws \RuntimeException |
||
| 111 | * @throws LogicException |
||
| 112 | * @throws ProcessFailedException |
||
| 113 | * @throws RuntimeException |
||
| 114 | */ |
||
| 115 | public function activate(Composer $composer, IOInterface $io) |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Prepares the plugin so it's main functionality can be run. |
||
| 125 | * |
||
| 126 | * @throws \RuntimeException |
||
| 127 | * @throws LogicException |
||
| 128 | * @throws ProcessFailedException |
||
| 129 | * @throws RuntimeException |
||
| 130 | */ |
||
| 131 | private function init() |
||
| 139 | |||
| 140 | /** |
||
| 141 | * {@inheritDoc} |
||
| 142 | */ |
||
| 143 | public static function getSubscribedEvents() |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Entry point for post install and post update events. |
||
| 157 | * |
||
| 158 | * @throws \InvalidArgumentException |
||
| 159 | * @throws LogicException |
||
| 160 | * @throws ProcessFailedException |
||
| 161 | * @throws RuntimeException |
||
| 162 | */ |
||
| 163 | public function onDependenciesChangedEvent() |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Load all paths from PHP_CodeSniffer into an array. |
||
| 189 | * |
||
| 190 | * @throws LogicException |
||
| 191 | * @throws ProcessFailedException |
||
| 192 | * @throws RuntimeException |
||
| 193 | */ |
||
| 194 | private function loadInstalledPaths() |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Save all coding standard paths back into PHP_CodeSniffer |
||
| 217 | * |
||
| 218 | * @throws LogicException |
||
| 219 | * @throws ProcessFailedException |
||
| 220 | * @throws RuntimeException |
||
| 221 | */ |
||
| 222 | private function saveInstalledPaths() |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Get the path to the current PHP version being used. |
||
| 273 | * |
||
| 274 | * Duplicate of the same in the EventDispatcher class in Composer itself. |
||
| 275 | */ |
||
| 276 | protected function getPhpExecCommand() |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Iterate trough all known paths and check if they are still valid. |
||
| 304 | * |
||
| 305 | * If path does not exists, is not an directory or isn't readable, the path |
||
| 306 | * is removed from the list. |
||
| 307 | * |
||
| 308 | * @return bool True if changes where made, false otherwise |
||
| 309 | */ |
||
| 310 | private function cleanInstalledPaths() |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Check all installed packages (including the root package) against |
||
| 329 | * the installed paths from PHP_CodeSniffer and add the missing ones. |
||
| 330 | * |
||
| 331 | * @return bool True if changes where made, false otherwise |
||
| 332 | * |
||
| 333 | * @throws \InvalidArgumentException |
||
| 334 | * @throws \RuntimeException |
||
| 335 | */ |
||
| 336 | private function updateInstalledPaths() |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Iterates through Composers' local repository looking for valid Coding |
||
| 391 | * Standard packages. |
||
| 392 | * |
||
| 393 | * If the package is the RootPackage (the one the plugin is installed into), |
||
| 394 | * the package is ignored for now since it needs a different install path logic. |
||
| 395 | * |
||
| 396 | * @return array Composer packages containing coding standard(s) |
||
| 397 | */ |
||
| 398 | private function getPHPCodingStandardPackages() |
||
| 418 | |||
| 419 | /** |
||
| 420 | * Searches for the installed PHP_CodeSniffer Composer package |
||
| 421 | * |
||
| 422 | * @param null|string|\Composer\Semver\Constraint\ConstraintInterface $versionConstraint to match against |
||
| 423 | * |
||
| 424 | * @return PackageInterface|null |
||
| 425 | */ |
||
| 426 | private function getPHPCodeSnifferPackage($versionConstraint = null) |
||
| 436 | |||
| 437 | /** |
||
| 438 | * Returns the path to the PHP_CodeSniffer package installation location |
||
| 439 | * |
||
| 440 | * @return string |
||
| 441 | */ |
||
| 442 | private function getPHPCodeSnifferInstallPath() |
||
| 446 | |||
| 447 | /** |
||
| 448 | * Simple check if PHP_CodeSniffer is installed. |
||
| 449 | * |
||
| 450 | * @param null|string|\Composer\Semver\Constraint\ConstraintInterface $versionConstraint to match against |
||
| 451 | * |
||
| 452 | * @return bool Whether PHP_CodeSniffer is installed |
||
| 453 | */ |
||
| 454 | private function isPHPCodeSnifferInstalled($versionConstraint = null) |
||
| 458 | |||
| 459 | /** |
||
| 460 | * Test if composer is running "global" |
||
| 461 | * This check kinda dirty, but it is the "Composer Way" |
||
| 462 | * |
||
| 463 | * @return bool Whether Composer is running "globally" |
||
| 464 | * |
||
| 465 | * @throws \RuntimeException |
||
| 466 | */ |
||
| 467 | private function isRunningGlobally() |
||
| 471 | |||
| 472 | /** |
||
| 473 | * Determines the maximum search depth when searching for Coding Standards. |
||
| 474 | * |
||
| 475 | * @return int |
||
| 476 | * |
||
| 477 | * @throws \InvalidArgumentException |
||
| 478 | */ |
||
| 479 | private function getMaxDepth() |
||
| 508 | |||
| 509 | /** |
||
| 510 | * Returns the minimal search depth for Coding Standard packages. |
||
| 511 | * |
||
| 512 | * Usually this is 0, unless PHP_CodeSniffer >= 3 is used. |
||
| 513 | * |
||
| 514 | * @return int |
||
| 515 | */ |
||
| 516 | private function getMinDepth() |
||
| 523 | } |
||
| 524 |
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: