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 |
||
| 34 | class Plugin implements PluginInterface, EventSubscriberInterface |
||
| 35 | { |
||
| 36 | |||
| 37 | const KEY_MAX_DEPTH = 'phpcodesniffer-search-depth'; |
||
| 38 | |||
| 39 | const MESSAGE_ERROR_WRONG_MAX_DEPTH = |
||
| 40 | 'The value of "%s" (in the composer.json "extra".section) must be an integer larger then %d, %s given.'; |
||
| 41 | const MESSAGE_NOT_INSTALLED = 'PHPCodeSniffer is not installed'; |
||
| 42 | const MESSAGE_NOTHING_TO_INSTALL = 'Nothing to install or update'; |
||
| 43 | const MESSAGE_RUNNING_INSTALLER = 'Running PHPCodeSniffer Composer Installer'; |
||
| 44 | |||
| 45 | const PACKAGE_NAME = 'squizlabs/php_codesniffer'; |
||
| 46 | const PACKAGE_TYPE = 'phpcodesniffer-standard'; |
||
| 47 | |||
| 48 | const PHPCS_CONFIG_KEY = 'installed_paths'; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var Composer |
||
| 52 | */ |
||
| 53 | private $composer; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var string |
||
| 57 | */ |
||
| 58 | private $cwd; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var Filesystem |
||
| 62 | */ |
||
| 63 | private $filesystem; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var array |
||
| 67 | */ |
||
| 68 | private $installedPaths; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @var IOInterface |
||
| 72 | */ |
||
| 73 | private $io; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @var ProcessExecutor |
||
| 77 | */ |
||
| 78 | private $processExecutor; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Triggers the plugin's main functionality. |
||
| 82 | * |
||
| 83 | * Makes it possible to run the plugin as a custom command. |
||
| 84 | * |
||
| 85 | * @param Event $event |
||
| 86 | * |
||
| 87 | * @throws \InvalidArgumentException |
||
| 88 | * @throws \RuntimeException |
||
| 89 | * @throws LogicException |
||
| 90 | * @throws ProcessFailedException |
||
| 91 | * @throws RuntimeException |
||
| 92 | */ |
||
| 93 | public static function run(Event $event) |
||
| 105 | |||
| 106 | /** |
||
| 107 | * {@inheritDoc} |
||
| 108 | * |
||
| 109 | * @throws \RuntimeException |
||
| 110 | * @throws LogicException |
||
| 111 | * @throws ProcessFailedException |
||
| 112 | * @throws RuntimeException |
||
| 113 | */ |
||
| 114 | public function activate(Composer $composer, IOInterface $io) |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Prepares the plugin so it's main functionality can be run. |
||
| 124 | * |
||
| 125 | * @throws \RuntimeException |
||
| 126 | * @throws LogicException |
||
| 127 | * @throws ProcessFailedException |
||
| 128 | * @throws RuntimeException |
||
| 129 | */ |
||
| 130 | private function init() |
||
| 138 | |||
| 139 | /** |
||
| 140 | * {@inheritDoc} |
||
| 141 | */ |
||
| 142 | public static function getSubscribedEvents() |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Entry point for post install and post update events. |
||
| 156 | * |
||
| 157 | * @throws \InvalidArgumentException |
||
| 158 | * @throws LogicException |
||
| 159 | * @throws ProcessFailedException |
||
| 160 | * @throws RuntimeException |
||
| 161 | */ |
||
| 162 | public function onDependenciesChangedEvent() |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Load all paths from PHP_CodeSniffer into an array. |
||
| 188 | * |
||
| 189 | * @throws LogicException |
||
| 190 | * @throws ProcessFailedException |
||
| 191 | * @throws RuntimeException |
||
| 192 | */ |
||
| 193 | private function loadInstalledPaths() |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Save all coding standard paths back into PHP_CodeSniffer |
||
| 216 | * |
||
| 217 | * @throws LogicException |
||
| 218 | * @throws ProcessFailedException |
||
| 219 | * @throws RuntimeException |
||
| 220 | */ |
||
| 221 | private function saveInstalledPaths() |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Iterate trough all known paths and check if they are still valid. |
||
| 259 | * |
||
| 260 | * If path does not exists, is not an directory or isn't readable, the path |
||
| 261 | * is removed from the list. |
||
| 262 | * |
||
| 263 | * @return bool True if changes where made, false otherwise |
||
| 264 | */ |
||
| 265 | private function cleanInstalledPaths() |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Check all installed packages (including the root package) against |
||
| 284 | * the installed paths from PHP_CodeSniffer and add the missing ones. |
||
| 285 | * |
||
| 286 | * @return bool True if changes where made, false otherwise |
||
| 287 | * |
||
| 288 | * @throws \InvalidArgumentException |
||
| 289 | * @throws \RuntimeException |
||
| 290 | */ |
||
| 291 | private function updateInstalledPaths() |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Iterates through Composers' local repository looking for valid Coding |
||
| 346 | * Standard packages. |
||
| 347 | * |
||
| 348 | * If the package is the RootPackage (the one the plugin is installed into), |
||
| 349 | * the package is ignored for now since it needs a different install path logic. |
||
| 350 | * |
||
| 351 | * @return array Composer packages containing coding standard(s) |
||
| 352 | */ |
||
| 353 | private function getPHPCodingStandardPackages() |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Searches for the installed PHP_CodeSniffer Composer package |
||
| 376 | * |
||
| 377 | * @param null|string|\Composer\Semver\Constraint\ConstraintInterface $versionConstraint to match against |
||
| 378 | * |
||
| 379 | * @return PackageInterface|null |
||
| 380 | */ |
||
| 381 | private function getPHPCodeSnifferPackage($versionConstraint = null) |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Returns the path to the PHP_CodeSniffer package installation location |
||
| 394 | * |
||
| 395 | * @return string |
||
| 396 | */ |
||
| 397 | private function getPHPCodeSnifferInstallPath() |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Simple check if PHP_CodeSniffer is installed. |
||
| 404 | * |
||
| 405 | * @param null|string|\Composer\Semver\Constraint\ConstraintInterface $versionConstraint to match against |
||
| 406 | * |
||
| 407 | * @return bool Whether PHP_CodeSniffer is installed |
||
| 408 | */ |
||
| 409 | private function isPHPCodeSnifferInstalled($versionConstraint = null) |
||
| 413 | |||
| 414 | /** |
||
| 415 | * Test if composer is running "global" |
||
| 416 | * This check kinda dirty, but it is the "Composer Way" |
||
| 417 | * |
||
| 418 | * @return bool Whether Composer is running "globally" |
||
| 419 | * |
||
| 420 | * @throws \RuntimeException |
||
| 421 | */ |
||
| 422 | private function isRunningGlobally() |
||
| 426 | |||
| 427 | /** |
||
| 428 | * Determines the maximum search depth when searching for Coding Standards. |
||
| 429 | * |
||
| 430 | * @return int |
||
| 431 | * |
||
| 432 | * @throws \InvalidArgumentException |
||
| 433 | */ |
||
| 434 | private function getMaxDepth() |
||
| 463 | |||
| 464 | /** |
||
| 465 | * Returns the minimal search depth for Coding Standard packages. |
||
| 466 | * |
||
| 467 | * Usually this is 0, unless PHP_CodeSniffer >= 3 is used. |
||
| 468 | * |
||
| 469 | * @return int |
||
| 470 | */ |
||
| 471 | private function getMinDepth() |
||
| 478 | } |
||
| 479 |
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: