diff --git a/docs/en/configuring_project.md b/docs/en/configuring_project.md index 2a291d0e..6760b8ff 100644 --- a/docs/en/configuring_project.md +++ b/docs/en/configuring_project.md @@ -156,6 +156,8 @@ branch-dev: Section `build_settings` contents common build settings: +* Option `php` set php executable path (For example: `php: php7.4` or `php: /usr/local/bin/php`. Default value: `php: php`). + * Option `verbose` enable/disable verbosity of plugins output (Default value: `verbose: true`). * Option `clone_depth: N` allows cloning repository with partial history (Git clone option `--depth=N`). Option diff --git a/src/Builder.php b/src/Builder.php index 075b7809..e29ff4f7 100644 --- a/src/Builder.php +++ b/src/Builder.php @@ -31,6 +31,10 @@ */ class Builder { + const PHP_CLI_TAG = '%PHP%'; + + protected string $phpExecutable = 'php'; + public string $buildPath = ''; /** @@ -144,6 +148,11 @@ public function setConfig(array $config): void $this->config = $config; } + public function setPhpExecutable(string $phpExecutable): void + { + $this->phpExecutable = $phpExecutable; + } + /** * Access a variable from the .php-censor.yml file. * @@ -303,6 +312,10 @@ protected function setTestCoverageTrend(): void */ public function executeCommand(...$params): bool { + if (!empty($params[0])) { + $params[0] = \str_replace(self::PHP_CLI_TAG, $this->phpExecutable, $params[0]); + } + return $this->commandExecutor->executeCommand($params); } diff --git a/src/Model/Build.php b/src/Model/Build.php index 145ae8c5..0d4b2166 100644 --- a/src/Model/Build.php +++ b/src/Model/Build.php @@ -206,6 +206,9 @@ public function handleConfigBeforeClone(Builder $builder) $builder->logDebug('Config before repository clone (DB): ' . \json_encode($buildConfig)); $builder->setConfig($buildConfig); + if (!empty($buildConfig['build_settings']['php'])) { + $builder->setPhpExecutable($buildConfig['build_settings']['php']); + } } } @@ -258,6 +261,9 @@ protected function handleConfig(Builder $builder, string $buildPath): bool $builder->logDebug('Final config: ' . \json_encode($buildConfig)); $builder->setConfig($buildConfig); + if (!empty($buildConfig['build_settings']['php'])) { + $builder->setPhpExecutable($buildConfig['build_settings']['php']); + } } return true; diff --git a/src/Plugin/Atoum.php b/src/Plugin/Atoum.php index 571ea265..d330674c 100644 --- a/src/Plugin/Atoum.php +++ b/src/Plugin/Atoum.php @@ -59,7 +59,7 @@ public function __construct(Builder $builder, Build $build, array $options = []) */ public function execute() { - $cmd = $this->executable; + $cmd = Builder::PHP_CLI_TAG . ' ' . $this->executable; if (null !== $this->args) { $cmd .= " {$this->args}"; diff --git a/src/Plugin/Behat.php b/src/Plugin/Behat.php index bfff90a4..07007175 100644 --- a/src/Plugin/Behat.php +++ b/src/Plugin/Behat.php @@ -56,7 +56,7 @@ public function execute() return false; } - $success = $this->builder->executeCommand($this->executable . ' %s', $this->features); + $success = $this->builder->executeCommand(Builder::PHP_CLI_TAG . ' ' . $this->executable . ' %s', $this->features); list($errorCount, $data) = $this->parseBehatOutput(); diff --git a/src/Plugin/Codeception.php b/src/Plugin/Codeception.php index 8af65569..9672ceaa 100644 --- a/src/Plugin/Codeception.php +++ b/src/Plugin/Codeception.php @@ -132,7 +132,7 @@ protected function runConfigFile() return false; } - $cmd = 'cd "%s" && ' . $codeception . ' run -c "%s" ' . $this->args . ' --xml'; + $cmd = 'cd "%s" && ' . Builder::PHP_CLI_TAG . ' ' . $codeception . ' run -c "%s" ' . $this->args . ' --xml'; $success = $this->builder->executeCommand($cmd, $this->directory, $this->ymlConfigFile); if (!$success) { diff --git a/src/Plugin/Composer.php b/src/Plugin/Composer.php index 97852ece..a1bd4b24 100644 --- a/src/Plugin/Composer.php +++ b/src/Plugin/Composer.php @@ -85,7 +85,7 @@ public function execute() { $composerLocation = $this->executable; - $cmd = $composerLocation . ' --no-ansi --no-interaction '; + $cmd = Builder::PHP_CLI_TAG . ' ' . $composerLocation . ' --no-ansi --no-interaction '; if ($this->preferDist) { $this->builder->log('Using --prefer-dist flag'); diff --git a/src/Plugin/DeployerOrg.php b/src/Plugin/DeployerOrg.php index c8e3b4b9..9b761503 100644 --- a/src/Plugin/DeployerOrg.php +++ b/src/Plugin/DeployerOrg.php @@ -53,7 +53,7 @@ public function execute() $branchConfig = $this->options[$this->branch]; $options = $this->getOptions($branchConfig); - $deployerCmd = "$this->executable $options"; + $deployerCmd = Builder::PHP_CLI_TAG . " $this->executable $options"; return $this->builder->executeCommand($deployerCmd); } diff --git a/src/Plugin/Lint.php b/src/Plugin/Lint.php index 9a7eb92b..7663391f 100644 --- a/src/Plugin/Lint.php +++ b/src/Plugin/Lint.php @@ -3,7 +3,6 @@ namespace PHPCensor\Plugin; use DirectoryIterator; -use PHPCensor; use PHPCensor\Builder; use PHPCensor\Model\Build; use PHPCensor\Plugin; @@ -66,10 +65,8 @@ public function execute() { $success = true; - $php = $this->findBinary('php'); - foreach ($this->directories as $dir) { - if (!$this->lintDirectory($php, $dir)) { + if (!$this->lintDirectory($dir)) { $success = false; } } @@ -81,15 +78,15 @@ public function execute() * Lint an item (file or directory) by calling the appropriate method. * @return bool */ - protected function lintItem($php, $item, $itemPath) + protected function lintItem($item, $itemPath) { $success = true; - if ($item->isFile() && $item->getExtension() === 'php' && !$this->lintFile($php, $itemPath)) { + if ($item->isFile() && $item->getExtension() === 'php' && !$this->lintFile($itemPath)) { $success = false; } elseif ($item->isDir() && $this->recursive && - !$this->lintDirectory($php, ($itemPath . '/'))) { + !$this->lintDirectory($itemPath . '/')) { $success = false; } @@ -100,7 +97,7 @@ protected function lintItem($php, $item, $itemPath) * Run php -l against a directory of files. * @return bool */ - protected function lintDirectory($php, $path) + protected function lintDirectory($path) { $success = true; $directory = new DirectoryIterator($this->builder->buildPath . $path); @@ -116,7 +113,7 @@ protected function lintDirectory($php, $path) continue; } - if (!$this->lintItem($php, $item, $itemPath)) { + if (!$this->lintItem($item, $itemPath)) { $success = false; } } @@ -128,11 +125,11 @@ protected function lintDirectory($php, $path) * Run php -l against a specific file. * @return bool */ - protected function lintFile($php, $path) + protected function lintFile($path) { $success = true; - if (!$this->builder->executeCommand($php . ' -l "%s"', $this->builder->buildPath . $path)) { + if (!$this->builder->executeCommand(Builder::PHP_CLI_TAG . ' -l "%s"', $this->builder->buildPath . $path)) { $this->builder->logFailure($path); $success = false; } diff --git a/src/Plugin/Mage.php b/src/Plugin/Mage.php index c276703b..dc4ebb7a 100644 --- a/src/Plugin/Mage.php +++ b/src/Plugin/Mage.php @@ -53,7 +53,7 @@ public function execute() return false; } - $result = $this->builder->executeCommand($this->executable . ' deploy to:' . $this->mageEnv); + $result = $this->builder->executeCommand(Builder::PHP_CLI_TAG . ' ' . $this->executable . ' deploy to:' . $this->mageEnv); try { $this->builder->log('########## MAGE LOG BEGIN ##########'); diff --git a/src/Plugin/Mage3.php b/src/Plugin/Mage3.php index c60ab9f4..da047003 100644 --- a/src/Plugin/Mage3.php +++ b/src/Plugin/Mage3.php @@ -58,7 +58,7 @@ public function execute() return false; } - $result = $this->builder->executeCommand($this->executable . ' -n deploy ' . $this->mageEnv); + $result = $this->builder->executeCommand(Builder::PHP_CLI_TAG . ' ' . $this->executable . ' -n deploy ' . $this->mageEnv); try { $this->builder->log('########## MAGE LOG BEGIN ##########'); diff --git a/src/Plugin/Pahout.php b/src/Plugin/Pahout.php index a997ff78..083bda0e 100644 --- a/src/Plugin/Pahout.php +++ b/src/Plugin/Pahout.php @@ -62,7 +62,7 @@ public function execute() } $this->builder->executeCommand( - 'cd "%s" && ' . $pahout . ' %s --format=json', + 'cd "%s" && ' . Builder::PHP_CLI_TAG . ' ' . $pahout . ' %s --format=json', $this->builder->buildPath, $this->directory ); diff --git a/src/Plugin/Pdepend.php b/src/Plugin/Pdepend.php index 74964c0b..f00ea6b5 100644 --- a/src/Plugin/Pdepend.php +++ b/src/Plugin/Pdepend.php @@ -104,7 +104,7 @@ public function execute() } $pdepend = $this->executable; - $cmd = 'cd "%s" && ' . $pdepend . ' --summary-xml="%s" --jdepend-chart="%s" --overview-pyramid="%s" %s "%s"'; + $cmd = 'cd "%s" && ' . Builder::PHP_CLI_TAG . ' ' . $pdepend . ' --summary-xml="%s" --jdepend-chart="%s" --overview-pyramid="%s" %s "%s"'; $ignore = ''; if (\count($this->ignore)) { diff --git a/src/Plugin/Phan.php b/src/Plugin/Phan.php index f3eeefe9..5cb898bc 100644 --- a/src/Plugin/Phan.php +++ b/src/Plugin/Phan.php @@ -78,7 +78,7 @@ public function execute() $phan = $this->findBinary(['phan', 'phan.phar']); // Launch Phan on PHP files with json output - $cmd = $phan.' -f %s -i -m json -o %s'; + $cmd = Builder::PHP_CLI_TAG . ' ' . $phan . ' -f %s -i -m json -o %s'; $this->builder->executeCommand($cmd, $this->location . '/phan.in', $this->location . '/phan.out'); diff --git a/src/Plugin/Phing.php b/src/Plugin/Phing.php index 231dadbe..6aea3b5f 100644 --- a/src/Plugin/Phing.php +++ b/src/Plugin/Phing.php @@ -68,7 +68,7 @@ public function execute() { $phingExecutable = $this->executable; - $cmd[] = $phingExecutable . ' -f ' . $this->getBuildFilePath(); + $cmd[] = Builder::PHP_CLI_TAG . ' ' . $phingExecutable . ' -f ' . $this->getBuildFilePath(); if ($this->getPropertyFile()) { $cmd[] = '-propertyfile ' . $this->getPropertyFile(); diff --git a/src/Plugin/Phlint.php b/src/Plugin/Phlint.php index 91501e8a..14d6c340 100644 --- a/src/Plugin/Phlint.php +++ b/src/Plugin/Phlint.php @@ -47,7 +47,7 @@ public function execute() $this->builder->executeCommand( 'cd "%s" && %s analyze --no-interaction --no-ansi', $this->builder->buildPath, - $this->executable + Builder::PHP_CLI_TAG . ' ' . $this->executable ); // Define that the plugin succeed diff --git a/src/Plugin/PhpCodeSniffer.php b/src/Plugin/PhpCodeSniffer.php index 082e221c..e9e1897b 100644 --- a/src/Plugin/PhpCodeSniffer.php +++ b/src/Plugin/PhpCodeSniffer.php @@ -151,7 +151,7 @@ public function execute() $this->builder->logExecOutput(false); } - $cmd = 'cd "%s" && ' . $phpcs . ' --report=json %s %s %s %s %s "%s" %s %s %s'; + $cmd = 'cd "%s" && ' . Builder::PHP_CLI_TAG . ' ' . $phpcs . ' --report=json %s %s %s %s %s "%s" %s %s %s'; $this->builder->executeCommand( $cmd, $this->builder->buildPath, diff --git a/src/Plugin/PhpCpd.php b/src/Plugin/PhpCpd.php index 8ea65208..de984416 100644 --- a/src/Plugin/PhpCpd.php +++ b/src/Plugin/PhpCpd.php @@ -78,16 +78,22 @@ public function execute() } $phpcpd = $this->executable; - $lastLine = \exec( - \sprintf('cd "%s" && ' . $phpcpd . ' %s "%s" --version', $this->builder->buildPath, $ignore, $this->directory) + $this->builder->executeCommand( + 'cd "%s" && ' . Builder::PHP_CLI_TAG . ' ' . $phpcpd . ' %s "%s" --version', + $this->builder->buildPath, + $ignore, + $this->directory, ); - if (false !== \strpos($lastLine, '--names-exclude')) { + $this->builder->logExecOutput(true); + + $lastOutput = $this->builder->getLastOutput(); + if (false !== \strpos($lastOutput, '--names-exclude')) { $ignore = $ignoreForNewVersion; } $tmpFileName = \tempnam(\sys_get_temp_dir(), (self::pluginName() . '_')); - $cmd = 'cd "%s" && ' . $phpcpd . ' --log-pmd "%s" %s "%s"'; + $cmd = 'cd "%s" && ' . Builder::PHP_CLI_TAG . ' ' . $phpcpd . ' --log-pmd "%s" %s "%s"'; $success = $this->builder->executeCommand($cmd, $this->builder->buildPath, $tmpFileName, $ignore, $this->directory); $errorCount = $this->processReport(\file_get_contents($tmpFileName)); diff --git a/src/Plugin/PhpCsFixer.php b/src/Plugin/PhpCsFixer.php index 36936820..10f5d967 100644 --- a/src/Plugin/PhpCsFixer.php +++ b/src/Plugin/PhpCsFixer.php @@ -96,7 +96,7 @@ public function execute() $phpCsFixer = $this->executable; // Determine the version of PHP CS Fixer - $cmd = $phpCsFixer . ' --version'; + $cmd = Builder::PHP_CLI_TAG . ' ' . $phpCsFixer . ' --version'; $success = $this->builder->executeCommand($cmd); $output = $this->builder->getLastOutput(); $matches = []; @@ -132,7 +132,7 @@ public function execute() } } - $cmd = $phpCsFixer . ' fix ' . $directory . ' %s'; + $cmd = Builder::PHP_CLI_TAG . ' ' . $phpCsFixer . ' fix ' . $directory . ' %s'; $success = $this->builder->executeCommand($cmd, $this->args); $this->builder->logExecOutput(true); diff --git a/src/Plugin/PhpDocblockChecker.php b/src/Plugin/PhpDocblockChecker.php index 516a75d2..f9805be3 100644 --- a/src/Plugin/PhpDocblockChecker.php +++ b/src/Plugin/PhpDocblockChecker.php @@ -110,7 +110,7 @@ public function execute() } // Build command string: - $cmd = $checkerCmd . ' --json --directory="%s"%s%s'; + $cmd = Builder::PHP_CLI_TAG . ' ' . $checkerCmd . ' --json --directory="%s"%s%s'; if (!$this->build->isDebug()) { $this->builder->logExecOutput(false); diff --git a/src/Plugin/PhpLoc.php b/src/Plugin/PhpLoc.php index 0529cc3b..6da188b8 100644 --- a/src/Plugin/PhpLoc.php +++ b/src/Plugin/PhpLoc.php @@ -65,7 +65,7 @@ public function execute() $phploc = $this->executable; - $success = $this->builder->executeCommand('cd "%s" && php -d xdebug.mode=off -d error_reporting=0 ' . $phploc . ' %s %s', $this->builder->buildPath, $ignore, $this->directory); + $success = $this->builder->executeCommand('cd "%s" && ' . Builder::PHP_CLI_TAG . ' -d xdebug.mode=off -d error_reporting=0 ' . Builder::PHP_CLI_TAG . ' ' . $phploc . ' %s %s', $this->builder->buildPath, $ignore, $this->directory); $output = $this->builder->getLastOutput(); if (\preg_match_all('/\((LOC|CLOC|NCLOC|LLOC)\)\s+([0-9]+)/', $output, $matches)) { diff --git a/src/Plugin/PhpMessDetector.php b/src/Plugin/PhpMessDetector.php index afa99af9..a8246ee2 100644 --- a/src/Plugin/PhpMessDetector.php +++ b/src/Plugin/PhpMessDetector.php @@ -173,7 +173,7 @@ protected function tryAndProcessRules() */ protected function executePhpMd($binaryPath) { - $cmd = 'cd "%s" && ' . $binaryPath . ' "%s" xml %s %s %s'; + $cmd = 'cd "%s" && ' . Builder::PHP_CLI_TAG . ' ' . $binaryPath . ' "%s" xml %s %s %s'; $ignore = ''; if (\is_array($this->ignore) && \count($this->ignore) > 0) { diff --git a/src/Plugin/PhpParallelLint.php b/src/Plugin/PhpParallelLint.php index 4ec480d7..24fc81a7 100644 --- a/src/Plugin/PhpParallelLint.php +++ b/src/Plugin/PhpParallelLint.php @@ -84,7 +84,7 @@ public function execute() $phplint = $this->executable; - $cmd = $phplint . ' -e %s' . '%s %s "%s"'; + $cmd = Builder::PHP_CLI_TAG . ' ' . $phplint . ' -e %s' . '%s %s "%s"'; $success = $this->builder->executeCommand( $cmd, $this->extensions, diff --git a/src/Plugin/PhpSpec.php b/src/Plugin/PhpSpec.php index c285dcdb..ddd916ed 100644 --- a/src/Plugin/PhpSpec.php +++ b/src/Plugin/PhpSpec.php @@ -40,7 +40,7 @@ public function execute() { $phpspec = $this->executable; - $success = $this->builder->executeCommand($phpspec . ' --format=junit --no-code-generation run'); + $success = $this->builder->executeCommand(Builder::PHP_CLI_TAG . ' ' . $phpspec . ' --format=junit --no-code-generation run'); $output = $this->builder->getLastOutput(); /* diff --git a/src/Plugin/PhpStan.php b/src/Plugin/PhpStan.php index 270efe20..00866dcc 100644 --- a/src/Plugin/PhpStan.php +++ b/src/Plugin/PhpStan.php @@ -65,7 +65,7 @@ public function execute() } $this->builder->executeCommand( - 'cd "%s" && ' . $phpStan . ' analyze --error-format=json %s', + 'cd "%s" && ' . Builder::PHP_CLI_TAG . ' ' . $phpStan . ' analyze --error-format=json %s', $this->builder->buildPath, \implode(' ', $this->directories) ); diff --git a/src/Plugin/PhpTalLint.php b/src/Plugin/PhpTalLint.php index 61e549fd..ccb6f749 100644 --- a/src/Plugin/PhpTalLint.php +++ b/src/Plugin/PhpTalLint.php @@ -160,7 +160,7 @@ protected function lintFile($path) $lint = __DIR__ . '/'; $lint .= 'vendor/phptal/phptal/'; $lint .= 'tools/phptal_lint.php'; - $cmd = 'php ' . $lint . ' %s "%s"'; + $cmd = Builder::PHP_CLI_TAG . ' ' . $lint . ' %s "%s"'; $this->builder->executeCommand($cmd, $suffixes, $this->builder->buildPath . $path); diff --git a/src/Plugin/PhpUnit.php b/src/Plugin/PhpUnit.php index aa8aad28..da46861a 100644 --- a/src/Plugin/PhpUnit.php +++ b/src/Plugin/PhpUnit.php @@ -115,9 +115,15 @@ public function execute() return false; } - $cmd = $this->executable; - $lastLine = \exec($cmd . ' --log-json . --version'); - if (false !== \strpos($lastLine, '--log-json')) { + $cmd = $this->executable; + + $this->builder->executeCommand( + Builder::PHP_CLI_TAG . ' ' . $cmd . ' --log-json . --version', + ); + $this->builder->logExecOutput(true); + + $lastOutput = $this->builder->getLastOutput(); + if (false !== \strpos($lastOutput, '--log-json')) { $logFormat = 'junit'; // --log-json is not supported } else { $logFormat = 'json'; @@ -190,7 +196,7 @@ protected function runConfig($directory, $configFile, $logFormat) } $arguments = $this->builder->interpolate($options->buildArgumentString(), true); - $cmd = $this->executable . ' %s %s'; + $cmd = Builder::PHP_CLI_TAG . ' ' . $this->executable . ' %s %s'; if ($options->getOption('coverage')) { $cmd = 'XDEBUG_MODE=coverage ' . $cmd; diff --git a/src/Plugin/Psalm.php b/src/Plugin/Psalm.php index 257add7d..d5582f3b 100644 --- a/src/Plugin/Psalm.php +++ b/src/Plugin/Psalm.php @@ -58,7 +58,7 @@ public function execute() $this->builder->logExecOutput(false); } - $this->builder->executeCommand('cd "%s" && ' . $psalm . ' --output-format=json', $this->builder->buildPath); + $this->builder->executeCommand('cd "%s" && ' . Builder::PHP_CLI_TAG . ' ' . $psalm . ' --output-format=json', $this->builder->buildPath); $this->builder->logExecOutput(true); // Define that the plugin succeed diff --git a/src/Plugin/SecurityChecker.php b/src/Plugin/SecurityChecker.php index 53bfeebe..e67d5844 100644 --- a/src/Plugin/SecurityChecker.php +++ b/src/Plugin/SecurityChecker.php @@ -91,10 +91,10 @@ public function execute() } if ('symfony' === $this->binaryType) { - $cmd = '%s check:security --format=json --dir=%s'; + $cmd = Builder::PHP_CLI_TAG . ' %s check:security --format=json --dir=%s'; $executable = $this->findBinary('symfony'); } else { - $cmd = '%s --format=json --path="%s"'; + $cmd = Builder::PHP_CLI_TAG . ' %s --format=json --path="%s"'; $executable = $this->findBinary('local-php-security-checker'); } diff --git a/src/Plugin/SensiolabsInsight.php b/src/Plugin/SensiolabsInsight.php index 4bd7790c..b0e249d2 100644 --- a/src/Plugin/SensiolabsInsight.php +++ b/src/Plugin/SensiolabsInsight.php @@ -137,7 +137,7 @@ protected function processReport($xmlString) */ protected function executeSensiolabsInsight($binaryPath) { - $cmd = $binaryPath . ' -n analyze --reference %s %s --api-token %s --user-uuid %s'; + $cmd = Builder::PHP_CLI_TAG . ' ' . $binaryPath . ' -n analyze --reference %s %s --api-token %s --user-uuid %s'; // Run Sensiolabs Insight: $this->builder->executeCommand( @@ -148,7 +148,7 @@ protected function executeSensiolabsInsight($binaryPath) $this->userUuid ); - $cmd = $binaryPath . ' -n analysis --format pmd %s --api-token %s --user-uuid %s'; + $cmd = Builder::PHP_CLI_TAG . ' ' . $binaryPath . ' -n analysis --format pmd %s --api-token %s --user-uuid %s'; // Run Sensiolabs Insight: $this->builder->executeCommand(