Skip to content

Commit

Permalink
Minor code cleanup for Plugin.php
Browse files Browse the repository at this point in the history
- Moves command outside of exec call, for readability
- Moves error message composition to begin of method, to keep it together with other preparations and keep `if/else` statement cleaner
- Adds whitespace to separate actions that do not belong together or for readability
- Removes redundant variable concat
  • Loading branch information
Potherca committed Sep 15, 2019
1 parent 57dd6ff commit b022a78
Showing 1 changed file with 28 additions and 17 deletions.
45 changes: 28 additions & 17 deletions src/Plugin.php
Expand Up @@ -239,23 +239,27 @@ private function saveInstalledPaths()
);
}

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

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

$exitCode = $this->processExecutor->execute(
sprintf(
'%s ./bin/phpcs %s',
$this->getPhpExecCommand(),
implode(' ', $arguments)
),
$command,
$configResult,
$this->getPHPCodeSnifferInstallPath()
);

if ($exitCode === 0) {
$this->io->write($configMessage);
} else {
$failMessage = sprintf(
'Failed to set PHP CodeSniffer <info>%s</info> Config',
self::PHPCS_CONFIG_KEY
);
$this->io->write($failMessage);
}

Expand All @@ -272,18 +276,25 @@ private function saveInstalledPaths()
protected function getPhpExecCommand()
{
$finder = new PhpExecutableFinder();

$phpPath = $finder->find(false);
if (!$phpPath) {

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);
$command .= $phpArgs;
$command .= ' -d allow_url_fopen=' . ProcessExecutor::escape(ini_get('allow_url_fopen'));
$command .= ' -d disable_functions=' . ProcessExecutor::escape(ini_get('disable_functions'));
$command .= ' -d memory_limit=' . ProcessExecutor::escape(ini_get('memory_limit'));
$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;
}
Expand Down

0 comments on commit b022a78

Please sign in to comment.