Skip to content

Commit

Permalink
Plugin: add new getPhpcsCommand() method (bug fix)
Browse files Browse the repository at this point in the history
PR 80 in response to bug 79 fixed the issue of the command to set the installed paths failing when the system default PHP version was being used instead of the PHP version used by Composer.

However, PHPCS is also called for the `--config-show` command to retrieve the originally set paths as well as to verify a successful save.

This `--config-show` command was not adjusted at the time and was still being run using the system default PHP version, which could lead to valid `installed_paths` which were present before the installation of the plugin being removed, as `--config-show` would silently fail, leading to the initial `$this->installedPaths` being empty.

This commit fixes that by:
1. Introducing a new `getPhpcsCommand()` method which will cobble together the PHP executable + the PHPCS executable to a runnable command.
2. Using that new method in both places in the plugin were PHPCS is called.

This fixes the bug and improves consistency in the plugin.

It also happens to work as a work-around for upstream by composer/composer 10504 as it removes the call to `$this->composer->getConfig()->get('bin-dir')` which would result in an incorrect execution directory on Windows with Composer 2.2.2+.
  • Loading branch information
jrfnl committed Mar 5, 2022
1 parent 11dd0c9 commit c01bd6f
Showing 1 changed file with 30 additions and 17 deletions.
47 changes: 30 additions & 17 deletions src/Plugin.php
Expand Up @@ -234,9 +234,9 @@ private function loadInstalledPaths()
{
if ($this->isPHPCodeSnifferInstalled() === true) {
$this->processExecutor->execute(
'phpcs --config-show',
$this->getPhpcsCommand() . ' --config-show',
$output,
$this->composer->getConfig()->get('bin-dir')
$this->getPHPCodeSnifferInstallPath()
);

$regex = sprintf(self::PHPCS_CONFIG_REGEX, self::PHPCS_CONFIG_KEY);
Expand Down Expand Up @@ -287,27 +287,16 @@ private function saveInstalledPaths()
self::PHPCS_CONFIG_KEY
);

// Determine the path to the main PHPCS file.
$phpcsPath = $this->getPHPCodeSnifferInstallPath();
if (file_exists($phpcsPath . '/bin/phpcs') === true) {
// PHPCS 3.x.
$phpcsExecutable = './bin/phpcs';
} else {
// PHPCS 2.x.
$phpcsExecutable = './scripts/phpcs';
}

// Okay, lets rock!
$command = vsprintf(
'%s %s %s',
'%s %s',
array(
'php executable' => $this->getPhpExecCommand(),
'phpcs executable' => $phpcsExecutable,
'arguments' => implode(' ', $arguments),
'phpcs command' => $this->getPhpcsCommand(),
'arguments' => implode(' ', $arguments),
)
);

$exitCode = $this->processExecutor->execute($command, $configResult, $phpcsPath);
$exitCode = $this->processExecutor->execute($command, $configResult, $this->getPHPCodeSnifferInstallPath());
if ($exitCode === 0) {
$exitCode = $this->verifySaveSuccess();
}
Expand Down Expand Up @@ -359,6 +348,30 @@ private function verifySaveSuccess()
return $exitCode;
}

/**
* Get the command to call PHPCS.
*/
protected function getPhpcsCommand()
{
// Determine the path to the main PHPCS file.
$phpcsPath = $this->getPHPCodeSnifferInstallPath();
if (file_exists($phpcsPath . '/bin/phpcs') === true) {
// PHPCS 3.x.
$phpcsExecutable = './bin/phpcs';
} else {
// PHPCS 2.x.
$phpcsExecutable = './scripts/phpcs';
}

return vsprintf(
'%s %s',
array(
'php executable' => $this->getPhpExecCommand(),
'phpcs executable' => $phpcsExecutable,
)
);
}

/**
* Get the path to the current PHP version being used.
*
Expand Down

0 comments on commit c01bd6f

Please sign in to comment.