Skip to content

Commit

Permalink
Add BLT command to use PHPCS to sniff unstaged modified or untracked …
Browse files Browse the repository at this point in the history
…files in repo.
  • Loading branch information
wu-edward committed Oct 9, 2018
1 parent 7e74296 commit 62d0bd1
Showing 1 changed file with 45 additions and 27 deletions.
72 changes: 45 additions & 27 deletions src/Robo/Commands/Validate/PhpcsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

}

0 comments on commit 62d0bd1

Please sign in to comment.