Skip to content

Commit

Permalink
Merge pull request #30 from DealerDirect/feature/full-phpcs3-compatib…
Browse files Browse the repository at this point in the history
…ility

Support for coding standard in the root repository for PHP_CodeSniffer v3.x
  • Loading branch information
frenck committed May 11, 2017
2 parents b3ce18e + ca876b6 commit d9d33d8
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions src/Plugin.php
Expand Up @@ -4,7 +4,7 @@
* This file is part of the Dealerdirect PHP_CodeSniffer Standards
* Composer Installer Plugin package.
*
* @copyright 2016 Dealerdirect B.V.
* @copyright 2016-2017 Dealerdirect B.V.
* @license MIT
*/

Expand All @@ -15,6 +15,7 @@
use Composer\IO\IOInterface;
use Composer\Package\AliasPackage;
use Composer\Package\PackageInterface;
use Composer\Package\RootpackageInterface;
use Composer\Plugin\PluginInterface;
use Composer\Script\Event;
use Composer\Script\ScriptEvents;
Expand Down Expand Up @@ -280,13 +281,23 @@ private function updateInstalledPaths()
$finder->files()
->ignoreUnreadableDirs()
->ignoreVCS(true)
->depth('>= 1')
->depth('< 4')
->name('ruleset.xml')
->in($searchPaths);

// Only version 3.x and higher has support for having coding standard in the root of the directory.
$allowCodingStandardsInRoot = $this->isPHPCodeSnifferInstalled('>= 3.0.0');

if ($allowCodingStandardsInRoot !== true) {
$finder->depth('>= 1');
}

foreach ($finder as $ruleset) {
$standardsPath = dirname($ruleset->getPath());
$standardsPath = $ruleset->getPath();

if ($allowCodingStandardsInRoot === false) {
$standardsPath = dirname($standardsPath);
}

// Use relative paths for local project repositories
if ($this->isRunningGlobally() === false) {
Expand Down Expand Up @@ -323,7 +334,7 @@ function (PackageInterface $package) {
}
);

if (! $this->composer->getPackage() instanceof \Composer\Package\RootpackageInterface
if (! $this->composer->getPackage() instanceof RootpackageInterface
&& $this->composer->getPackage()->getType() === self::PACKAGE_TYPE
) {
$codingStandardPackages[] = $this->composer->getPackage();
Expand All @@ -335,15 +346,17 @@ function (PackageInterface $package) {
/**
* Searches for the installed PHP_CodeSniffer Composer package
*
* @param null|string|\Composer\Semver\Constraint\ConstraintInterface $versionConstraint to match against
*
* @return PackageInterface|null
*/
private function getPHPCodeSnifferPackage()
private function getPHPCodeSnifferPackage($versionConstraint = null)
{
$packages = $this
->composer
->getRepositoryManager()
->getLocalRepository()
->findPackages(self::PACKAGE_NAME);
->findPackages(self::PACKAGE_NAME, $versionConstraint);

return array_shift($packages);
}
Expand All @@ -361,11 +374,13 @@ private function getPHPCodeSnifferInstallPath()
/**
* Simple check if PHP_CodeSniffer is installed.
*
* @param null|string|\Composer\Semver\Constraint\ConstraintInterface $versionConstraint to match against
*
* @return bool Whether PHP_CodeSniffer is installed
*/
private function isPHPCodeSnifferInstalled()
private function isPHPCodeSnifferInstalled($versionConstraint = null)
{
return ($this->getPHPCodeSnifferPackage() !== null);
return ($this->getPHPCodeSnifferPackage($versionConstraint) !== null);
}

/**
Expand Down

0 comments on commit d9d33d8

Please sign in to comment.