diff --git a/PHPCI/Plugin/PhpCodeSniffer.php b/PHPCI/Plugin/PhpCodeSniffer.php index b942e44b..fe19fa0a 100755 --- a/PHPCI/Plugin/PhpCodeSniffer.php +++ b/PHPCI/Plugin/PhpCodeSniffer.php @@ -151,7 +151,7 @@ class PhpCodeSniffer implements PHPCI\Plugin, PHPCI\ZeroConfigPlugin return false; } - $cmd = $phpcs . ' --report=emacs %s %s %s %s %s "%s"'; + $cmd = $phpcs . ' --report=json %s %s %s %s %s "%s"'; $this->phpci->executeCommand( $cmd, $standard, @@ -163,26 +163,19 @@ class PhpCodeSniffer implements PHPCI\Plugin, PHPCI\ZeroConfigPlugin ); $output = $this->phpci->getLastOutput(); + list($errors, $warnings, $data) = $this->processReport(json_decode(trim($output), true)); $success = true; - $matches = array(); - if (preg_match_all('/\: warning \-/', $output, $matches)) { - $warnings = count($matches[0]); - $this->build->storeMeta('phpcs-warnings', $warnings); + $this->build->storeMeta('phpcs-warnings', $warnings); + $this->build->storeMeta('phpcs-errors', $errors); + $this->build->storeMeta('phpcs-data', $data); - if ($this->allowed_warnings != -1 && $warnings > $this->allowed_warnings) { - $success = false; - } + if ($this->allowed_warnings != -1 && $warnings > $this->allowed_warnings) { + $success = false; } - $matches = array(); - if (preg_match_all('/\: error \-/', $output, $matches)) { - $errors = count($matches[0]); - $this->build->storeMeta('phpcs-errors', $errors); - - if ($this->allowed_errors != -1 && $errors > $this->allowed_errors) { - $success = false; - } + if ($this->allowed_errors != -1 && $errors > $this->allowed_errors) { + $success = false; } return $success; @@ -208,4 +201,31 @@ class PhpCodeSniffer implements PHPCI\Plugin, PHPCI\ZeroConfigPlugin return array($ignore, $standard, $suffixes); } + + protected function processReport($data) + { + if (!is_array($data)) { + throw new \Exception('Could not process PHPCS report JSON.'); + } + + $errors = $data['totals']['errors']; + $warnings = $data['totals']['warnings']; + + $rtn = array(); + + foreach ($data['files'] as $fileName => $file) { + $fileName = str_replace($this->phpci->buildPath, '', $fileName); + + foreach ($file['messages'] as $message) { + $rtn[] = array( + 'file' => $fileName, + 'line' => $message['line'], + 'type' => $message['type'], + 'message' => $message['message'], + ); + } + } + + return array($errors, $warnings, $rtn); + } }