/** * Test prepare report method. * * @return void */ public function testPrepare() { $fixtureFilesViolations = $this->getFixtureFilesViolations(); $reports = $this->reporting->prepare($fixtureFilesViolations); $this->assertArrayHasKey('files', $reports); $this->assertEquals(3, count($reports['files'])); $this->assertArrayHasKey('foo', $reports['files']); $this->assertArrayHasKey('errors', $reports['files']['foo']); $this->assertEquals(4, $reports['files']['foo']['errors']); $this->assertArrayHasKey('warnings', $reports['files']['foo']); $this->assertEquals(2, $reports['files']['foo']['warnings']); // Two errors on line 1 column 10. $this->assertArrayHasKey('messages', $reports['files']['foo']); $fooMessages = $reports['files']['foo']['messages']; $this->assertArrayHasKey(1, $fooMessages, 'messages on line 1'); $this->assertArrayHasKey(10, $fooMessages[1], 'messages on line 1 column 10'); $this->assertEquals(2, count($fooMessages[1][10]), '2 messages on line 1 column 10'); // One error one warning on line 10 column 1. $this->assertArrayHasKey(10, $fooMessages, 'messages on line 10'); $this->assertArrayHasKey(1, $fooMessages[10], 'messages on line 10 column 1'); $this->assertEquals(2, count($fooMessages[10][1]), '2 messages on line 10 column 1'); // Empty file has structure without data. $this->assertArrayHasKey('baz', $reports['files']); $this->assertArrayHasKey('messages', $reports['files']['baz']); $this->assertEquals(0, count($reports['files']['baz']['messages'])); // Totals. $this->assertArrayHasKey('totals', $reports, 'report totals exist'); $this->assertArrayHasKey('errors', $reports['totals'], 'errors total exists'); $this->assertEquals(8, $reports['totals']['errors'], 'errors total is well calculated'); $this->assertArrayHasKey('warnings', $reports['totals'], 'warnings total exists'); $this->assertEquals(2, $reports['totals']['warnings'], 'warnings total is well calculated'); // Files Order. reset($reports['files']); $this->assertEquals('bar', key($reports['files']), 'report files ordered by name'); next($reports['files']); $this->assertEquals('baz', key($reports['files']), 'report files ordered by name'); // Violations Order. reset($fooMessages); $this->assertEquals(1, key($fooMessages), 'line level violations order'); next($fooMessages); $this->assertEquals(5, key($fooMessages), 'line level violations order'); reset($fooMessages[1]); $this->assertEquals(1, key($fooMessages[1]), 'column level violations order'); next($fooMessages[1]); $this->assertEquals(10, key($fooMessages[1]), 'column level violations order'); }
/** * Prints the error report. * * @param PHP_CodeSniffer $phpcs The PHP_CodeSniffer object containing * the errors. * * @return int The number of error and warning messages shown. */ protected function printErrorReport($phpcs) { if ($this->showSniffs) { $sniffs = $phpcs->getSniffs(); $sniffStr = ''; foreach ($sniffs as $sniff) { $sniffStr .= '- ' . $sniff . PHP_EOL; } $this->log('The list of used sniffs (#' . count($sniffs) . '): ' . PHP_EOL . $sniffStr, Project::MSG_INFO); } $filesViolations = $phpcs->getFilesErrors(); $reporting = new PHP_CodeSniffer_Reporting(); $report = $reporting->prepare($filesViolations, $this->showWarnings); // process output foreach ($this->formatters as $fe) { switch ($fe->getType()) { case 'default': // default format goes to logs, no buffering $this->outputCustomFormat($report); $fe->setUseFile(false); break; default: $reportFile = ''; if ($fe->getUseFile()) { $reportFile = $fe->getOutfile()->getPath(); ob_start(); } $reporting->printReport($fe->getType(), $filesViolations, $this->showWarnings, $this->showSources, $reportFile, $this->reportWidth); // reporting class uses ob_end_flush(), but we don't want // an output if we use a file if ($fe->getUseFile()) { ob_end_clean(); } break; } } return $report; }
/** * Prints the error report. * * @param PHP_CodeSniffer $phpcs The PHP_CodeSniffer object containing * the errors. * * @return int The number of error and warning messages shown. */ protected function printErrorReport($phpcs) { if ($this->showSniffs) { $sniffs = $phpcs->getSniffs(); $sniffStr = ''; foreach ($sniffs as $sniff) { $sniffStr .= '- ' . $sniff . PHP_EOL; } $this->log('The list of used sniffs (#' . count($sniffs) . '): ' . PHP_EOL . $sniffStr, Project::MSG_INFO); } $filesViolations = $phpcs->getFilesErrors(); $reporting = new PHP_CodeSniffer_Reporting(); $report = $reporting->prepare($filesViolations, $this->showWarnings); // process output foreach ($this->formatters as $fe) { switch ($fe->getType()) { case 'default': // default format goes to logs, no buffering $this->outputCustomFormat($report); $fe->setUseFile(false); break; default: $reportFile = null; if ($fe->getUseFile()) { $reportFile = $fe->getOutfile(); ob_start(); } // Determine number of parameters required to // ensure backwards compatibility $rm = new ReflectionMethod('PHP_CodeSniffer_Reporting', 'printReport'); if ($rm->getNumberOfParameters() == 5) { $reporting->printReport($fe->getType(), $filesViolations, $this->showSources, $reportFile, $this->reportWidth); } else { $reporting->printReport($fe->getType(), $filesViolations, $this->showWarnings, $this->showSources, $reportFile, $this->reportWidth); } // reporting class uses ob_end_flush(), but we don't want // an output if we use a file if ($fe->getUseFile()) { ob_end_clean(); } break; } } return $report; }
/** * Execute the task * * @return self * @throw BuildException */ public function execute() { if (!$this->getStandard()) { throw new BuildException("No standard set"); } if (!class_exists("CodeSniffer")) { Pale::run(function () { require_once "PHP/CodeSniffer.php"; }); } if (CodeSniffer::isInstalledStandard($this->getStandard()) === false) { throw new BuildException("Invalid standard name"); } // Clear out argv so PHP_CodeSniffer doesn"t freak out $oldArgv = $SERVER["argv"]; $SERVER["argv"] = array(); $SERVER["argc"] = 0; // Get the current working directory because PHP_CodeSniffer will change it $cwd = getcwd(); $codeSniffer = new CodeSniffer(0, 0, "UTF-8"); $codeSniffer->process($this->getFiles(), $this->filterProperties($this->getStandard())); // Restore the argv/c superglobals $SERVER["argv"] = $oldArgv; $SERVER["argc"] = count($oldArgv); // Reset the current working directory chdir($cwd); $filesViolations = $codeSniffer->getFilesErrors(); $reporting = new Reporting(); $report = $reporting->prepare($filesViolations, $this->getShowWarnings()); $reporting->printReport($this->getReportType(), $filesViolations, $this->getShowSources(), $this->getReportFile(), $this->getReportWidth()); return $this; }