Ejemplo n.º 1
0
 /**
  * 実行
  *
  * @param string $path パス
  *
  * @return void
  */
 public static function process($path)
 {
     require_once 'PHP/CodeSniffer.php';
     require_once 'PHP/CodeSniffer/Reports/Full.php';
     $phpcs = new PHP_CodeSniffer();
     try {
         $phpcs->process($path, 'BEAR');
         echo "<pre><code>";
         echo "<div class='info'>BEAR Convention</div>";
         $fileViolations = $phpcs->getFilesErrors();
         $report = new PHP_CodeSniffer_Reporting();
         $report->printReport('Summary', $fileViolations, true, null, 120);
         $report->printReport('Full', $fileViolations, false, null, 120);
         echo "</code></pre>";
     } catch (Exception $e) {
         echo $e->getMessage();
     }
 }
Ejemplo n.º 2
0
Archivo: Cli.php Proyecto: memaw/phpcs
 /**
  * Run the coding style check
  *
  * @return int
  */
 public function process()
 {
     $this->setDefaultValues();
     \PHP_CodeSniffer_Reporting::startTiming();
     $phpcs = new \PHP_CodeSniffer_CLI();
     $phpcs->checkRequirements();
     $values = $phpcs->getCommandLineValues();
     foreach ($this->defaultValues as $k => $v) {
         if (empty($values[$k])) {
             $values[$k] = $v;
         }
     }
     return $phpcs->process($values);
 }
Ejemplo n.º 3
0
 /**
  * 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');
 }
Ejemplo n.º 4
0
 /**
  * Run the code sniffs over a single given file.
  *
  * Processes the file and runs the PHP_CodeSniffer sniffs to verify that it
  * conforms with the standard.
  *
  * @param string $file     The file to process.
  * @param string $contents The contents to parse. If NULL, the content
  *                         is taken from the file system.
  *
  * @return void
  * @throws PHP_CodeSniffer_Exception If the file could not be processed.
  * @see    _processFile()
  */
 public function processFile($file, $contents = null)
 {
     if ($contents === null && file_exists($file) === false) {
         throw new PHP_CodeSniffer_Exception("Source file {$file} does not exist");
     }
     // If the file's path matches one of our ignore patterns, skip it.
     foreach ($this->ignorePatterns as $pattern) {
         $replacements = array('\\,' => ',', '*' => '.*');
         $pattern = strtr($pattern, $replacements);
         if (preg_match("|{$pattern}|i", $file) === 1) {
             return;
         }
     }
     // Before we go and spend time tokenizing this file, just check
     // to see if there is a tag up top to indicate that the whole
     // file should be ignored. It must be on one of the first two lines.
     $firstContent = $contents;
     if ($contents === null) {
         $handle = fopen($file, 'r');
         if ($handle !== false) {
             $firstContent = fgets($handle);
             $firstContent .= fgets($handle);
             fclose($handle);
         }
     }
     if (strpos($firstContent, '@codingStandardsIgnoreFile') !== false) {
         // We are ignoring the whole file.
         if (PHP_CODESNIFFER_VERBOSITY > 0) {
             echo 'Ignoring ' . basename($file) . PHP_EOL;
         }
         return;
     }
     $this->_processFile($file, $contents);
     if (PHP_CODESNIFFER_INTERACTIVE === false) {
         return;
     }
     /*
         Running interactively.
         Print the error report for the current file and then wait for user input.
     */
     $reporting = new PHP_CodeSniffer_Reporting();
     $cliValues = $this->cli->getCommandLineValues();
     // Get current violations and then clear the list to make sure
     // we only print violations for a single file each time.
     $numErrors = null;
     while ($numErrors !== 0) {
         $filesViolations = $this->getFilesErrors();
         $this->files = array();
         $numErrors = $reporting->printReport($cliValues['report'], $filesViolations, $cliValues['showSources'], '', $cliValues['reportWidth']);
         if ($numErrors === 0) {
             continue;
         }
         echo '<ENTER> to recheck, [s] to skip or [q] to quit : ';
         $input = fgets(STDIN);
         $input = trim($input);
         switch ($input) {
             case 's':
                 break;
             case 'q':
                 exit(0);
                 break;
             default:
                 // Repopulate the sniffs because some of them save their state
                 // and only clear it when the file changes, but we are rechecking
                 // the same file.
                 $this->populateTokenListeners();
                 $this->_processFile($file, $contents);
                 break;
         }
     }
     //end while
 }
Ejemplo n.º 5
0
 * Run the code checker from the command-line.
 *
 * @package    local
 * @subpackage codechecker
 * @copyright  2011 The Open University
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
define('CLI_SCRIPT', true);
require dirname(__FILE__) . '/../../config.php';
require_once $CFG->libdir . '/clilib.php';
require_once $CFG->dirroot . '/local/codechecker/locallib.php';
// Get the command-line options.
list($options, $unrecognized) = cli_get_params(array('help' => false), array('h' => 'help'));
if (count($unrecognized) != 1) {
    $options['help'] = true;
} else {
    $path = clean_param(reset($unrecognized), PARAM_PATH);
}
if ($options['help']) {
    echo get_string('clihelp', 'local_codechecker'), "\n";
    die;
}
raise_memory_limit(MEMORY_HUGE);
$standard = $CFG->dirroot . str_replace('/', DIRECTORY_SEPARATOR, '/local/codechecker/moodle');
$phpcs = new PHP_CodeSniffer(1);
$phpcs->setCli(new local_codechecker_codesniffer_cli());
$phpcs->setIgnorePatterns(local_codesniffer_get_ignores());
$numerrors = $phpcs->process(local_codechecker_clean_path($CFG->dirroot . '/' . trim($path, '/')), local_codechecker_clean_path($standard));
$reporting = new PHP_CodeSniffer_Reporting();
$problems = $phpcs->getFilesErrors();
$reporting->printReport('full', $problems, false, null);
Ejemplo n.º 6
0
 /**
  * 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;
 }
Ejemplo n.º 7
0
 /**
  * Prints the error report for the run.
  *
  * Note that this function may actually print multiple reports
  * as the user may have specified a number of output formats.
  *
  * @param PHP_CodeSniffer $phpcs       The PHP_CodeSniffer object containing
  *                                     the errors.
  * @param array           $reports     A list of reports to print.
  * @param bool            $showSources TRUE if report should show error sources
  *                                     (not used by all reports).
  * @param string          $reportFile  A default file to log report output to.
  * @param int             $reportWidth How wide the screen reports should be.
  *
  * @return int The number of error and warning messages shown.
  */
 public function printErrorReport(PHP_CodeSniffer $phpcs, $reports, $showSources, $reportFile, $reportWidth)
 {
     $reporting = new PHP_CodeSniffer_Reporting();
     $filesViolations = $phpcs->getFilesErrors();
     if (empty($reports) === true) {
         $reports['full'] = $reportFile;
     }
     $errors = 0;
     $toScreen = false;
     foreach ($reports as $report => $output) {
         if ($output === null) {
             $output = $reportFile;
         }
         if ($reportFile === null) {
             $toScreen = true;
         }
         // We don't add errors here because the number of
         // errors reported by each report type will always be the
         // same, so we really just need 1 number.
         $errors = $reporting->printReport($report, $filesViolations, $showSources, $output, $reportWidth);
     }
     // Only print PHP_Timer output if no reports were
     // printed to the screen so we don't put additional output
     // in something like an XML report. If we are printing to screen,
     // the report types would have already worked out who should
     // print the timer info.
     if ($toScreen === false && PHP_CODESNIFFER_INTERACTIVE === false && class_exists('PHP_Timer', false) === true) {
         echo PHP_Timer::resourceUsage() . PHP_EOL . PHP_EOL;
     }
     // They should all return the same value, so it
     // doesn't matter which return value we end up using.
     return $errors;
 }
Ejemplo n.º 8
0
 /**
  * Prints the error report for the run.
  *
  * Note that this function may actually print multiple reports
  * as the user may have specified a number of output formats.
  *
  * @param PHP_CodeSniffer $phpcs       The PHP_CodeSniffer object containing
  *                                     the errors.
  * @param array           $reports     A list of reports to print.
  * @param bool            $showSources TRUE if report should show error sources
  *                                     (not used by all reports).
  * @param string          $reportFile  A default file to log report output to.
  * @param int             $reportWidth How wide the screen reports should be.
  *
  * @return int The number of error and warning messages shown.
  */
 public function printErrorReport(PHP_CodeSniffer $phpcs, $reports, $showSources, $reportFile, $reportWidth)
 {
     if (empty($reports) === true) {
         $reports['full'] = $reportFile;
     }
     $errors = 0;
     $warnings = 0;
     $toScreen = false;
     foreach ($reports as $report => $output) {
         if ($output === null) {
             $output = $reportFile;
         }
         if ($reportFile === null) {
             $toScreen = true;
         }
         // We don't add errors here because the number of
         // errors reported by each report type will always be the
         // same, so we really just need 1 number.
         $result = $phpcs->reporting->printReport($report, $showSources, $this->values, $output, $reportWidth);
         $errors = $result['errors'];
         $warnings = $result['warnings'];
     }
     //end foreach
     // Only print timer output if no reports were
     // printed to the screen so we don't put additional output
     // in something like an XML report. If we are printing to screen,
     // the report types would have already worked out who should
     // print the timer info.
     if (PHP_CODESNIFFER_INTERACTIVE === false && ($toScreen === false || $errors + $warnings === 0 && $this->values['showProgress'] === true)) {
         PHP_CodeSniffer_Reporting::printRunTime();
     }
     // They should all return the same value, so it
     // doesn't matter which return value we end up using.
     $ignoreWarnings = PHP_CodeSniffer::getConfigData('ignore_warnings_on_exit');
     $ignoreErrors = PHP_CodeSniffer::getConfigData('ignore_errors_on_exit');
     $return = $errors + $warnings;
     if ($ignoreErrors !== null) {
         $ignoreErrors = (bool) $ignoreErrors;
         if ($ignoreErrors === true) {
             $return -= $errors;
         }
     }
     if ($ignoreWarnings !== null) {
         $ignoreWarnings = (bool) $ignoreWarnings;
         if ($ignoreWarnings === true) {
             $return -= $warnings;
         }
     }
     return $return;
 }
 /**
  * Prints the source of all errors and warnings.
  *
  * @param string  $cachedData    Any partial report data that was returned from
  *                               generateFileReport during the run.
  * @param int     $totalFiles    Total number of files processed during the run.
  * @param int     $totalErrors   Total number of errors found during the run.
  * @param int     $totalWarnings Total number of warnings found during the run.
  * @param int     $totalFixable  Total number of problems that can be fixed.
  * @param boolean $showSources   Show sources?
  * @param int     $width         Maximum allowed line width.
  * @param boolean $toScreen      Is the report being printed to screen?
  *
  * @return void
  */
 public function generate($cachedData, $totalFiles, $totalErrors, $totalWarnings, $totalFixable, $showSources = false, $width = 80, $toScreen = true)
 {
     $width = max($width, 70);
     if (empty($this->_sourceCache) === true) {
         // Nothing to show.
         return;
     }
     asort($this->_sourceCache);
     $this->_sourceCache = array_reverse($this->_sourceCache);
     echo PHP_EOL . 'PHP CODE SNIFFER VIOLATION SOURCE SUMMARY' . PHP_EOL;
     echo str_repeat('-', $width) . PHP_EOL;
     if ($showSources === true) {
         if ($totalFixable > 0) {
             echo '    SOURCE' . str_repeat(' ', $width - 15) . 'COUNT' . PHP_EOL;
         } else {
             echo 'SOURCE' . str_repeat(' ', $width - 11) . 'COUNT' . PHP_EOL;
         }
     } else {
         if ($totalFixable > 0) {
             echo '    STANDARD  CATEGORY            SNIFF' . str_repeat(' ', $width - 44) . 'COUNT' . PHP_EOL;
         } else {
             echo 'STANDARD  CATEGORY            SNIFF' . str_repeat(' ', $width - 40) . 'COUNT' . PHP_EOL;
         }
     }
     echo str_repeat('-', $width) . PHP_EOL;
     $fixableSources = 0;
     $maxSniffWidth = 37;
     if ($totalFixable > 0) {
         $maxSniffWidth += 4;
     }
     foreach ($this->_sourceCache as $source => $sourceData) {
         if ($totalFixable > 0) {
             echo '[';
             if ($sourceData['fixable'] === true) {
                 echo 'x';
                 $fixableSources++;
             } else {
                 echo ' ';
             }
             echo '] ';
         }
         if ($showSources === true) {
             echo $source;
             if ($totalFixable > 0) {
                 echo str_repeat(' ', $width - 9 - strlen($source));
             } else {
                 echo str_repeat(' ', $width - 5 - strlen($source));
             }
         } else {
             $parts = explode('.', $source);
             if ($parts[0] === 'Internal') {
                 $parts[2] = $parts[1];
                 $parts[1] = '';
             }
             if (strlen($parts[0]) > 8) {
                 $parts[0] = substr($parts[0], 0, (strlen($parts[0]) - 8) * -1);
             }
             echo $parts[0] . str_repeat(' ', 10 - strlen($parts[0]));
             $category = $this->makeFriendlyName($parts[1]);
             if (strlen($category) > 18) {
                 $category = substr($category, 0, (strlen($category) - 18) * -1);
             }
             echo $category . str_repeat(' ', 20 - strlen($category));
             $sniff = $this->makeFriendlyName($parts[2]);
             if (isset($parts[3]) === true) {
                 $name = $this->makeFriendlyName($parts[3]);
                 $name[0] = strtolower($name[0]);
                 $sniff .= ' ' . $name;
             }
             if (strlen($sniff) > $width - $maxSniffWidth) {
                 $sniff = substr($sniff, 0, $width - $maxSniffWidth - strlen($sniff));
             }
             if ($totalFixable > 0) {
                 echo $sniff . str_repeat(' ', $width - 39 - strlen($sniff));
             } else {
                 echo $sniff . str_repeat(' ', $width - 35 - strlen($sniff));
             }
         }
         //end if
         echo $sourceData['count'] . PHP_EOL;
     }
     //end foreach
     echo str_repeat('-', $width) . PHP_EOL;
     echo 'A TOTAL OF ' . ($totalErrors + $totalWarnings) . ' SNIFF VIOLATION';
     if ($totalErrors + $totalWarnings > 1) {
         echo 'S';
     }
     echo ' WERE FOUND IN ' . count($this->_sourceCache) . ' SOURCE';
     if (count($this->_sourceCache) !== 1) {
         echo 'S';
     }
     if ($totalFixable > 0) {
         echo PHP_EOL . str_repeat('-', $width) . PHP_EOL;
         echo "PHPCBF CAN FIX THE {$fixableSources} MARKED SOURCES AUTOMATICALLY ({$totalFixable} VIOLATIONS IN TOTAL)";
     }
     echo PHP_EOL . str_repeat('-', $width) . PHP_EOL . PHP_EOL;
     if ($toScreen === true && PHP_CODESNIFFER_INTERACTIVE === false) {
         PHP_CodeSniffer_Reporting::printRunTime();
     }
 }
Ejemplo n.º 10
0
 /**
  * Prints the author of all errors and warnings, as given by "version control blame".
  *
  * @param string  $cachedData    Any partial report data that was returned from
  *                               generateFileReport during the run.
  * @param int     $totalFiles    Total number of files processed during the run.
  * @param int     $totalErrors   Total number of errors found during the run.
  * @param int     $totalWarnings Total number of warnings found during the run.
  * @param int     $totalFixable  Total number of problems that can be fixed.
  * @param boolean $showSources   Show sources?
  * @param int     $width         Maximum allowed line width.
  * @param boolean $toScreen      Is the report being printed to screen?
  *
  * @return void
  */
 public function generate($cachedData, $totalFiles, $totalErrors, $totalWarnings, $totalFixable, $showSources = false, $width = 80, $toScreen = true)
 {
     $errorsShown = $totalErrors + $totalWarnings;
     if ($errorsShown === 0) {
         // Nothing to show.
         return;
     }
     // Make sure the report width isn't too big.
     $maxLength = 0;
     foreach ($this->_authorCache as $author => $count) {
         $maxLength = max($maxLength, strlen($author));
         if ($showSources === true && isset($this->_sourceCache[$author]) === true) {
             foreach ($this->_sourceCache[$author] as $source => $sourceData) {
                 if ($source === 'count') {
                     continue;
                 }
                 $maxLength = max($maxLength, strlen($source) + 9);
             }
         }
     }
     $width = min($width, $maxLength + 30);
     $width = max($width, 70);
     arsort($this->_authorCache);
     echo PHP_EOL . "" . 'PHP CODE SNIFFER ' . $this->reportName . ' BLAME SUMMARY' . "" . PHP_EOL;
     echo str_repeat('-', $width) . PHP_EOL . "";
     if ($showSources === true) {
         echo 'AUTHOR   SOURCE' . str_repeat(' ', $width - 43) . '(Author %) (Overall %) COUNT' . PHP_EOL;
         echo str_repeat('-', $width) . PHP_EOL;
     } else {
         echo 'AUTHOR' . str_repeat(' ', $width - 34) . '(Author %) (Overall %) COUNT' . PHP_EOL;
         echo str_repeat('-', $width) . PHP_EOL;
     }
     echo "";
     if ($showSources === true) {
         $maxSniffWidth = $width - 15;
         if ($totalFixable > 0) {
             $maxSniffWidth -= 4;
         }
     }
     $fixableSources = 0;
     foreach ($this->_authorCache as $author => $count) {
         if ($this->_praiseCache[$author]['good'] === 0) {
             $percent = 0;
         } else {
             $total = $this->_praiseCache[$author]['bad'] + $this->_praiseCache[$author]['good'];
             $percent = round($this->_praiseCache[$author]['bad'] / $total * 100, 2);
         }
         $overallPercent = '(' . round($count / $errorsShown * 100, 2) . ')';
         $authorPercent = '(' . $percent . ')';
         $line = str_repeat(' ', 6 - strlen($count)) . $count;
         $line = str_repeat(' ', 12 - strlen($overallPercent)) . $overallPercent . $line;
         $line = str_repeat(' ', 11 - strlen($authorPercent)) . $authorPercent . $line;
         $line = $author . str_repeat(' ', $width - strlen($author) - strlen($line)) . $line;
         if ($showSources === true) {
             $line = "{$line}";
         }
         echo $line . PHP_EOL;
         if ($showSources === true && isset($this->_sourceCache[$author]) === true) {
             $errors = $this->_sourceCache[$author];
             asort($errors);
             $errors = array_reverse($errors);
             foreach ($errors as $source => $sourceData) {
                 if ($source === 'count') {
                     continue;
                 }
                 $count = $sourceData['count'];
                 $srcLength = strlen($source);
                 if ($srcLength > $maxSniffWidth) {
                     $source = substr($source, 0, $maxSniffWidth);
                 }
                 $line = str_repeat(' ', 5 - strlen($count)) . $count;
                 echo '         ';
                 if ($totalFixable > 0) {
                     echo '[';
                     if ($sourceData['fixable'] === true) {
                         echo 'x';
                         $fixableSources++;
                     } else {
                         echo ' ';
                     }
                     echo '] ';
                 }
                 echo $source;
                 if ($totalFixable > 0) {
                     echo str_repeat(' ', $width - 18 - strlen($source));
                 } else {
                     echo str_repeat(' ', $width - 14 - strlen($source));
                 }
                 echo $line . PHP_EOL;
             }
             //end foreach
         }
         //end if
     }
     //end foreach
     echo str_repeat('-', $width) . PHP_EOL;
     echo "" . 'A TOTAL OF ' . $errorsShown . ' SNIFF VIOLATION';
     if ($errorsShown !== 1) {
         echo 'S';
     }
     echo ' WERE COMMITTED BY ' . count($this->_authorCache) . ' AUTHOR';
     if (count($this->_authorCache) !== 1) {
         echo 'S';
     }
     echo "";
     if ($totalFixable > 0) {
         if ($showSources === true) {
             echo PHP_EOL . str_repeat('-', $width) . PHP_EOL;
             echo "PHPCBF CAN FIX THE {$fixableSources} MARKED SOURCES AUTOMATICALLY ({$totalFixable} VIOLATIONS IN TOTAL)";
         } else {
             echo PHP_EOL . str_repeat('-', $width) . PHP_EOL;
             echo "PHPCBF CAN FIX {$totalFixable} OF THESE SNIFF VIOLATIONS AUTOMATICALLY";
         }
     }
     echo PHP_EOL . str_repeat('-', $width) . PHP_EOL . PHP_EOL;
     if ($toScreen === true && PHP_CODESNIFFER_INTERACTIVE === false) {
         PHP_CodeSniffer_Reporting::printRunTime();
     }
 }
Ejemplo n.º 11
0
 /**
  * Generates a summary of errors and warnings for each file processed.
  *
  * @param string  $cachedData    Any partial report data that was returned from
  *                               generateFileReport during the run.
  * @param int     $totalFiles    Total number of files processed during the run.
  * @param int     $totalErrors   Total number of errors found during the run.
  * @param int     $totalWarnings Total number of warnings found during the run.
  * @param int     $totalFixable  Total number of problems that can be fixed.
  * @param boolean $showSources   Show sources?
  * @param int     $width         Maximum allowed line width.
  * @param boolean $toScreen      Is the report being printed to screen?
  *
  * @return void
  */
 public function generate($cachedData, $totalFiles, $totalErrors, $totalWarnings, $totalFixable, $showSources = false, $width = 80, $toScreen = true)
 {
     if (empty($this->_reportFiles) === true) {
         return;
     }
     // Make sure the report width isn't too big.
     $maxLength = 0;
     foreach ($this->_reportFiles as $file => $data) {
         $maxLength = max($maxLength, $data['strlen']);
     }
     $width = min($width, $maxLength + 21);
     $width = max($width, 70);
     echo PHP_EOL . "" . 'PHP CODE SNIFFER REPORT SUMMARY' . "" . PHP_EOL;
     echo str_repeat('-', $width) . PHP_EOL;
     echo "" . 'FILE' . str_repeat(' ', $width - 20) . 'ERRORS  WARNINGS' . "" . PHP_EOL;
     echo str_repeat('-', $width) . PHP_EOL;
     foreach ($this->_reportFiles as $file => $data) {
         $padding = $width - 18 - $data['strlen'];
         if ($padding < 0) {
             $file = '...' . substr($file, $padding * -1 + 3);
             $padding = 0;
         }
         echo $file . str_repeat(' ', $padding) . '  ';
         if ($data['errors'] !== 0) {
             echo "" . $data['errors'] . "";
             echo str_repeat(' ', 8 - strlen((string) $data['errors']));
         } else {
             echo '0       ';
         }
         if ($data['warnings'] !== 0) {
             echo "" . $data['warnings'] . "";
         } else {
             echo '0';
         }
         echo PHP_EOL;
     }
     //end foreach
     echo str_repeat('-', $width) . PHP_EOL;
     echo "A TOTAL OF {$totalErrors} ERROR";
     if ($totalErrors !== 1) {
         echo 'S';
     }
     echo ' AND ' . $totalWarnings . ' WARNING';
     if ($totalWarnings !== 1) {
         echo 'S';
     }
     echo ' WERE FOUND IN ' . $totalFiles . ' FILE';
     if ($totalFiles !== 1) {
         echo 'S';
     }
     echo "";
     if ($totalFixable > 0) {
         echo PHP_EOL . str_repeat('-', $width) . PHP_EOL;
         echo "PHPCBF CAN FIX {$totalFixable} OF THESE SNIFF VIOLATIONS AUTOMATICALLY";
     }
     echo PHP_EOL . str_repeat('-', $width) . PHP_EOL . PHP_EOL;
     if ($toScreen === true && PHP_CODESNIFFER_INTERACTIVE === false) {
         PHP_CodeSniffer_Reporting::printRunTime();
     }
 }
Ejemplo n.º 12
0
 /**
  * Start recording time for the run.
  *
  * @return void
  */
 public static function startTiming()
 {
     self::$startTime = microtime(true);
 }
Ejemplo n.º 13
0
 /**
  * Generates a summary of errors and warnings for each file processed.
  *
  * @param string  $cachedData    Any partial report data that was returned from
  *                               generateFileReport during the run.
  * @param int     $totalFiles    Total number of files processed during the run.
  * @param int     $totalErrors   Total number of errors found during the run.
  * @param int     $totalWarnings Total number of warnings found during the run.
  * @param int     $totalFixable  Total number of problems that can be fixed.
  * @param boolean $showSources   Show sources?
  * @param int     $width         Maximum allowed line width.
  * @param boolean $toScreen      Is the report being printed to screen?
  *
  * @return void
  */
 public function generate($cachedData, $totalFiles, $totalErrors, $totalWarnings, $totalFixable, $showSources = false, $width = 80, $toScreen = true)
 {
     if ($cachedData === '') {
         return;
     }
     echo PHP_EOL . 'PHP CODE SNIFFER REPORT SUMMARY' . PHP_EOL;
     echo str_repeat('-', $width) . PHP_EOL;
     echo 'FILE' . str_repeat(' ', $width - 20) . 'ERRORS  WARNINGS' . PHP_EOL;
     echo str_repeat('-', $width) . PHP_EOL;
     echo $cachedData;
     echo str_repeat('-', $width) . PHP_EOL;
     echo 'A TOTAL OF ' . $totalErrors . ' ERROR';
     if ($totalErrors !== 1) {
         echo 'S';
     }
     echo ' AND ' . $totalWarnings . ' WARNING';
     if ($totalWarnings !== 1) {
         echo 'S';
     }
     echo ' WERE FOUND IN ' . $totalFiles . ' FILE';
     if ($totalFiles !== 1) {
         echo 'S';
     }
     if ($totalFixable > 0) {
         echo PHP_EOL . str_repeat('-', $width) . PHP_EOL;
         echo 'PHPCBF CAN FIX ' . $totalFixable . ' OF THESE SNIFF VIOLATIONS AUTOMATICALLY';
     }
     echo PHP_EOL . str_repeat('-', $width) . PHP_EOL . PHP_EOL;
     if ($toScreen === true && PHP_CODESNIFFER_INTERACTIVE === false) {
         PHP_CodeSniffer_Reporting::printRunTime();
     }
 }
 /**
  * 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;
 }
Ejemplo n.º 15
0
 /**
  * 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;
 }
Ejemplo n.º 16
0
 /** {@inheritDoc} */
 public function generateFileReport($report, PHP_CodeSniffer_File $phpcsFile, $showSources = false, $width = 80)
 {
     $diff = $this->getStagedDiff();
     $changes = $this->getChanges($diff);
     $report = $this->filterReport($report, $changes);
     $reporting = new PHP_CodeSniffer_Reporting();
     $actual = $reporting->factory($this->reportType);
     return $actual->generateFileReport($report, $phpcsFile, $showSources, $width);
 }
Ejemplo n.º 17
0
 /**
  * Prints the source of all errors and warnings.
  *
  * @param string  $cachedData    Any partial report data that was returned from
  *                               generateFileReport during the run.
  * @param int     $totalFiles    Total number of files processed during the run.
  * @param int     $totalErrors   Total number of errors found during the run.
  * @param int     $totalWarnings Total number of warnings found during the run.
  * @param int     $totalFixable  Total number of problems that can be fixed.
  * @param boolean $showSources   Show sources?
  * @param int     $width         Maximum allowed line width.
  * @param boolean $toScreen      Is the report being printed to screen?
  *
  * @return void
  */
 public function generate($cachedData, $totalFiles, $totalErrors, $totalWarnings, $totalFixable, $showSources = false, $width = 80, $toScreen = true)
 {
     if (empty($this->_sourceCache) === true) {
         // Nothing to show.
         return;
     }
     // Make sure the report width isn't too big.
     $maxLength = 0;
     foreach ($this->_sourceCache as $source => $data) {
         $maxLength = max($maxLength, $data['strlen']);
     }
     if ($showSources === true) {
         $width = min($width, $maxLength + 11);
     } else {
         $width = min($width, $maxLength + 41);
     }
     $width = max($width, 70);
     asort($this->_sourceCache);
     $this->_sourceCache = array_reverse($this->_sourceCache);
     echo PHP_EOL . "PHP CODE SNIFFER VIOLATION SOURCE SUMMARY" . PHP_EOL;
     echo str_repeat('-', $width) . PHP_EOL . "";
     if ($showSources === true) {
         if ($totalFixable > 0) {
             echo '    SOURCE' . str_repeat(' ', $width - 15) . 'COUNT' . PHP_EOL;
         } else {
             echo 'SOURCE' . str_repeat(' ', $width - 11) . 'COUNT' . PHP_EOL;
         }
     } else {
         if ($totalFixable > 0) {
             echo '    STANDARD  CATEGORY            SNIFF' . str_repeat(' ', $width - 44) . 'COUNT' . PHP_EOL;
         } else {
             echo 'STANDARD  CATEGORY            SNIFF' . str_repeat(' ', $width - 40) . 'COUNT' . PHP_EOL;
         }
     }
     echo "" . str_repeat('-', $width) . PHP_EOL;
     $fixableSources = 0;
     if ($showSources === true) {
         $maxSniffWidth = $width - 7;
     } else {
         $maxSniffWidth = $width - 37;
     }
     if ($totalFixable > 0) {
         $maxSniffWidth -= 4;
     }
     foreach ($this->_sourceCache as $source => $sourceData) {
         if ($totalFixable > 0) {
             echo '[';
             if ($sourceData['fixable'] === true) {
                 echo 'x';
                 $fixableSources++;
             } else {
                 echo ' ';
             }
             echo '] ';
         }
         if ($showSources === true) {
             if ($sourceData['strlen'] > $maxSniffWidth) {
                 $source = substr($source, 0, $maxSniffWidth);
             }
             echo $source;
             if ($totalFixable > 0) {
                 echo str_repeat(' ', $width - 9 - strlen($source));
             } else {
                 echo str_repeat(' ', $width - 5 - strlen($source));
             }
         } else {
             $parts = $sourceData['parts'];
             if (strlen($parts[0]) > 8) {
                 $parts[0] = substr($parts[0], 0, (strlen($parts[0]) - 8) * -1);
             }
             echo $parts[0] . str_repeat(' ', 10 - strlen($parts[0]));
             $category = $parts[1];
             if (strlen($category) > 18) {
                 $category = substr($category, 0, (strlen($category) - 18) * -1);
             }
             echo $category . str_repeat(' ', 20 - strlen($category));
             $sniff = $parts[2];
             if (strlen($sniff) > $maxSniffWidth) {
                 $sniff = substr($sniff, 0, $maxSniffWidth);
             }
             if ($totalFixable > 0) {
                 echo $sniff . str_repeat(' ', $width - 39 - strlen($sniff));
             } else {
                 echo $sniff . str_repeat(' ', $width - 35 - strlen($sniff));
             }
         }
         //end if
         echo $sourceData['count'] . PHP_EOL;
     }
     //end foreach
     echo str_repeat('-', $width) . PHP_EOL;
     echo "" . 'A TOTAL OF ' . ($totalErrors + $totalWarnings) . ' SNIFF VIOLATION';
     if ($totalErrors + $totalWarnings > 1) {
         echo 'S';
     }
     echo ' WERE FOUND IN ' . count($this->_sourceCache) . ' SOURCE';
     if (count($this->_sourceCache) !== 1) {
         echo 'S';
     }
     echo "";
     if ($totalFixable > 0) {
         echo PHP_EOL . str_repeat('-', $width) . PHP_EOL;
         echo "PHPCBF CAN FIX THE {$fixableSources} MARKED SOURCES AUTOMATICALLY ({$totalFixable} VIOLATIONS IN TOTAL)";
     }
     echo PHP_EOL . str_repeat('-', $width) . PHP_EOL . PHP_EOL;
     if ($toScreen === true && PHP_CODESNIFFER_INTERACTIVE === false) {
         PHP_CodeSniffer_Reporting::printRunTime();
     }
 }
Ejemplo n.º 18
0
 /**
  * Print a report into a file
  * 
  * @param string $file       File path
  * @param string $reportType One of full xml checkstyle csv emacs source summary svnblame gitblame
  *
  * @return int               Error and warning count
  */
 function report($file, $reportType = "xml")
 {
     // Create the file
     $reportPath = $this->makeReportPath($file, $reportType);
     CMbPath::forceDir(dirname($reportPath));
     touch($reportPath);
     // Build the report
     $reporting = new PHP_CodeSniffer_Reporting();
     return $reporting->printReport($reportType, $this->getFilesErrors(), $showSources = true, $reportPath, $reportWidth = 120);
 }
Ejemplo n.º 19
0
 /**
  * Prints the source of all errors and warnings.
  *
  * @param string  $cachedData    Any partial report data that was returned from
  *                               generateFileReport during the run.
  * @param int     $totalFiles    Total number of files processed during the run.
  * @param int     $totalErrors   Total number of errors found during the run.
  * @param int     $totalWarnings Total number of warnings found during the run.
  * @param int     $totalFixable  Total number of problems that can be fixed.
  * @param boolean $showSources   Show sources?
  * @param int     $width         Maximum allowed line width.
  * @param boolean $toScreen      Is the report being printed to screen?
  *
  * @return void
  */
 public function generate($cachedData, $totalFiles, $totalErrors, $totalWarnings, $totalFixable, $showSources = false, $width = 80, $toScreen = true)
 {
     if (empty($this->_metricCache) === true) {
         // Nothing to show.
         return;
     }
     ksort($this->_metricCache);
     echo PHP_EOL . "" . 'PHP CODE SNIFFER INFORMATION REPORT' . "" . PHP_EOL;
     echo str_repeat('-', 70) . PHP_EOL;
     foreach ($this->_metricCache as $metric => $values) {
         $winner = '';
         $winnerCount = 0;
         $totalCount = 0;
         foreach ($values as $value => $count) {
             $totalCount += $count;
             if ($count > $winnerCount) {
                 $winner = $value;
                 $winnerCount = $count;
             }
         }
         $winPercent = round($winnerCount / $totalCount * 100, 2);
         echo "{$metric}: {$winner} [{$winnerCount}/{$totalCount}, {$winPercent}%]" . PHP_EOL;
         asort($values);
         $values = array_reverse($values, true);
         foreach ($values as $value => $count) {
             if ($value === $winner) {
                 continue;
             }
             $percent = round($count / $totalCount * 100, 2);
             echo "\t{$value} => {$count} ({$percent}%)" . PHP_EOL;
         }
         echo PHP_EOL;
     }
     //end foreach
     echo str_repeat('-', 70) . PHP_EOL;
     if ($toScreen === true && PHP_CODESNIFFER_INTERACTIVE === false) {
         PHP_CodeSniffer_Reporting::printRunTime();
     }
 }
Ejemplo n.º 20
0
 /**
  * Prints the error report.
  *
  * @param PHP_CodeSniffer $phpcs        The PHP_CodeSniffer object containing
  *                                      the errors.
  * @param string          $report       The type of report to print.
  * @param bool            $showWarnings TRUE if warnings should also be printed.
  * @param bool            $showSources  TRUE if report should show error sources
  *                                      (not used by all reports).
  * @param string          $reportFile   A file to log the report out to.
  * @param int             $reportWidth  How wide the screen reports should be.
  *
  * @return int The number of error and warning messages shown.
  */
 public function printErrorReport(PHP_CodeSniffer $phpcs, $report, $showWarnings, $showSources, $reportFile, $reportWidth)
 {
     $filesViolations = $phpcs->getFilesErrors();
     $reporting = new PHP_CodeSniffer_Reporting();
     return $reporting->printReport($report, $filesViolations, $showWarnings, $showSources, $reportFile, $reportWidth);
 }
Ejemplo n.º 21
0
 /**
  * Prints all errors and warnings for each file processed.
  *
  * @param string  $cachedData    Any partial report data that was returned from
  *                               generateFileReport during the run.
  * @param int     $totalFiles    Total number of files processed during the run.
  * @param int     $totalErrors   Total number of errors found during the run.
  * @param int     $totalWarnings Total number of warnings found during the run.
  * @param int     $totalFixable  Total number of problems that can be fixed.
  * @param boolean $showSources   Show sources?
  * @param int     $width         Maximum allowed line width.
  * @param boolean $toScreen      Is the report being printed to screen?
  *
  * @return void
  */
 public function generate($cachedData, $totalFiles, $totalErrors, $totalWarnings, $totalFixable, $showSources = false, $width = 80, $toScreen = true)
 {
     if ($cachedData === '') {
         return;
     }
     echo $cachedData;
     if ($toScreen === true && PHP_CODESNIFFER_INTERACTIVE === false) {
         PHP_CodeSniffer_Reporting::printRunTime();
     }
 }
 /**
  * Prints the author of all errors and warnings, as given by "version control blame".
  *
  * @param string  $cachedData    Any partial report data that was returned from
  *                               generateFileReport during the run.
  * @param int     $totalFiles    Total number of files processed during the run.
  * @param int     $totalErrors   Total number of errors found during the run.
  * @param int     $totalWarnings Total number of warnings found during the run.
  * @param int     $totalFixable  Total number of problems that can be fixed.
  * @param boolean $showSources   Show sources?
  * @param int     $width         Maximum allowed line width.
  * @param boolean $toScreen      Is the report being printed to screen?
  *
  * @return void
  */
 public function generate($cachedData, $totalFiles, $totalErrors, $totalWarnings, $totalFixable, $showSources = false, $width = 80, $toScreen = true)
 {
     $errorsShown = $totalErrors + $totalWarnings;
     if ($errorsShown === 0) {
         // Nothing to show.
         return;
     }
     $width = max($width, 70);
     arsort($this->_authorCache);
     echo PHP_EOL . 'PHP CODE SNIFFER ' . $this->reportName . ' BLAME SUMMARY' . PHP_EOL;
     echo str_repeat('-', $width) . PHP_EOL;
     if ($showSources === true) {
         echo 'AUTHOR   SOURCE' . str_repeat(' ', $width - 43) . '(Author %) (Overall %) COUNT' . PHP_EOL;
         echo str_repeat('-', $width) . PHP_EOL;
     } else {
         echo 'AUTHOR' . str_repeat(' ', $width - 34) . '(Author %) (Overall %) COUNT' . PHP_EOL;
         echo str_repeat('-', $width) . PHP_EOL;
     }
     foreach ($this->_authorCache as $author => $count) {
         if ($this->_praiseCache[$author]['good'] === 0) {
             $percent = 0;
         } else {
             $total = $this->_praiseCache[$author]['bad'] + $this->_praiseCache[$author]['good'];
             $percent = round($this->_praiseCache[$author]['bad'] / $total * 100, 2);
         }
         $overallPercent = '(' . round($count / $errorsShown * 100, 2) . ')';
         $authorPercent = '(' . $percent . ')';
         $line = str_repeat(' ', 6 - strlen($count)) . $count;
         $line = str_repeat(' ', 12 - strlen($overallPercent)) . $overallPercent . $line;
         $line = str_repeat(' ', 11 - strlen($authorPercent)) . $authorPercent . $line;
         $line = $author . str_repeat(' ', $width - strlen($author) - strlen($line)) . $line;
         echo $line . PHP_EOL;
         if ($showSources === true && isset($this->_sourceCache[$author]) === true) {
             $errors = $this->_sourceCache[$author];
             asort($errors);
             $errors = array_reverse($errors);
             foreach ($errors as $source => $count) {
                 if ($source === 'count') {
                     continue;
                 }
                 $line = str_repeat(' ', 5 - strlen($count)) . $count;
                 echo '         ' . $source . str_repeat(' ', $width - 14 - strlen($source)) . $line . PHP_EOL;
             }
         }
     }
     //end foreach
     echo str_repeat('-', $width) . PHP_EOL;
     echo 'A TOTAL OF ' . $errorsShown . ' SNIFF VIOLATION';
     if ($errorsShown !== 1) {
         echo 'S';
     }
     echo ' WERE COMMITTED BY ' . count($this->_authorCache) . ' AUTHOR';
     if (count($this->_authorCache) !== 1) {
         echo 'S';
     }
     if ($totalFixable > 0) {
         echo PHP_EOL . str_repeat('-', $width) . PHP_EOL;
         echo 'PHPCBF CAN FIX ' . $totalFixable . ' OF THESE SNIFF VIOLATIONS AUTOMATICALLY';
     }
     echo PHP_EOL . str_repeat('-', $width) . PHP_EOL . PHP_EOL;
     if ($toScreen === true && PHP_CODESNIFFER_INTERACTIVE === false) {
         PHP_CodeSniffer_Reporting::printRunTime();
     }
 }
Ejemplo n.º 23
0
 /**
  * Run the code sniffs over a single given file.
  *
  * Processes the file and runs the PHP_CodeSniffer sniffs to verify that it
  * conforms with the standard. Returns the processed file object, or NULL
  * if no file was processed due to error.
  *
  * @param string $file     The file to process.
  * @param string $contents The contents to parse. If NULL, the content
  *                         is taken from the file system.
  *
  * @return PHP_CodeSniffer_File
  * @throws PHP_CodeSniffer_Exception If the file could not be processed.
  * @see    _processFile()
  */
 public function processFile($file, $contents = null)
 {
     if ($contents === null && file_exists($file) === false) {
         throw new PHP_CodeSniffer_Exception("Source file {$file} does not exist");
     }
     $filePath = realpath($file);
     if ($filePath === false) {
         $filePath = $file;
     }
     // Before we go and spend time tokenizing this file, just check
     // to see if there is a tag up top to indicate that the whole
     // file should be ignored. It must be on one of the first two lines.
     $firstContent = $contents;
     if ($contents === null && is_readable($filePath) === true) {
         $handle = fopen($filePath, 'r');
         if ($handle !== false) {
             $firstContent = fgets($handle);
             $firstContent .= fgets($handle);
             fclose($handle);
         }
     }
     if (strpos($firstContent, '@codingStandardsIgnoreFile') !== false) {
         // We are ignoring the whole file.
         if (PHP_CODESNIFFER_VERBOSITY > 0) {
             echo 'Ignoring ' . basename($filePath) . PHP_EOL;
         }
         return null;
     }
     try {
         $phpcsFile = $this->_processFile($file, $contents);
     } catch (Exception $e) {
         $trace = $e->getTrace();
         $filename = $trace[0]['args'][0];
         if (is_object($filename) === true && get_class($filename) === 'PHP_CodeSniffer_File') {
             $filename = $filename->getFilename();
         } else {
             if (is_numeric($filename) === true) {
                 // See if we can find the PHP_CodeSniffer_File object.
                 foreach ($trace as $data) {
                     if (isset($data['args'][0]) === true && $data['args'][0] instanceof PHP_CodeSniffer_File === true) {
                         $filename = $data['args'][0]->getFilename();
                     }
                 }
             } else {
                 if (is_string($filename) === false) {
                     $filename = (string) $filename;
                 }
             }
         }
         $error = 'An error occurred during processing; checking has been aborted. The error message was: ' . $e->getMessage();
         $phpcsFile = new PHP_CodeSniffer_File($filename, $this->listeners, $this->allowedFileExtensions, $this->ruleset, $this);
         $this->addFile($phpcsFile);
         $phpcsFile->addError($error, null);
     }
     //end try
     if (PHP_CODESNIFFER_INTERACTIVE === false) {
         return $phpcsFile;
     }
     /*
         Running interactively.
         Print the error report for the current file and then wait for user input.
     */
     $reporting = new PHP_CodeSniffer_Reporting();
     $cliValues = $this->cli->getCommandLineValues();
     // Get current violations and then clear the list to make sure
     // we only print violations for a single file each time.
     $numErrors = null;
     while ($numErrors !== 0) {
         $filesViolations = $this->getFilesErrors();
         $this->files = array();
         $numErrors = $reporting->printReport('full', $filesViolations, $cliValues['showSources'], null, $cliValues['reportWidth']);
         if ($numErrors === 0) {
             continue;
         }
         echo '<ENTER> to recheck, [s] to skip or [q] to quit : ';
         $input = fgets(STDIN);
         $input = trim($input);
         switch ($input) {
             case 's':
                 break;
             case 'q':
                 exit(0);
                 break;
             default:
                 // Repopulate the sniffs because some of them save their state
                 // and only clear it when the file changes, but we are rechecking
                 // the same file.
                 $this->populateTokenListeners();
                 $phpcsFile = $this->_processFile($file, $contents);
                 break;
         }
     }
     //end while
     return $phpcsFile;
 }
Ejemplo n.º 24
0
 /**
  * Prints the error report for the run.
  *
  * Note that this function may actually print multiple reports
  * as the user may have specified a number of output formats.
  *
  * @param PHP_CodeSniffer $phpcs       The PHP_CodeSniffer object containing
  *                                     the errors.
  * @param array           $reports     A list of reports to print.
  * @param bool            $showSources TRUE if report should show error sources
  *                                     (not used by all reports).
  * @param string          $reportFile  A default file to log report output to.
  * @param int             $reportWidth How wide the screen reports should be.
  *
  * @return int The number of error and warning messages shown.
  */
 public function printErrorReport(PHP_CodeSniffer $phpcs, $reports, $showSources, $reportFile, $reportWidth)
 {
     $reporting = new PHP_CodeSniffer_Reporting();
     $filesViolations = $phpcs->getFilesErrors();
     if (empty($reports) === true) {
         $reports['full'] = $reportFile;
     }
     $errors = 0;
     foreach ($reports as $report => $output) {
         if ($output === null) {
             $output = $reportFile;
         }
         $errors = $reporting->printReport($report, $filesViolations, $showSources, $output, $reportWidth);
     }
     // They should all return the same value, so it
     // doesn't matter which return value we end up using.
     return $errors;
 }