/**
  * Initialise the standard that the run will use.
  *
  * @param string|array $standards    The set of code sniffs we are testing
  *                                   against.
  * @param array        $restrictions The sniff codes to restrict the
  *
  * @return void
  */
 public function initStandard($standards, array $restrictions = array())
 {
     $standards = (array) $standards;
     // Reset the members.
     $this->listeners = array();
     $this->sniffs = array();
     $this->ruleset = array();
     $this->_tokenListeners = array();
     self::$rulesetDirs = array();
     // Ensure this option is enabled or else line endings will not always
     // be detected properly for files created on a Mac with the /r line ending.
     ini_set('auto_detect_line_endings', true);
     $sniffs = array();
     foreach ($standards as $standard) {
         $installed = $this->getInstalledStandardPath($standard);
         if ($installed !== null) {
             $standard = $installed;
         } else {
             $standard = self::realpath($standard);
             if (is_dir($standard) === true && is_file(self::realpath($standard . DIRECTORY_SEPARATOR . 'ruleset.xml')) === true) {
                 $standard = self::realpath($standard . DIRECTORY_SEPARATOR . 'ruleset.xml');
             }
         }
         if (PHP_CODESNIFFER_VERBOSITY === 1) {
             $ruleset = simplexml_load_string(file_get_contents($standard));
             if ($ruleset !== false) {
                 $standardName = (string) $ruleset['name'];
             }
             echo "Registering sniffs in the {$standardName} standard... ";
             if (count($standards) > 1 || PHP_CODESNIFFER_VERBOSITY > 2) {
                 echo PHP_EOL;
             }
         }
         $sniffs = array_merge($sniffs, $this->processRuleset($standard));
     }
     //end foreach
     $sniffRestrictions = array();
     foreach ($restrictions as $sniffCode) {
         $parts = explode('.', strtolower($sniffCode));
         $sniffRestrictions[] = $parts[0] . '_sniffs_' . $parts[1] . '_' . $parts[2] . 'sniff';
     }
     $this->registerSniffs($sniffs, $sniffRestrictions);
     $this->populateTokenListeners();
     if (PHP_CODESNIFFER_VERBOSITY === 1) {
         $numSniffs = count($this->sniffs);
         echo "DONE ({$numSniffs} sniffs registered)" . PHP_EOL;
     }
 }
Beispiel #2
0
 /**
  * Processes the files/directories that PHP_CodeSniffer was constructed with.
  *
  * @param string|array $files        The files and directories to process. For
  *                                   directories, each sub directory will also
  *                                   be traversed for source files.
  * @param string|array $standards    The set of code sniffs we are testing
  *                                   against.
  * @param array        $restrictions The sniff codes to restrict the
  *                                   violations to.
  * @param boolean      $local        If true, don't recurse into directories.
  *
  * @return void
  * @throws PHP_CodeSniffer_Exception If files or standard are invalid.
  */
 public function process($files, $standards, array $restrictions = array(), $local = false)
 {
     if (is_array($files) === false) {
         $files = array($files);
     }
     if (is_array($standards) === false) {
         $standards = array($standards);
     }
     // Reset the members.
     $this->listeners = array();
     $this->sniffs = array();
     $this->ruleset = array();
     $this->_tokenListeners = array();
     self::$rulesetDirs = array();
     // Ensure this option is enabled or else line endings will not always
     // be detected properly for files created on a Mac with the /r line ending.
     ini_set('auto_detect_line_endings', true);
     $sniffs = array();
     foreach ($standards as $standard) {
         $installed = $this->getInstalledStandardPath($standard);
         if ($installed !== null) {
             $standard = $installed;
         } else {
             if (is_dir($standard) === true && is_file(realpath($standard . '/ruleset.xml')) === true) {
                 $standard = realpath($standard . '/ruleset.xml');
             }
         }
         if (PHP_CODESNIFFER_VERBOSITY === 1) {
             $ruleset = simplexml_load_file($standard);
             if ($ruleset !== false) {
                 $standardName = (string) $ruleset['name'];
             }
             echo "Registering sniffs in the {$standardName} standard... ";
             if (count($standards) > 1 || PHP_CODESNIFFER_VERBOSITY > 2) {
                 echo PHP_EOL;
             }
         }
         $sniffs = array_merge($sniffs, $this->processRuleset($standard));
     }
     //end foreach
     $sniffRestrictions = array();
     foreach ($restrictions as $sniffCode) {
         $parts = explode('.', strtolower($sniffCode));
         $sniffRestrictions[] = $parts[0] . '_sniffs_' . $parts[1] . '_' . $parts[2] . 'sniff';
     }
     $this->registerSniffs($sniffs, $sniffRestrictions);
     $this->populateTokenListeners();
     if (PHP_CODESNIFFER_VERBOSITY === 1) {
         $numSniffs = count($this->sniffs);
         echo "DONE ({$numSniffs} sniffs registered)" . PHP_EOL;
     }
     // The SVN pre-commit calls process() to init the sniffs
     // and ruleset so there may not be any files to process.
     // But this has to come after that initial setup.
     if (empty($files) === true) {
         return;
     }
     $cliValues = $this->cli->getCommandLineValues();
     $showProgress = $cliValues['showProgress'];
     if (PHP_CODESNIFFER_VERBOSITY > 0) {
         echo 'Creating file list... ';
     }
     $todo = $this->getFilesToProcess($files, $local);
     $numFiles = count($todo);
     if (PHP_CODESNIFFER_VERBOSITY > 0) {
         echo "DONE ({$numFiles} files in queue)" . PHP_EOL;
     }
     $numProcessed = 0;
     $dots = 0;
     $maxLength = strlen($numFiles);
     $lastDir = '';
     foreach ($todo as $file) {
         $this->file = $file;
         $currDir = dirname($file);
         if ($lastDir !== $currDir) {
             if (PHP_CODESNIFFER_VERBOSITY > 0) {
                 echo 'Changing into directory ' . $currDir . PHP_EOL;
             }
             $lastDir = $currDir;
         }
         $phpcsFile = $this->processFile($file, null, $restrictions);
         $numProcessed++;
         if (PHP_CODESNIFFER_VERBOSITY > 0 || PHP_CODESNIFFER_INTERACTIVE === true || $showProgress === false) {
             continue;
         }
         // Show progress information.
         if ($phpcsFile === null) {
             echo 'S';
         } else {
             $errors = $phpcsFile->getErrorCount();
             $warnings = $phpcsFile->getWarningCount();
             if ($errors > 0) {
                 echo 'E';
             } else {
                 if ($warnings > 0) {
                     echo 'W';
                 } else {
                     echo '.';
                 }
             }
         }
         $dots++;
         if ($dots === 60) {
             $padding = $maxLength - strlen($numProcessed);
             echo str_repeat(' ', $padding);
             $percent = round($numProcessed / $numFiles * 100);
             echo " {$numProcessed} / {$numFiles} ({$percent}%)" . PHP_EOL;
             $dots = 0;
         }
     }
     //end foreach
     if (PHP_CODESNIFFER_VERBOSITY === 0 && PHP_CODESNIFFER_INTERACTIVE === false && $showProgress === true) {
         echo PHP_EOL . PHP_EOL;
     }
 }