From 8fc19a12385afdbcea0441b7344563ff9a0eef3a Mon Sep 17 00:00:00 2001 From: jrfnl Date: Fri, 4 Feb 2022 13:23:16 +0100 Subject: [PATCH] Plugin: only search root package when it has the `phpcodesniffer-standard` type (bug fix) In the "olden days" of PHPCS 1.x, it was customary for projects to call their custom project ruleset `ruleset.xml` instead of `[.]phpcs.xml[.dist]`. This could lead to a root package with such an _old-style_ PHPCS ruleset being registered as if it were a proper PHPCS external standard. This was previously reported in issue 32. The fix I'm proposing now, applies the same rules as for `vendor` installed standards to the root package, i.e.: * Must have the `phpcodesniffer-standard` type set in the `composer.json` file AND * Must have a `ruleset.xml` file. Root packages which do not comply with _both_ these rules will no longer be considered for registration with PHPCS. This should also make the plugin slightly faster for those packages which do not have external standards, but do have the plugin installed. Includes removing some dead code (condition which could never be `true`) which was loosely related to this. Fixes 32 --- src/Plugin.php | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/src/Plugin.php b/src/Plugin.php index 1cb5af0f..c7a734f0 100644 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -444,9 +444,17 @@ private function cleanInstalledPaths() */ private function updateInstalledPaths() { - $changes = false; + $changes = false; + $searchPaths = array(); + + // Add root package only if it has the expected package type. + if ( + $this->composer->getPackage() instanceof RootPackageInterface + && $this->composer->getPackage()->getType() === self::PACKAGE_TYPE + ) { + $searchPaths[] = $this->cwd; + } - $searchPaths = array($this->cwd); $codingStandardPackages = $this->getPHPCodingStandardPackages(); foreach ($codingStandardPackages as $package) { $installPath = $this->composer->getInstallationManager()->getInstallPath($package); @@ -458,6 +466,11 @@ private function updateInstalledPaths() $searchPaths[] = $installPath; } + // Nothing to do. + if ($searchPaths === array()) { + return false; + } + $finder = new Finder(); $finder->files() ->depth('<= ' . $this->getMaxDepth()) @@ -499,9 +512,6 @@ private function updateInstalledPaths() * Iterates through Composers' local repository looking for valid Coding * Standard packages. * - * If the package is the RootPackage (the one the plugin is installed into), - * the package is ignored for now since it needs a different install path logic. - * * @return array Composer packages containing coding standard(s) */ private function getPHPCodingStandardPackages() @@ -516,13 +526,6 @@ function (PackageInterface $package) { } ); - if ( - ! $this->composer->getPackage() instanceof RootPackageInterface - && $this->composer->getPackage()->getType() === self::PACKAGE_TYPE - ) { - $codingStandardPackages[] = $this->composer->getPackage(); - } - return $codingStandardPackages; }