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 99d1bdb
Showing 1 changed file with 60 additions and 4 deletions.
64 changes: 60 additions & 4 deletions src/Plugin.php
Expand Up @@ -32,6 +32,11 @@
*/
class Plugin implements PluginInterface, EventSubscriberInterface
{
const ERROR_WRONG_MAX_DEPTH =
'The value of "%s" (in the composer.json "extra".section) must be an integer larger then %d, %s given.';

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 Down Expand Up @@ -277,17 +282,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 +445,53 @@ 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(
self::ERROR_WRONG_MAX_DEPTH,
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 99d1bdb

Please sign in to comment.