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_RUNNING_INSTALLER = 'Running PHPCodeSniffer Composer Installer'; |
||
42 | const MESSAGE_NOTHING_TO_INSTALL = 'Nothing to install or update'; |
||
43 | const MESSAGE_NOT_INSTALLED = 'PHPCodeSniffer is not installed'; |
||
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 IOInterface |
||
57 | */ |
||
58 | private $io; |
||
59 | |||
60 | /** |
||
61 | * @var array |
||
62 | */ |
||
63 | private $installedPaths; |
||
64 | |||
65 | /** |
||
66 | * @var ProcessExecutor |
||
67 | */ |
||
68 | private $processExecutor; |
||
69 | |||
70 | /** |
||
71 | * @var Filesystem |
||
72 | */ |
||
73 | private $filesystem; |
||
74 | |||
75 | /** |
||
76 | * Triggers the plugin's main functionality. |
||
77 | * |
||
78 | * Makes it possible to run the plugin as a custom command. |
||
79 | * |
||
80 | * @param Event $event |
||
81 | * |
||
82 | * @throws \InvalidArgumentException |
||
83 | * @throws \RuntimeException |
||
84 | * @throws LogicException |
||
85 | * @throws ProcessFailedException |
||
86 | * @throws RuntimeException |
||
87 | */ |
||
88 | public static function run(Event $event) |
||
100 | |||
101 | /** |
||
102 | * {@inheritDoc} |
||
103 | * |
||
104 | * @throws \RuntimeException |
||
105 | * @throws LogicException |
||
106 | * @throws RuntimeException |
||
107 | * @throws ProcessFailedException |
||
108 | */ |
||
109 | public function activate(Composer $composer, IOInterface $io) |
||
116 | |||
117 | /** |
||
118 | * Prepares the plugin so it's main functionality can be run. |
||
119 | * |
||
120 | * @throws \RuntimeException |
||
121 | * @throws LogicException |
||
122 | * @throws ProcessFailedException |
||
123 | * @throws RuntimeException |
||
124 | */ |
||
125 | private function init() |
||
131 | |||
132 | /** |
||
133 | * {@inheritDoc} |
||
134 | */ |
||
135 | public static function getSubscribedEvents() |
||
146 | |||
147 | /** |
||
148 | * Entry point for post install and post update events. |
||
149 | * |
||
150 | * @throws \InvalidArgumentException |
||
151 | * @throws RuntimeException |
||
152 | * @throws LogicException |
||
153 | * @throws ProcessFailedException |
||
154 | */ |
||
155 | public function onDependenciesChangedEvent() |
||
178 | |||
179 | /** |
||
180 | * Load all paths from PHP_CodeSniffer into an array. |
||
181 | * |
||
182 | * @throws RuntimeException |
||
183 | * @throws LogicException |
||
184 | * @throws ProcessFailedException |
||
185 | */ |
||
186 | private function loadInstalledPaths() |
||
206 | |||
207 | /** |
||
208 | * Save all coding standard paths back into PHP_CodeSniffer |
||
209 | * |
||
210 | * @throws RuntimeException |
||
211 | * @throws LogicException |
||
212 | * @throws ProcessFailedException |
||
213 | */ |
||
214 | private function saveInstalledPaths() |
||
249 | |||
250 | /** |
||
251 | * Iterate trough all known paths and check if they are still valid. |
||
252 | * |
||
253 | * If path does not exists, is not an directory or isn't readable, the path |
||
254 | * is removed from the list. |
||
255 | * |
||
256 | * @return bool True if changes where made, false otherwise |
||
257 | */ |
||
258 | private function cleanInstalledPaths() |
||
274 | |||
275 | /** |
||
276 | * Check all installed packages (including the root package) against |
||
277 | * the installed paths from PHP_CodeSniffer and add the missing ones. |
||
278 | * |
||
279 | * @return bool True if changes where made, false otherwise |
||
280 | * |
||
281 | * @throws \InvalidArgumentException |
||
282 | * @throws \RuntimeException |
||
283 | */ |
||
284 | private function updateInstalledPaths() |
||
330 | |||
331 | /** |
||
332 | * Iterates through Composers' local repository looking for valid Coding |
||
333 | * Standard packages. |
||
334 | * |
||
335 | * If the package is the RootPackage (the one the plugin is installed into), |
||
336 | * the package is ignored for now since it needs a different install path logic. |
||
337 | * |
||
338 | * @return array Composer packages containing coding standard(s) |
||
339 | */ |
||
340 | private function getPHPCodingStandardPackages() |
||
360 | |||
361 | /** |
||
362 | * Searches for the installed PHP_CodeSniffer Composer package |
||
363 | * |
||
364 | * @param null|string|\Composer\Semver\Constraint\ConstraintInterface $versionConstraint to match against |
||
365 | * |
||
366 | * @return PackageInterface|null |
||
367 | */ |
||
368 | private function getPHPCodeSnifferPackage($versionConstraint = null) |
||
378 | |||
379 | /** |
||
380 | * Returns the path to the PHP_CodeSniffer package installation location |
||
381 | * |
||
382 | * @return string |
||
383 | */ |
||
384 | private function getPHPCodeSnifferInstallPath() |
||
388 | |||
389 | /** |
||
390 | * Simple check if PHP_CodeSniffer is installed. |
||
391 | * |
||
392 | * @param null|string|\Composer\Semver\Constraint\ConstraintInterface $versionConstraint to match against |
||
393 | * |
||
394 | * @return bool Whether PHP_CodeSniffer is installed |
||
395 | */ |
||
396 | private function isPHPCodeSnifferInstalled($versionConstraint = null) |
||
400 | |||
401 | /** |
||
402 | * Test if composer is running "global" |
||
403 | * This check kinda dirty, but it is the "Composer Way" |
||
404 | * |
||
405 | * @return bool Whether Composer is running "globally" |
||
406 | * |
||
407 | * @throws \RuntimeException |
||
408 | */ |
||
409 | private function isRunningGlobally() |
||
413 | |||
414 | /** |
||
415 | * Determines the maximum search depth when searching for Coding Standards. |
||
416 | * |
||
417 | * @return int |
||
418 | * |
||
419 | * @throws \InvalidArgumentException |
||
420 | */ |
||
421 | private function getMaxDepth() |
||
450 | |||
451 | /** |
||
452 | * Returns the minimal search depth for Coding Standard packages. |
||
453 | * |
||
454 | * Usually this is 0, unless PHP_CodeSniffer >= 3 is used. |
||
455 | * |
||
456 | * @return int |
||
457 | */ |
||
458 | private function getMinDepth() |
||
465 | } |
||
466 |
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: