From b022a78ec3393ff0bc148145ecd5356676f6f3e9 Mon Sep 17 00:00:00 2001 From: Ben Peachey Date: Wed, 11 Sep 2019 21:24:27 +0200 Subject: [PATCH] Minor code cleanup for Plugin.php - 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 --- src/Plugin.php | 45 ++++++++++++++++++++++++++++----------------- 1 file changed, 28 insertions(+), 17 deletions(-) diff --git a/src/Plugin.php b/src/Plugin.php index f3de166a..94696290 100644 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -239,12 +239,20 @@ private function saveInstalledPaths() ); } + // Prepare message in case of failure + $failMessage = sprintf( + 'Failed to set PHP CodeSniffer %s 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() ); @@ -252,10 +260,6 @@ private function saveInstalledPaths() if ($exitCode === 0) { $this->io->write($configMessage); } else { - $failMessage = sprintf( - 'Failed to set PHP CodeSniffer %s Config', - self::PHPCS_CONFIG_KEY - ); $this->io->write($failMessage); } @@ -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; }