You are here

function _coder_review_sniffer_callback in Coder 7.2

Rule callback: Runs the PHP_CodeSniffer rules.

See also

do_coder_review_callback()

1 string reference to '_coder_review_sniffer_callback'
coder_review_sniffer_reviews in coder_review/includes/coder_review_sniffer.inc
Implements hook_reviews().

File

coder_review/includes/coder_review_sniffer.inc, line 46
This include file implements coder_review functionality for PHP_CodeSniffer.

Code

function _coder_review_sniffer_callback(array &$coder_args, array $review, array $rule, array $lines, array &$results) {
  if (!class_exists('PHP_CodeSniffer')) {
    _message(_t('PHP/CodeSniffer.php not found'), 'error');
    return;
  }

  // Include the CodeSniffer class once.
  // Allocate the CodeSniffer object once.
  static $phpcs, $phpcs_dir, $phpcs_cli;
  if (!isset($phpcs)) {

    // Allocate the CodeSniffer object.
    // Save the CodeSniffer directory, and restore our default directory.
    $current_dir = getcwd();
    $phpcs = new PHP_CodeSniffer(0, 0, 'utf-8');
    $phpcs_dir = getcwd();
    chdir($current_dir);

    // Bypass the CodeSniffer client which will always look at the command line options.
    class CoderReviewCodeSnifferClient extends PHP_CodeSniffer_CLI {
      protected $values = array();
      public function __construct($allowed_extensions = array()) {
        $this->values = $this
          ->getDefaults();
        $this->values['extensions'] = implode(',', $allowed_extensions);
        $this->errorSeverity = PHPCS_DEFAULT_ERROR_SEV;
        $this->warningSeverity = PHPCS_DEFAULT_WARN_SEV;
      }
      public function getCommandLineValues() {
        return $this->values;
      }

    }
    $allowed_extensions = array_merge($coder_args['#php_extensions'], $coder_args['#include_extensions'], array(
      'module',
    ));
    $phpcs_cli = new CoderReviewCodeSnifferClient($allowed_extensions);
    $phpcs
      ->setCli($phpcs_cli);
    $sniffer_dir = __DIR__ . '/../../coder_sniffer/Drupal';
    $phpcs
      ->process(array(), $sniffer_dir, array());
    $phpcs
      ->populateTokenListeners();
  }

  // Process this file.
  // Restore the CodeSniffer directory during file processing.
  // Then restore it back to our directory.
  $filename = realpath($coder_args['#filename']);
  if (!$filename) {
    $tmpname = drupal_tempnam('temporary://', 'file');
    if (file_put_contents($tmpname, $coder_args['#raw_contents']) !== FALSE) {
      $filename = $tmpname;
    }
  }
  $current_dir = getcwd();
  chdir($phpcs_dir);
  $phpcs_file = $phpcs
    ->processFile($filename, $coder_args['#raw_contents']);
  chdir($current_dir);

  // Read the errors.
  $errors = $phpcs_file
    ->getErrors();
  $warnings = $phpcs_file
    ->getWarnings();
  if ($errors || $warnings) {
    _coder_review_sniffer_warnings($results, $rule, $lines, $coder_args['#ignores'], $errors);
    _coder_review_sniffer_warnings($results, $rule, $lines, $coder_args['#ignores'], $warnings);
  }
  if (isset($tmpname) && $filename == $tmpname) {
    unlink($tmpname);
  }
}