Skip to content

Commit

Permalink
Plugin: only search root package when it has the `phpcodesniffer-stan…
Browse files Browse the repository at this point in the history
…dard` 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
  • Loading branch information
jrfnl committed May 25, 2022
1 parent 1489d2f commit 93bee80
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions src/Plugin.php
Expand Up @@ -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);
Expand All @@ -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())
Expand Down Expand Up @@ -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()
Expand All @@ -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;
}

Expand Down

0 comments on commit 93bee80

Please sign in to comment.