Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Respect PHP version used by Composer and provide better feedback on failure #80

Merged
merged 4 commits into from Dec 16, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
71 changes: 63 additions & 8 deletions src/Plugin.php
Expand Up @@ -25,6 +25,7 @@
use Symfony\Component\Process\Exception\LogicException;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\Exception\RuntimeException;
use Symfony\Component\Process\PhpExecutableFinder;

/**
* PHP_CodeSniffer standard installation manager.
Expand Down Expand Up @@ -238,22 +239,76 @@ private function saveInstalledPaths()
);
}

$this->io->write($configMessage);
// Prepare message in case of failure
$failMessage = sprintf(
'Failed to set PHP CodeSniffer <info>%s</info> Config',
self::PHPCS_CONFIG_KEY
);

$this->processExecutor->execute(
sprintf(
'phpcs %s',
implode(' ', $arguments)
),
$configResult,
$this->composer->getConfig()->get('bin-dir')
// 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',
array(
'php executable' => $this->getPhpExecCommand(),
'phpcs executable' => $phpcsExecutable,
'arguments' => implode(' ', $arguments)
)
);

$exitCode = $this->processExecutor->execute($command, $configResult, $phpcsPath);

if ($exitCode === 0) {
$this->io->write($configMessage);
} else {
$this->io->write($failMessage);
}

if ($this->io->isVerbose() && !empty($configResult)) {
$this->io->write(sprintf('<info>%s</info>', $configResult));
}
}

/**
* Get the path to the current PHP version being used.
*
* Duplicate of the same in the EventDispatcher class in Composer itself.
*/
protected function getPhpExecCommand()
Potherca marked this conversation as resolved.
Show resolved Hide resolved
{
$finder = new PhpExecutableFinder();

$phpPath = $finder->find(false);

if ($phpPath === false) {
throw new \RuntimeException('Failed to locate PHP binary to execute ' . $phpPath);
}

$phpArgs = $finder->findArguments();
$phpArgs = $phpArgs
? ' ' . implode(' ', $phpArgs)
: ''
;

$command = ProcessExecutor::escape($phpPath) .
$phpArgs .
' -d allow_url_fopen=' . ProcessExecutor::escape(ini_get('allow_url_fopen')) .
' -d disable_functions=' . ProcessExecutor::escape(ini_get('disable_functions')) .
' -d memory_limit=' . ProcessExecutor::escape(ini_get('memory_limit'))
;

return $command;
}

/**
* Iterate trough all known paths and check if they are still valid.
*
Expand Down