From ec63bcd38d550252cbc06f97665a116eff5ac38d Mon Sep 17 00:00:00 2001 From: Ben Peachey Date: Thu, 1 Feb 2018 09:15:37 +0100 Subject: [PATCH] Adds the ability to set the max depth from the composer.json file Fixes #45 --- src/Plugin.php | 77 +++++++++++++++++++++++++++++++++++++------------- 1 file changed, 58 insertions(+), 19 deletions(-) diff --git a/src/Plugin.php b/src/Plugin.php index b12a6ccd..62f59938 100644 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -32,6 +32,8 @@ */ class Plugin implements PluginInterface, EventSubscriberInterface { + const KEY_MAX_DEPTH = 'phpcodesniffer-max-depth'; + const MESSAGE_RUNNING_INSTALLER = 'Running PHPCodeSniffer Composer Installer'; const MESSAGE_NOTHING_TO_INSTALL = 'Nothing to install or update'; const MESSAGE_NOT_INSTALLED = 'PHPCodeSniffer is not installed'; @@ -41,24 +43,13 @@ class Plugin implements PluginInterface, EventSubscriberInterface const PHPCS_CONFIG_KEY = 'installed_paths'; - /** - * @var Composer - */ + /** @var Composer */ private $composer; - - /** - * @var IOInterface - */ + /** @var IOInterface */ private $io; - - /** - * @var array - */ + /** @var array */ private $installedPaths; - - /** - * @var ProcessBuilder - */ + /* @var ProcessBuilder */ private $processBuilder; /** @@ -277,17 +268,19 @@ private function updateInstalledPaths() $searchPaths[] = $this->composer->getInstallationManager()->getInstallPath($package); } + $maxDepth = $this->getMaxDepth(); + $minDepth = $this->getMinDepth(); + $finder = new Finder(); $finder->files() ->ignoreUnreadableDirs() ->ignoreVCS(true) - ->depth('< 4') + ->depth('< ' . $maxDepth) ->name('ruleset.xml') ->in($searchPaths); - // Only version 3.x and higher has support for having coding standard in the root of the directory. - if ($this->isPHPCodeSnifferInstalled('>= 3.0.0') !== true) { - $finder->depth('>= 1'); + if ($minDepth !== 0) { + $finder->depth('>= ' . $minDepth); } // Process each found possible ruleset. @@ -438,4 +431,50 @@ private function getRelativePath($to) } return implode('/', $relPath); } + + /** + * @return string + * @throws \InvalidArgumentException + */ + private function getMaxDepth() + { + $maxDepth = '4'; + + $extra = $this->composer->getPackage()->getExtra(); + + if (array_key_exists(self::KEY_MAX_DEPTH, $extra)) { + $maxDepth = $extra[self::KEY_MAX_DEPTH]; + $minDepth = $this->getMinDepth(); + + if ( + is_int($maxDepth) === false /* Must be an integer */ + || $maxDepth <= $minDepth /* Larger than the minimum */ + || is_float($maxDepth) === true /* Within the boundaries of integer */ + ) { + $message = vsprintf('The value of "%s" (in the composer.json "extra" section) must be an integer larger then %d, %s given.', array( + 'key' => self::KEY_MAX_DEPTH, + 'min' => $minDepth, + 'given' => var_export($maxDepth, true), + )); + + throw new \InvalidArgumentException($message); + } + } + + return $maxDepth; + } + + /** + * @return int + */ + private function getMinDepth() + { + $minDepth = 0; + + if ($this->isPHPCodeSnifferInstalled('>= 3.0.0') !== true) { + $minDepth = 1; + } + + return $minDepth; + } }