From 62d0bd17add95449953efdfdb794051a02606c4e Mon Sep 17 00:00:00 2001 From: Edward Wu Date: Thu, 4 Oct 2018 10:08:30 -0700 Subject: [PATCH] Add BLT command to use PHPCS to sniff unstaged modified or untracked files in repo. --- src/Robo/Commands/Validate/PhpcsCommand.php | 72 +++++++++++++-------- 1 file changed, 45 insertions(+), 27 deletions(-) diff --git a/src/Robo/Commands/Validate/PhpcsCommand.php b/src/Robo/Commands/Validate/PhpcsCommand.php index a43adbea78..992c24bf92 100644 --- a/src/Robo/Commands/Validate/PhpcsCommand.php +++ b/src/Robo/Commands/Validate/PhpcsCommand.php @@ -67,45 +67,63 @@ public function sniffFileList($file_list) { $this->say("Sniffing directories containing changed files..."); $files = explode("\n", $file_list); $files = array_filter($files); - $exit_code = $this->doSniffFileList($files); + if ($files) { + $temp_path = $this->getConfigValue('repo.root') . '/tmp/phpcs-fileset'; + $this->taskWriteToFile($temp_path) + ->lines($files) + ->run(); + $arguments = "--file-list='$temp_path' -l"; + $exit_code = $this->doSniff($arguments); + unlink($temp_path); - return $exit_code; + return $exit_code; + } + + return 0; } /** - * Executes PHP Code Sniffer against an array of files. + * Executes PHPCS against (unstaged) modified or untracked files in repo. * - * @param array $files - * A flat array of absolute file paths. + * This command will execute PHP Codesniffer against modified/untracked files + * if those files are a subset of the phpcs.filesets filesets. + * + * @command tests:phpcs:sniff:modified + * @aliases tpsm * * @return int */ - protected function doSniffFileList(array $files) { - if ($files) { - $temp_path = $this->getConfigValue('repo.root') . '/tmp/phpcs-fileset'; - $this->taskWriteToFile($temp_path) - ->lines($files) - ->run(); + public function sniffModified() { + $this->say("Sniffing modified and untracked files in repo..."); + $arguments = "--filter=gitmodified " . $this->getConfigValue('repo.root'); + $exit_code = $this->doSniff($arguments); - $bin = $this->getConfigValue('composer.bin') . '/phpcs'; - $command = "'$bin' --file-list='$temp_path' -l"; - if ($this->output()->isVerbose()) { - $command .= ' -v'; - } - elseif ($this->output()->isVeryVerbose()) { - $command .= ' -vv'; - } - $result = $this->taskExecStack() - ->exec($command) - ->printMetadata(FALSE) - ->run(); - - unlink($temp_path); + return $exit_code; + } - return $result->getExitCode(); + /** + * Executes PHP Code Sniffer using specified options/arguments. + * + * @param string $arguments + * The command arguments/options. + * + * @return int + */ + protected function doSniff($arguments) { + $bin = $this->getConfigValue('composer.bin') . '/phpcs'; + $command = "'$bin' $arguments"; + if ($this->output()->isVerbose()) { + $command .= ' -v'; } + elseif ($this->output()->isVeryVerbose()) { + $command .= ' -vv'; + } + $result = $this->taskExecStack() + ->exec($command) + ->printMetadata(FALSE) + ->run(); - return 0; + return $result->getExitCode(); } }