Skip to content

Commit

Permalink
Merge pull request #80 from jrfnl/feature/79-bugfix
Browse files Browse the repository at this point in the history
Respect PHP version used by Composer and provide better feedback on failure
  • Loading branch information
Potherca committed Dec 16, 2019
2 parents c20ba7d + 22d3cdc commit 2531231
Showing 1 changed file with 63 additions and 8 deletions.
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()
{
$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

0 comments on commit 2531231

Please sign in to comment.