From 3f5fe150dd54558c0d3fde97d5b7ef46a723fd4b Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Wed, 10 May 2017 16:20:18 +0200 Subject: [PATCH 1/2] ADDED: Support for coding standard in the root repository for PHP_CodeSniffer v3.x --- src/Plugin.php | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/src/Plugin.php b/src/Plugin.php index 57cac171..4d155eb4 100644 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -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; @@ -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) { @@ -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(); @@ -335,15 +346,17 @@ function (PackageInterface $package) { /** * Searches for the installed PHP_CodeSniffer Composer package * + * @param 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); } @@ -361,11 +374,13 @@ private function getPHPCodeSnifferInstallPath() /** * Simple check if PHP_CodeSniffer is installed. * + * @param 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); } /** From ca876b644fb5fd7d3956e7d93cba18f15568a5f2 Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Thu, 11 May 2017 09:31:05 +0200 Subject: [PATCH 2/2] Changed code documentation. --- src/Plugin.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Plugin.php b/src/Plugin.php index 4d155eb4..58f8eb02 100644 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -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 */ @@ -346,7 +346,7 @@ function (PackageInterface $package) { /** * Searches for the installed PHP_CodeSniffer Composer package * - * @param string|\Composer\Semver\Constraint\ConstraintInterface $versionConstraint to match against + * @param null|string|\Composer\Semver\Constraint\ConstraintInterface $versionConstraint to match against * * @return PackageInterface|null */ @@ -374,7 +374,7 @@ private function getPHPCodeSnifferInstallPath() /** * Simple check if PHP_CodeSniffer is installed. * - * @param string|\Composer\Semver\Constraint\ConstraintInterface $versionConstraint to match against + * @param null|string|\Composer\Semver\Constraint\ConstraintInterface $versionConstraint to match against * * @return bool Whether PHP_CodeSniffer is installed */