Exemplo n.º 1
0
 /**
  * Checks filtering rules to see if a path should be ignored.
  *
  * @param string $path The path to the file or directory being checked.
  *
  * @return bool
  */
 protected function shouldIgnorePath($path)
 {
     if ($this->ignoreFilePatterns === null) {
         $this->ignoreDirPatterns = array();
         $this->ignoreFilePatterns = array();
         $ignorePatterns = array_merge($this->config->ignored, $this->ruleset->getIgnorePatterns());
         foreach ($ignorePatterns as $pattern => $type) {
             // If the ignore pattern ends with /* then it is ignoring an entire directory.
             if (substr($pattern, -2) === '/*') {
                 $this->ignoreDirPatterns[substr($pattern, 0, -2)] = $type;
             } else {
                 $this->ignoreFilePatterns[$pattern] = $type;
             }
         }
     }
     $relativePath = $path;
     if (strpos($path, $this->basedir) === 0) {
         // The +1 cuts off the directory separator as well.
         $relativePath = substr($path, strlen($this->basedir) + 1);
     }
     if (is_dir($path) === true) {
         $ignorePatterns = $this->ignoreDirPatterns;
     } else {
         $ignorePatterns = $this->ignoreFilePatterns;
     }
     foreach ($ignorePatterns as $pattern => $type) {
         // Maintains backwards compatibility in case the ignore pattern does
         // not have a relative/absolute value.
         if (is_int($pattern) === true) {
             $pattern = $type;
             $type = 'absolute';
         }
         $replacements = array('\\,' => ',', '*' => '.*');
         // We assume a / directory separator, as do the exclude rules
         // most developers write, so we need a special case for any system
         // that is different.
         if (DIRECTORY_SEPARATOR === '\\') {
             $replacements['/'] = '\\\\';
         }
         $pattern = strtr($pattern, $replacements);
         if ($type === 'relative') {
             $testPath = $relativePath;
         } else {
             $testPath = $path;
         }
         $pattern = '`' . $pattern . '`i';
         if (preg_match($pattern, $testPath) === 1) {
             return true;
         }
     }
     //end foreach
     return false;
 }
Exemplo n.º 2
0
 /**
  * Constructs a file.
  *
  * @param string                   $path    The absolute path to the file to process.
  * @param \PHP_CodeSniffer\Ruleset $ruleset The ruleset used for the run.
  * @param \PHP_CodeSniffer\Config  $config  The config data for the run.
  *
  * @return void
  */
 public function __construct($path, Ruleset $ruleset, Config $config)
 {
     $this->path = $path;
     $this->ruleset = $ruleset;
     $this->config = $config;
     $this->fixer = new Fixer();
     $parts = explode('.', $path);
     $extension = array_pop($parts);
     if (isset($config->extensions[$extension]) === true) {
         $this->tokenizerType = $config->extensions[$extension];
     } else {
         // Revert to default.
         $this->tokenizerType = 'PHP';
     }
     $this->configCache['cache'] = $this->config->cache;
     $this->configCache['sniffs'] = $this->config->sniffs;
     $this->configCache['exclude'] = $this->config->exclude;
     $this->configCache['errorSeverity'] = $this->config->errorSeverity;
     $this->configCache['warningSeverity'] = $this->config->warningSeverity;
     $this->configCache['recordErrors'] = $this->config->recordErrors;
     $this->configCache['ignorePatterns'] = $this->ruleset->getIgnorePatterns();
 }