diff --git a/.github/workflows/integration.yml b/.github/workflows/integration.yml index cb6b363..925ae87 100644 --- a/.github/workflows/integration.yml +++ b/.github/workflows/integration.yml @@ -41,4 +41,4 @@ jobs: run: tools/phpcs --standard=psr12 src tests - name: Static code analysis - run: tools/phpstan analyse + run: tools/phpstan analyse -c phpstan-${{ matrix.php-versions }}.neon diff --git a/phive.xml b/phive.xml index 55d8d8a..ea7b0a4 100644 --- a/phive.xml +++ b/phive.xml @@ -1,6 +1,6 @@ - - - + + + diff --git a/phpstan-7.3.neon b/phpstan-7.3.neon new file mode 100644 index 0000000..f4476cb --- /dev/null +++ b/phpstan-7.3.neon @@ -0,0 +1,4 @@ +parameters: + level: 5 + paths: + - %currentWorkingDirectory%/src/ diff --git a/phpstan-7.4.neon b/phpstan-7.4.neon new file mode 100644 index 0000000..f4476cb --- /dev/null +++ b/phpstan-7.4.neon @@ -0,0 +1,4 @@ +parameters: + level: 5 + paths: + - %currentWorkingDirectory%/src/ diff --git a/phpstan-8.0.neon b/phpstan-8.0.neon new file mode 100644 index 0000000..2bb2649 --- /dev/null +++ b/phpstan-8.0.neon @@ -0,0 +1,6 @@ +parameters: + level: 5 + paths: + - %currentWorkingDirectory%/src/ + ignoreErrors: + - '#Left side of && is always true#' diff --git a/src/Command/Executable.php b/src/Command/Executable.php index 7a4c812..beb0024 100644 --- a/src/Command/Executable.php +++ b/src/Command/Executable.php @@ -45,6 +45,13 @@ class Executable implements Command */ private $options = []; + /** + * List of variables to define + * + * @var string[] + */ + private $vars = []; + /** * List of acceptable exit codes. * @@ -71,9 +78,9 @@ public function __construct(string $cmd, array $exitCodes = [0]) */ public function getCommand(): string { - $cmd = sprintf('"%s"', $this->cmd) - . (count($this->options) ? ' ' . implode(' ', $this->options) : '') - . ($this->isSilent ? ' 2> /dev/null' : ''); + $cmd = $this->getVars() . sprintf('"%s"', $this->cmd) + . (count($this->options) ? ' ' . implode(' ', $this->options) : '') + . ($this->isSilent ? ' 2> /dev/null' : ''); return Util::escapeSpacesIfOnWindows($cmd); } @@ -125,6 +132,36 @@ public function addOption(string $option, $value = null, string $glue = '='): Ex return $this; } + /** + * Add a var definition to a command + * + * @param string $name + * @param string $value + * @return $this + */ + public function addVar(string $name, string $value): Executable + { + $this->vars[$name] = $value; + + return $this; + } + + /** + * Return variable definition string e.g. "MYFOO='sometext' MYBAR='nothing' " + * + * @return string + */ + protected function getVars(): string + { + $varStrings = []; + + foreach ($this->vars as $name => $value) { + $varStrings[] = $name . '=' . escapeshellarg($value); + } + + return count($varStrings) ? implode(' ', $varStrings) . ' ' : ''; + } + /** * Adds an option to a command if it is not empty. * diff --git a/tests/cli/Command/ExecutableTest.php b/tests/cli/Command/ExecutableTest.php index 9590fad..d903a11 100644 --- a/tests/cli/Command/ExecutableTest.php +++ b/tests/cli/Command/ExecutableTest.php @@ -107,4 +107,17 @@ public function testAddOptionIfEmptyAsValue() $this->assertEquals('"foo" -bar=\'fiz\'', (string) $cmd, 'option should be added'); } + + /** + * Tests Executable::addVar + */ + public function testAddVar() + { + $cmd = new Executable('tool'); + $cmd->addVar('FOO', 'fiz'); + $cmd->addVar('BAR', 'baz'); + $cmd->addOptionIfNotEmpty('--extra', 'bonus'); + + $this->assertEquals('FOO=\'fiz\' BAR=\'baz\' "tool" --extra=\'bonus\'', (string) $cmd, 'vars prefix'); + } }