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 | |||
36 | const KEY_MAX_DEPTH = 'phpcodesniffer-search-depth'; |
||
37 | |||
38 | const MESSAGE_ERROR_WRONG_MAX_DEPTH = |
||
39 | 'The value of "%s" (in the composer.json "extra".section) must be an integer larger then %d, %s given.'; |
||
40 | const MESSAGE_RUNNING_INSTALLER = 'Running PHPCodeSniffer Composer Installer'; |
||
41 | const MESSAGE_NOTHING_TO_INSTALL = 'Nothing to install or update'; |
||
42 | const MESSAGE_NOT_INSTALLED = 'PHPCodeSniffer is not installed'; |
||
43 | |||
44 | const PACKAGE_NAME = 'squizlabs/php_codesniffer'; |
||
45 | const PACKAGE_TYPE = 'phpcodesniffer-standard'; |
||
46 | |||
47 | const PHPCS_CONFIG_KEY = 'installed_paths'; |
||
48 | |||
49 | /** |
||
50 | * @var Composer |
||
51 | */ |
||
52 | private $composer; |
||
53 | |||
54 | /** |
||
55 | * @var IOInterface |
||
56 | */ |
||
57 | private $io; |
||
58 | |||
59 | /** |
||
60 | * @var array |
||
61 | */ |
||
62 | private $installedPaths; |
||
63 | |||
64 | /** |
||
65 | * @var ProcessBuilder |
||
66 | */ |
||
67 | private $processBuilder; |
||
68 | |||
69 | /** |
||
70 | * Triggers the plugin's main functionality. |
||
71 | * |
||
72 | * Makes it possible to run the plugin as a custom command. |
||
73 | * |
||
74 | * @param Event $event |
||
75 | * |
||
76 | * @throws \InvalidArgumentException |
||
77 | * @throws \RuntimeException |
||
78 | * @throws LogicException |
||
79 | * @throws ProcessFailedException |
||
80 | * @throws RuntimeException |
||
81 | */ |
||
82 | public static function run(Event $event) |
||
83 | { |
||
84 | $io = $event->getIO(); |
||
85 | $composer = $event->getComposer(); |
||
86 | |||
87 | $instance = new static(); |
||
88 | |||
89 | $instance->io = $io; |
||
90 | $instance->composer = $composer; |
||
91 | $instance->init(); |
||
92 | $instance->onDependenciesChangedEvent(); |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * {@inheritDoc} |
||
97 | * |
||
98 | * @throws \RuntimeException |
||
99 | * @throws LogicException |
||
100 | * @throws RuntimeException |
||
101 | * @throws ProcessFailedException |
||
102 | */ |
||
103 | public function activate(Composer $composer, IOInterface $io) |
||
104 | { |
||
105 | $this->composer = $composer; |
||
106 | $this->io = $io; |
||
107 | |||
108 | $this->init(); |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * Prepares the plugin so it's main functionality can be run. |
||
113 | * |
||
114 | * @throws \RuntimeException |
||
115 | * @throws LogicException |
||
116 | * @throws ProcessFailedException |
||
117 | * @throws RuntimeException |
||
118 | */ |
||
119 | private function init() |
||
120 | { |
||
121 | $this->installedPaths = array(); |
||
122 | |||
123 | $this->processBuilder = new ProcessBuilder(); |
||
124 | $this->processBuilder->setPrefix($this->composer->getConfig()->get('bin-dir') . DIRECTORY_SEPARATOR . 'phpcs'); |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * {@inheritDoc} |
||
129 | */ |
||
130 | public static function getSubscribedEvents() |
||
131 | { |
||
132 | return array( |
||
133 | ScriptEvents::POST_INSTALL_CMD => array( |
||
134 | array('onDependenciesChangedEvent', 0), |
||
135 | ), |
||
136 | ScriptEvents::POST_UPDATE_CMD => array( |
||
137 | array('onDependenciesChangedEvent', 0), |
||
138 | ), |
||
139 | ); |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * Entry point for post install and post update events. |
||
144 | * |
||
145 | * @throws \InvalidArgumentException |
||
146 | * @throws RuntimeException |
||
147 | * @throws LogicException |
||
148 | * @throws ProcessFailedException |
||
149 | */ |
||
150 | public function onDependenciesChangedEvent() |
||
151 | { |
||
152 | $io = $this->io; |
||
153 | $isVerbose = $io->isVerbose(); |
||
154 | |||
155 | if ($isVerbose) { |
||
156 | $io->write(sprintf('<info>%s</info>', self::MESSAGE_RUNNING_INSTALLER)); |
||
157 | } |
||
158 | |||
159 | if ($this->isPHPCodeSnifferInstalled() === true) { |
||
160 | $this->loadInstalledPaths(); |
||
161 | $installPathCleaned = $this->cleanInstalledPaths(); |
||
162 | $installPathUpdated = $this->updateInstalledPaths(); |
||
163 | |||
164 | if ($installPathCleaned === true || $installPathUpdated === true) { |
||
165 | $this->saveInstalledPaths(); |
||
166 | } elseif ($isVerbose) { |
||
167 | $io->write(sprintf('<info>%s</info>', self::MESSAGE_NOTHING_TO_INSTALL)); |
||
168 | } |
||
169 | } elseif ($isVerbose) { |
||
170 | $io->write(sprintf('<info>%s</info>', self::MESSAGE_NOT_INSTALLED)); |
||
171 | } |
||
172 | } |
||
173 | |||
174 | /** |
||
175 | * Load all paths from PHP_CodeSniffer into an array. |
||
176 | * |
||
177 | * @throws RuntimeException |
||
178 | * @throws LogicException |
||
179 | * @throws ProcessFailedException |
||
180 | */ |
||
181 | private function loadInstalledPaths() |
||
182 | { |
||
183 | if ($this->isPHPCodeSnifferInstalled() === true) { |
||
184 | $output = $this->processBuilder |
||
185 | ->setArguments(array('--config-show', self::PHPCS_CONFIG_KEY)) |
||
186 | ->getProcess() |
||
187 | ->mustRun() |
||
188 | ->getOutput(); |
||
189 | |||
190 | $phpcsInstalledPaths = str_replace(self::PHPCS_CONFIG_KEY . ': ', '', $output); |
||
191 | $phpcsInstalledPaths = trim($phpcsInstalledPaths); |
||
192 | |||
193 | if ($phpcsInstalledPaths !== '') { |
||
194 | $this->installedPaths = explode(',', $phpcsInstalledPaths); |
||
195 | } |
||
196 | } |
||
197 | } |
||
198 | |||
199 | /** |
||
200 | * Save all coding standard paths back into PHP_CodeSniffer |
||
201 | * |
||
202 | * @throws RuntimeException |
||
203 | * @throws LogicException |
||
204 | * @throws ProcessFailedException |
||
205 | */ |
||
206 | private function saveInstalledPaths() |
||
207 | { |
||
208 | // Check if we found installed paths to set. |
||
209 | if (count($this->installedPaths) !== 0) { |
||
210 | $paths = implode(',', $this->installedPaths); |
||
211 | $arguments = array('--config-set', self::PHPCS_CONFIG_KEY, $paths); |
||
212 | $configMessage = sprintf( |
||
213 | 'PHP CodeSniffer Config <info>%s</info> <comment>set to</comment> <info>%s</info>', |
||
214 | self::PHPCS_CONFIG_KEY, |
||
215 | $paths |
||
216 | ); |
||
217 | } else { |
||
218 | // Delete the installed paths if none were found. |
||
219 | $arguments = array('--config-delete', self::PHPCS_CONFIG_KEY); |
||
220 | $configMessage = sprintf( |
||
221 | 'PHP CodeSniffer Config <info>%s</info> <comment>delete</comment>', |
||
222 | self::PHPCS_CONFIG_KEY |
||
223 | ); |
||
224 | } |
||
225 | |||
226 | $this->io->write($configMessage); |
||
227 | |||
228 | $configResult = $this->processBuilder |
||
229 | ->setArguments($arguments) |
||
230 | ->getProcess() |
||
231 | ->mustRun() |
||
232 | ->getOutput() |
||
233 | ; |
||
234 | |||
235 | if ($this->io->isVerbose() && !empty($configResult)) { |
||
236 | $this->io->write(sprintf('<info>%s</info>', $configResult)); |
||
237 | } |
||
238 | } |
||
239 | |||
240 | /** |
||
241 | * Iterate trough all known paths and check if they are still valid. |
||
242 | * |
||
243 | * If path does not exists, is not an directory or isn't readable, the path |
||
244 | * is removed from the list. |
||
245 | * |
||
246 | * @return bool True if changes where made, false otherwise |
||
247 | */ |
||
248 | private function cleanInstalledPaths() |
||
249 | { |
||
250 | $changes = false; |
||
251 | foreach ($this->installedPaths as $key => $path) { |
||
252 | // This might be a relative path as well |
||
253 | $alternativePath = realpath($this->getPHPCodeSnifferInstallPath() . DIRECTORY_SEPARATOR . $path); |
||
254 | |||
255 | if ((is_dir($path) === false || is_readable($path) === false) && |
||
256 | (is_dir($alternativePath) === false || is_readable($alternativePath) === false) |
||
257 | ) { |
||
258 | unset($this->installedPaths[$key]); |
||
259 | $changes = true; |
||
260 | } |
||
261 | } |
||
262 | return $changes; |
||
263 | } |
||
264 | |||
265 | /** |
||
266 | * Check all installed packages (including the root package) against |
||
267 | * the installed paths from PHP_CodeSniffer and add the missing ones. |
||
268 | * |
||
269 | * @return bool True if changes where made, false otherwise |
||
270 | * |
||
271 | * @throws \InvalidArgumentException |
||
272 | * @throws \RuntimeException |
||
273 | */ |
||
274 | private function updateInstalledPaths() |
||
275 | { |
||
276 | $changes = false; |
||
277 | |||
278 | $searchPaths = array(getcwd()); |
||
279 | $codingStandardPackages = $this->getPHPCodingStandardPackages(); |
||
280 | foreach ($codingStandardPackages as $package) { |
||
281 | $searchPaths[] = $this->composer->getInstallationManager()->getInstallPath($package); |
||
282 | } |
||
283 | |||
284 | $finder = new Finder(); |
||
285 | $finder->files() |
||
286 | ->ignoreUnreadableDirs() |
||
287 | ->ignoreVCS(true) |
||
288 | ->depth('<= ' . $this->getMaxDepth()) |
||
289 | ->depth('>= ' . $this->getMinDepth()) |
||
290 | ->name('ruleset.xml') |
||
291 | ->in($searchPaths); |
||
292 | |||
293 | // Process each found possible ruleset. |
||
294 | foreach ($finder as $ruleset) { |
||
295 | $standardsPath = $ruleset->getPath(); |
||
296 | |||
297 | // Pick the directory above the directory containing the standard, unless this is the project root. |
||
298 | if ($standardsPath !== getcwd()) { |
||
299 | $standardsPath = dirname($standardsPath); |
||
300 | } |
||
301 | |||
302 | // Use relative paths for local project repositories. |
||
303 | if ($this->isRunningGlobally() === false) { |
||
304 | $standardsPath = $this->getRelativePath($standardsPath); |
||
305 | } |
||
306 | |||
307 | // De-duplicate and add when directory is not configured. |
||
308 | if (in_array($standardsPath, $this->installedPaths, true) === false) { |
||
309 | $this->installedPaths[] = $standardsPath; |
||
310 | $changes = true; |
||
311 | } |
||
312 | } |
||
313 | |||
314 | return $changes; |
||
315 | } |
||
316 | |||
317 | /** |
||
318 | * Iterates through Composers' local repository looking for valid Coding |
||
319 | * Standard packages. |
||
320 | * |
||
321 | * If the package is the RootPackage (the one the plugin is installed into), |
||
322 | * the package is ignored for now since it needs a different install path logic. |
||
323 | * |
||
324 | * @return array Composer packages containing coding standard(s) |
||
325 | */ |
||
326 | private function getPHPCodingStandardPackages() |
||
346 | |||
347 | /** |
||
348 | * Searches for the installed PHP_CodeSniffer Composer package |
||
349 | * |
||
350 | * @param null|string|\Composer\Semver\Constraint\ConstraintInterface $versionConstraint to match against |
||
351 | * |
||
352 | * @return PackageInterface|null |
||
353 | */ |
||
354 | private function getPHPCodeSnifferPackage($versionConstraint = null) |
||
364 | |||
365 | /** |
||
366 | * Returns the path to the PHP_CodeSniffer package installation location |
||
367 | * |
||
368 | * @return string |
||
369 | */ |
||
370 | private function getPHPCodeSnifferInstallPath() |
||
374 | |||
375 | /** |
||
376 | * Simple check if PHP_CodeSniffer is installed. |
||
377 | * |
||
378 | * @param null|string|\Composer\Semver\Constraint\ConstraintInterface $versionConstraint to match against |
||
379 | * |
||
380 | * @return bool Whether PHP_CodeSniffer is installed |
||
381 | */ |
||
382 | private function isPHPCodeSnifferInstalled($versionConstraint = null) |
||
386 | |||
387 | /** |
||
388 | * Test if composer is running "global" |
||
389 | * This check kinda dirty, but it is the "Composer Way" |
||
390 | * |
||
391 | * @return bool Whether Composer is running "globally" |
||
392 | * |
||
393 | * @throws \RuntimeException |
||
394 | */ |
||
395 | private function isRunningGlobally() |
||
399 | |||
400 | /** |
||
401 | * Returns the relative path to PHP_CodeSniffer from any other absolute path |
||
402 | * |
||
403 | * @param string $to Absolute path |
||
404 | * |
||
405 | * @return string Relative path |
||
406 | */ |
||
407 | private function getRelativePath($to) |
||
441 | |||
442 | /** |
||
443 | * Determines the maximum search depth when searching for Coding Standards. |
||
444 | * |
||
445 | * @return int |
||
446 | * |
||
447 | * @throws \InvalidArgumentException |
||
448 | */ |
||
449 | private function getMaxDepth() |
||
450 | { |
||
478 | |||
479 | /** |
||
480 | * Returns the minimal search depth for Coding Standard packages. |
||
481 | * |
||
482 | * Usually this is 0, unless PHP_CodeSniffer >= 3 is used. |
||
483 | * |
||
484 | * @return int |
||
485 | */ |
||
486 | private function getMinDepth() |
||
493 | } |
||
494 |
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: