Skip to content

Commit 93bee80

Browse files
committed
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
1 parent 1489d2f commit 93bee80

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

src/Plugin.php

+15-12
Original file line numberDiff line numberDiff line change
@@ -444,9 +444,17 @@ private function cleanInstalledPaths()
444444
*/
445445
private function updateInstalledPaths()
446446
{
447-
$changes = false;
447+
$changes = false;
448+
$searchPaths = array();
449+
450+
// Add root package only if it has the expected package type.
451+
if (
452+
$this->composer->getPackage() instanceof RootPackageInterface
453+
&& $this->composer->getPackage()->getType() === self::PACKAGE_TYPE
454+
) {
455+
$searchPaths[] = $this->cwd;
456+
}
448457

449-
$searchPaths = array($this->cwd);
450458
$codingStandardPackages = $this->getPHPCodingStandardPackages();
451459
foreach ($codingStandardPackages as $package) {
452460
$installPath = $this->composer->getInstallationManager()->getInstallPath($package);
@@ -458,6 +466,11 @@ private function updateInstalledPaths()
458466
$searchPaths[] = $installPath;
459467
}
460468

469+
// Nothing to do.
470+
if ($searchPaths === array()) {
471+
return false;
472+
}
473+
461474
$finder = new Finder();
462475
$finder->files()
463476
->depth('<= ' . $this->getMaxDepth())
@@ -499,9 +512,6 @@ private function updateInstalledPaths()
499512
* Iterates through Composers' local repository looking for valid Coding
500513
* Standard packages.
501514
*
502-
* If the package is the RootPackage (the one the plugin is installed into),
503-
* the package is ignored for now since it needs a different install path logic.
504-
*
505515
* @return array Composer packages containing coding standard(s)
506516
*/
507517
private function getPHPCodingStandardPackages()
@@ -516,13 +526,6 @@ function (PackageInterface $package) {
516526
}
517527
);
518528

519-
if (
520-
! $this->composer->getPackage() instanceof RootPackageInterface
521-
&& $this->composer->getPackage()->getType() === self::PACKAGE_TYPE
522-
) {
523-
$codingStandardPackages[] = $this->composer->getPackage();
524-
}
525-
526529
return $codingStandardPackages;
527530
}
528531

0 commit comments

Comments
 (0)