Skip to content

Commit

Permalink
Adds the ability to set the max depth from the composer.json file
Browse files Browse the repository at this point in the history
Fixes #45
  • Loading branch information
Potherca committed Feb 1, 2018
1 parent 2e41850 commit ec63bcd
Showing 1 changed file with 58 additions and 19 deletions.
77 changes: 58 additions & 19 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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;

/**
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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;
}
}

0 comments on commit ec63bcd

Please sign in to comment.