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() |
||
266 | |||
267 | /** |
||
268 | * Get the path to the current PHP version being used. |
||
269 | * |
||
270 | * Duplicate of the same in the EventDispatcher class in Composer itself. |
||
271 | */ |
||
272 | protected function getPhpExecCommand() |
||
290 | |||
291 | /** |
||
292 | * Iterate trough all known paths and check if they are still valid. |
||
293 | * |
||
294 | * If path does not exists, is not an directory or isn't readable, the path |
||
295 | * is removed from the list. |
||
296 | * |
||
297 | * @return bool True if changes where made, false otherwise |
||
298 | */ |
||
299 | private function cleanInstalledPaths() |
||
315 | |||
316 | /** |
||
317 | * Check all installed packages (including the root package) against |
||
318 | * the installed paths from PHP_CodeSniffer and add the missing ones. |
||
319 | * |
||
320 | * @return bool True if changes where made, false otherwise |
||
321 | * |
||
322 | * @throws \InvalidArgumentException |
||
323 | * @throws \RuntimeException |
||
324 | */ |
||
325 | private function updateInstalledPaths() |
||
377 | |||
378 | /** |
||
379 | * Iterates through Composers' local repository looking for valid Coding |
||
380 | * Standard packages. |
||
381 | * |
||
382 | * If the package is the RootPackage (the one the plugin is installed into), |
||
383 | * the package is ignored for now since it needs a different install path logic. |
||
384 | * |
||
385 | * @return array Composer packages containing coding standard(s) |
||
386 | */ |
||
387 | private function getPHPCodingStandardPackages() |
||
407 | |||
408 | /** |
||
409 | * Searches for the installed PHP_CodeSniffer Composer package |
||
410 | * |
||
411 | * @param null|string|\Composer\Semver\Constraint\ConstraintInterface $versionConstraint to match against |
||
412 | * |
||
413 | * @return PackageInterface|null |
||
414 | */ |
||
415 | private function getPHPCodeSnifferPackage($versionConstraint = null) |
||
425 | |||
426 | /** |
||
427 | * Returns the path to the PHP_CodeSniffer package installation location |
||
428 | * |
||
429 | * @return string |
||
430 | */ |
||
431 | private function getPHPCodeSnifferInstallPath() |
||
435 | |||
436 | /** |
||
437 | * Simple check if PHP_CodeSniffer is installed. |
||
438 | * |
||
439 | * @param null|string|\Composer\Semver\Constraint\ConstraintInterface $versionConstraint to match against |
||
440 | * |
||
441 | * @return bool Whether PHP_CodeSniffer is installed |
||
442 | */ |
||
443 | private function isPHPCodeSnifferInstalled($versionConstraint = null) |
||
447 | |||
448 | /** |
||
449 | * Test if composer is running "global" |
||
450 | * This check kinda dirty, but it is the "Composer Way" |
||
451 | * |
||
452 | * @return bool Whether Composer is running "globally" |
||
453 | * |
||
454 | * @throws \RuntimeException |
||
455 | */ |
||
456 | private function isRunningGlobally() |
||
460 | |||
461 | /** |
||
462 | * Determines the maximum search depth when searching for Coding Standards. |
||
463 | * |
||
464 | * @return int |
||
465 | * |
||
466 | * @throws \InvalidArgumentException |
||
467 | */ |
||
468 | private function getMaxDepth() |
||
497 | |||
498 | /** |
||
499 | * Returns the minimal search depth for Coding Standard packages. |
||
500 | * |
||
501 | * Usually this is 0, unless PHP_CodeSniffer >= 3 is used. |
||
502 | * |
||
503 | * @return int |
||
504 | */ |
||
505 | private function getMinDepth() |
||
512 | } |
||
513 |
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: