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 |
||
37 | class Plugin implements PluginInterface, EventSubscriberInterface |
||
38 | { |
||
39 | /** |
||
40 | * The type of packages this plugin supports |
||
41 | */ |
||
42 | const PACKAGE_TYPE = 'magento-module'; |
||
43 | |||
44 | const VENDOR_DIR_KEY = 'vendor-dir'; |
||
45 | |||
46 | const BIN_DIR_KEY = 'bin-dir'; |
||
47 | |||
48 | const THESEER_AUTOLOAD_EXEC_BIN_PATH = '/phpab'; |
||
49 | |||
50 | const THESEER_AUTOLOAD_EXEC_REL_PATH = '/theseer/autoload/composer/bin/phpab'; |
||
51 | |||
52 | /** |
||
53 | * @var IOInterface |
||
54 | */ |
||
55 | protected $io; |
||
56 | |||
57 | /** |
||
58 | * @var ProjectConfig |
||
59 | */ |
||
60 | protected $config; |
||
61 | |||
62 | /** |
||
63 | * @var DeployManager |
||
64 | */ |
||
65 | protected $deployManager; |
||
66 | |||
67 | /** |
||
68 | * @var Composer |
||
69 | */ |
||
70 | protected $composer; |
||
71 | |||
72 | /** |
||
73 | * @var Filesystem |
||
74 | */ |
||
75 | protected $filesystem; |
||
76 | |||
77 | /** |
||
78 | * @var EntryFactory |
||
79 | */ |
||
80 | protected $entryFactory; |
||
81 | |||
82 | /** |
||
83 | * @var EventManager |
||
84 | */ |
||
85 | private $eventManager; |
||
86 | |||
87 | /** |
||
88 | * @var ModuleManager |
||
89 | */ |
||
90 | private $moduleManager; |
||
91 | |||
92 | /** |
||
93 | * init the DeployManager |
||
94 | * |
||
95 | * @param Composer $composer |
||
96 | * @param IOInterface $io |
||
97 | */ |
||
98 | 6 | protected function initDeployManager(Composer $composer, IOInterface $io, EventManager $eventManager) |
|
105 | |||
106 | 6 | protected function applyEvents(EventManager $eventManager) |
|
124 | |||
125 | /** |
||
126 | * get Sort Priority from extra Config |
||
127 | * |
||
128 | * @param \Composer\Composer $composer |
||
129 | * |
||
130 | * @return array |
||
131 | */ |
||
132 | 6 | private function getSortPriority(Composer $composer) |
|
140 | |||
141 | /** |
||
142 | * Apply plugin modifications to composer |
||
143 | * |
||
144 | * @param Composer $composer |
||
145 | * @param IOInterface $io |
||
146 | */ |
||
147 | 6 | public function activate(Composer $composer, IOInterface $io) |
|
168 | |||
169 | /** |
||
170 | * Returns an array of event names this subscriber wants to listen to. |
||
171 | * |
||
172 | * The array keys are event names and the value can be: |
||
173 | * |
||
174 | * * The method name to call (priority defaults to 0) |
||
175 | * * An array composed of the method name to call and the priority |
||
176 | * * An array of arrays composed of the method names to call and respective |
||
177 | * priorities, or 0 if unset |
||
178 | * |
||
179 | * For instance: |
||
180 | * |
||
181 | * * array('eventName' => 'methodName') |
||
182 | * * array('eventName' => array('methodName', $priority)) |
||
183 | * * array('eventName' => array(array('methodName1', $priority), array('methodName2')) |
||
184 | * |
||
185 | * @return array The event names to listen to |
||
186 | */ |
||
187 | public static function getSubscribedEvents() |
||
198 | |||
199 | /** |
||
200 | * event listener is named this way, as it listens for events leading to changed code files |
||
201 | * |
||
202 | * @param Event $event |
||
203 | */ |
||
204 | 3 | public function onNewCodeEvent(Event $event) |
|
205 | { |
||
206 | |||
207 | 3 | $packageTypeToMatch = static::PACKAGE_TYPE; |
|
208 | 3 | $magentoModules = array_filter( |
|
209 | 3 | $this->composer->getRepositoryManager()->getLocalRepository()->getPackages(), |
|
210 | 3 | function (PackageInterface $package) use ($packageTypeToMatch) { |
|
211 | 3 | if ($package instanceof AliasPackage) { |
|
212 | 1 | return false; |
|
213 | } |
||
214 | 3 | return $package->getType() === $packageTypeToMatch; |
|
215 | 3 | } |
|
216 | ); |
||
217 | |||
218 | 3 | if ($this->composer->getPackage()->getType() === static::PACKAGE_TYPE |
|
219 | 3 | && $this->config->getIncludeRootPackage() === true |
|
220 | ) { |
||
221 | 1 | $magentoModules[] = $this->composer->getPackage(); |
|
222 | } |
||
223 | |||
224 | 3 | $vendorDir = rtrim($this->composer->getConfig()->get(self::VENDOR_DIR_KEY), '/'); |
|
225 | |||
226 | 3 | Helper::initMagentoRootDir( |
|
227 | 3 | $this->config, |
|
228 | 3 | $this->io, |
|
229 | 3 | $this->filesystem, |
|
230 | $vendorDir |
||
231 | ); |
||
232 | |||
233 | 3 | $this->applyEvents($this->getEventManager()); |
|
234 | |||
235 | 3 | if (in_array('--redeploy', $event->getArguments())) { |
|
236 | $this->writeDebug('remove all deployed modules'); |
||
237 | $this->getModuleManager()->updateInstalledPackages(array()); |
||
238 | } |
||
239 | 3 | $this->writeDebug('start magento module deploy via moduleManager'); |
|
240 | 3 | $this->getModuleManager()->updateInstalledPackages($magentoModules); |
|
241 | 3 | $this->deployLibraries(); |
|
242 | |||
243 | 3 | $patcher = Bootstrap::fromConfig($this->config); |
|
244 | 3 | $patcher->setIo($this->io); |
|
245 | try { |
||
246 | 3 | $patcher->patch(); |
|
247 | 3 | } catch (\DomainException $e) { |
|
248 | 3 | $this->io->write('<comment>'.$e->getMessage().'</comment>'); |
|
249 | } |
||
250 | 3 | } |
|
251 | |||
252 | /** |
||
253 | * test configured repositories and give message about adding recommended ones |
||
254 | */ |
||
255 | 5 | protected function suggestComposerRepositories() |
|
256 | { |
||
257 | 5 | $foundFiregento = false; |
|
258 | 5 | $foundMagento = false; |
|
259 | |||
260 | 5 | foreach ($this->config->getComposerRepositories() as $repository) { |
|
261 | if (!isset($repository["type"]) || $repository["type"] !== "composer") { |
||
262 | continue; |
||
263 | } |
||
264 | if (strpos($repository["url"], "packages.firegento.com") !== false) { |
||
265 | $foundFiregento = true; |
||
266 | } |
||
267 | if (strpos($repository["url"], "packages.magento.com") !== false) { |
||
268 | $foundMagento = true; |
||
269 | } |
||
270 | }; |
||
271 | 5 | $message1 = "<comment>you may want to add the %s repository to composer.</comment>"; |
|
272 | 5 | $message2 = "<comment>add it with:</comment> composer.phar config -g repositories.%s composer %s"; |
|
273 | 5 | if (!$foundFiregento) { |
|
274 | 5 | $this->io->write(sprintf($message1, 'packages.firegento.com')); |
|
275 | 5 | $this->io->write(sprintf($message2, 'firegento', 'https://packages.firegento.com')); |
|
276 | } |
||
277 | 5 | if (!$foundMagento) { |
|
278 | 5 | $this->io->write(sprintf($message1, 'packages.magento.com')); |
|
279 | 5 | $this->io->write(sprintf($message2, 'magento', 'https://packages.magento.com')); |
|
280 | } |
||
281 | |||
282 | 5 | } |
|
283 | |||
284 | /** |
||
285 | * deploy Libraries |
||
286 | */ |
||
287 | 3 | protected function deployLibraries() |
|
288 | { |
||
289 | 3 | $packages = $this->composer->getRepositoryManager()->getLocalRepository()->getPackages(); |
|
290 | 3 | $autoloadDirectories = array(); |
|
291 | |||
292 | 3 | $libraryPath = $this->config->getLibraryPath(); |
|
293 | |||
294 | 3 | if ($libraryPath === null) { |
|
295 | 3 | $this->writeDebug('jump over deployLibraries as no Magento libraryPath is set'); |
|
296 | |||
297 | 3 | return; |
|
298 | } |
||
299 | |||
300 | $vendorDir = rtrim($this->composer->getConfig()->get(self::VENDOR_DIR_KEY), '/'); |
||
301 | |||
302 | $this->filesystem->removeDirectory($libraryPath); |
||
303 | $this->filesystem->ensureDirectoryExists($libraryPath); |
||
304 | |||
305 | foreach ($packages as $package) { |
||
306 | /** @var PackageInterface $package */ |
||
307 | $packageConfig = $this->config->getLibraryConfigByPackagename($package->getName()); |
||
308 | if ($packageConfig === null) { |
||
309 | continue; |
||
310 | } |
||
311 | if (!isset($packageConfig['autoload'])) { |
||
312 | $packageConfig['autoload'] = array('/'); |
||
313 | } |
||
314 | foreach ($packageConfig['autoload'] as $path) { |
||
315 | $autoloadDirectories[] = $libraryPath . '/' . $package->getName() . "/" . $path; |
||
316 | } |
||
317 | $this->writeDebug(sprintf('Magento deployLibraries executed for %s', $package->getName())); |
||
318 | |||
319 | $libraryTargetPath = $libraryPath . '/' . $package->getName(); |
||
320 | $this->filesystem->removeDirectory($libraryTargetPath); |
||
321 | $this->filesystem->ensureDirectoryExists($libraryTargetPath); |
||
322 | $this->copyRecursive($vendorDir . '/' . $package->getPrettyName(), $libraryTargetPath); |
||
323 | } |
||
324 | |||
325 | if (false !== ($executable = $this->getTheseerAutoloadExecutable())) { |
||
326 | $this->writeDebug('Magento deployLibraries executes autoload generator'); |
||
327 | |||
328 | $params = $this->getTheseerAutoloadParams($libraryPath, $autoloadDirectories); |
||
329 | |||
330 | $process = new Process($executable . $params); |
||
331 | $process->run(); |
||
332 | } |
||
333 | } |
||
334 | |||
335 | /** |
||
336 | * return the autoload generator binary path or false if not found |
||
337 | * |
||
338 | * @return bool|string |
||
339 | */ |
||
340 | protected function getTheseerAutoloadExecutable() |
||
341 | { |
||
342 | $executable = $this->composer->getConfig()->get(self::BIN_DIR_KEY) |
||
343 | . self::THESEER_AUTOLOAD_EXEC_BIN_PATH; |
||
344 | |||
345 | if (!file_exists($executable)) { |
||
346 | $executable = $this->composer->getConfig()->get(self::VENDOR_DIR_KEY) |
||
347 | . self::THESEER_AUTOLOAD_EXEC_REL_PATH; |
||
348 | } |
||
349 | |||
350 | if (!file_exists($executable)) { |
||
351 | $this->writeDebug( |
||
352 | 'Magento deployLibraries autoload generator not available, you should require "theseer/autoload"', |
||
353 | $executable |
||
354 | ); |
||
355 | |||
356 | return false; |
||
357 | } |
||
358 | |||
359 | return $executable; |
||
360 | } |
||
361 | |||
362 | /** |
||
363 | * get Theseer Autoload Generator Params |
||
364 | * |
||
365 | * @param string $libraryPath |
||
366 | * @param array $autoloadDirectories |
||
367 | * |
||
368 | * @return string |
||
369 | */ |
||
370 | protected function getTheseerAutoloadParams($libraryPath, $autoloadDirectories) |
||
375 | |||
376 | /** |
||
377 | * Copy then delete is a non-atomic version of {@link rename}. |
||
378 | * |
||
379 | * Some systems can't rename and also don't have proc_open, |
||
380 | * which requires this solution. |
||
381 | * |
||
382 | * copied from \Composer\Util\Filesystem::copyThenRemove and removed the remove part |
||
383 | * |
||
384 | * @param string $source |
||
385 | * @param string $target |
||
386 | */ |
||
387 | protected function copyRecursive($source, $target) |
||
388 | { |
||
389 | $it = new RecursiveDirectoryIterator($source, RecursiveDirectoryIterator::SKIP_DOTS); |
||
390 | $ri = new RecursiveIteratorIterator($it, RecursiveIteratorIterator::SELF_FIRST); |
||
391 | $this->filesystem->ensureDirectoryExists($target); |
||
392 | |||
393 | foreach ($ri as $file) { |
||
394 | $targetPath = $target . DIRECTORY_SEPARATOR . $ri->getSubPathName(); |
||
395 | if ($file->isDir()) { |
||
396 | $this->filesystem->ensureDirectoryExists($targetPath); |
||
397 | } else { |
||
398 | copy($file->getPathname(), $targetPath); |
||
399 | } |
||
400 | } |
||
401 | } |
||
402 | |||
403 | /** |
||
404 | * print Debug Message |
||
405 | * |
||
406 | * @param $message |
||
407 | */ |
||
408 | 6 | private function writeDebug($message, $varDump = null) |
|
409 | { |
||
410 | 6 | if ($this->io->isDebug()) { |
|
411 | 1 | $this->io->write($message); |
|
412 | |||
413 | 1 | if (!is_null($varDump)) { |
|
414 | var_dump($varDump); |
||
415 | } |
||
416 | } |
||
417 | 6 | } |
|
418 | |||
419 | /** |
||
420 | * @param PackageInterface $package |
||
421 | * @return string |
||
422 | */ |
||
423 | public function getPackageInstallPath(PackageInterface $package) |
||
428 | |||
429 | /** |
||
430 | * @return EventManager |
||
431 | */ |
||
432 | protected function getEventManager() |
||
433 | { |
||
434 | if (null === $this->eventManager) { |
||
440 | |||
441 | /** |
||
442 | * @return ModuleManager |
||
443 | */ |
||
444 | protected function getModuleManager() |
||
461 | } |
||
462 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.