Esempio n. 1
0
 /**
  * Validate the current check
  *
  * Validate the check on the specified repository. Returns an array of 
  * found issues.
  * 
  * @param pchRepository $repository 
  * @return void
  */
 public function validate(pchRepository $repository)
 {
     $cs = new PHP_CodeSniffer();
     $cs->process(array(), $this->standard);
     foreach ($this->getChangedFiles($repository) as $file) {
         $cs->processFile($file, stream_get_contents($this->getFileContents($repository, $file)));
     }
     $issues = array();
     foreach ($cs->getFilesErrors() as $file => $messages) {
         foreach ($messages['errors'] as $errors) {
             foreach ($errors as $line => $lineErrors) {
                 foreach ($lineErrors as $error) {
                     $issues[] = new pchIssue(E_ERROR, $file, $line, $error['source'] . ': ' . $error['message']);
                 }
             }
         }
         foreach ($messages['warnings'] as $errors) {
             foreach ($errors as $line => $lineErrors) {
                 foreach ($lineErrors as $error) {
                     $issues[] = new pchIssue(E_WARNING, $file, $line, $error['source'] . ': ' . $error['message']);
                 }
             }
         }
     }
     return $issues;
 }
Esempio n. 2
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();
     }
 }
Esempio n. 3
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;
 }
 /**
  * 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;
 }
Esempio 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);
Esempio n. 6
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);
 }
 /**
  * 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;
 }
Esempio n. 8
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;
 }
Esempio n. 9
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;
 }