PHP_CodeSniffer
[ class tree: PHP_CodeSniffer ] [ index: PHP_CodeSniffer ] [ all elements ]

Source for file Filter.php

Documentation is available at Filter.php

  1. <?php
  2. /**
  3.  * A base filter class for filtering out files and folders during a run.
  4.  *
  5.  * @author    Greg Sherwood <gsherwood@squiz.net>
  6.  * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
  7.  * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
  8.  */
  9.  
  10. namespace PHP_CodeSniffer\Filters;
  11.  
  12. use PHP_CodeSniffer\Util;
  13. use PHP_CodeSniffer\Ruleset;
  14. use PHP_CodeSniffer\Config;
  15.  
  16. class Filter extends \RecursiveFilterIterator
  17. {
  18.     /**
  19.      * The top-level path we are filtering.
  20.      *
  21.      * @var string 
  22.      */
  23.     protected $basedir = null;
  24.  
  25.     /**
  26.      * The config data for the run.
  27.      *
  28.      * @var \PHP_CodeSniffer\Config 
  29.      */
  30.     protected $config = null;
  31.  
  32.     /**
  33.      * The ruleset used for the run.
  34.      *
  35.      * @var \PHP_CodeSniffer\Ruleset 
  36.      */
  37.     protected $ruleset = null;
  38.  
  39.     /**
  40.      * A list of ignore patterns that apply to directories only.
  41.      *
  42.      * @var array 
  43.      */
  44.     protected $ignoreDirPatterns = null;
  45.  
  46.     /**
  47.      * A list of ignore patterns that apply to files only.
  48.      *
  49.      * @var array 
  50.      */
  51.     protected $ignoreFilePatterns = null;
  52.  
  53.  
  54.     /**
  55.      * Constructs a filter.
  56.      *
  57.      * @param \RecursiveIterator       $iterator The iterator we are using to get file paths.
  58.      * @param string                   $basedir  The top-level path we are filtering.
  59.      * @param \PHP_CodeSniffer\Config  $config   The config data for the run.
  60.      * @param \PHP_CodeSniffer\Ruleset $ruleset  The ruleset used for the run.
  61.      *
  62.      * @return void 
  63.      */
  64.     public function __construct($iterator$basedirConfig $configRuleset $ruleset)
  65.     {
  66.         parent::__construct($iterator);
  67.         $this->basedir $basedir;
  68.         $this->config  $config;
  69.         $this->ruleset $ruleset;
  70.  
  71.     }//end __construct()
  72.  
  73.  
  74.     /**
  75.      * Check whether the current element of the iterator is acceptable.
  76.      *
  77.      * Files are checked for allowed extensions and ignore patterns.
  78.      * Directories are checked for ignore patterns only.
  79.      *
  80.      * @return bool 
  81.      */
  82.     public function accept()
  83.     {
  84.         $filePath = Util\Common::realpath($this->current());
  85.         if ($filePath === false{
  86.             return false;
  87.         }
  88.  
  89.         if (is_dir($filePath=== true{
  90.             if ($this->config->local === true{
  91.                 return false;
  92.             }
  93.         else if ($this->shouldProcessFile($filePath=== false{
  94.             return false;
  95.         }
  96.  
  97.         if ($this->shouldIgnorePath($filePath=== true{
  98.             return false;
  99.         }
  100.  
  101.         return true;
  102.  
  103.     }//end accept()
  104.  
  105.  
  106.     /**
  107.      * Returns an iterator for the current entry.
  108.      *
  109.      * Ensures that the ignore patterns are preserved so they don't have
  110.      * to be generated each time.
  111.      *
  112.      * @return \RecursiveIterator 
  113.      */
  114.     public function getChildren()
  115.     {
  116.         $children = new static(
  117.             new \RecursiveDirectoryIterator($this->current()(\RecursiveDirectoryIterator::SKIP_DOTS | \FilesystemIterator::FOLLOW_SYMLINKS)),
  118.             $this->basedir,
  119.             $this->config,
  120.             $this->ruleset
  121.         );
  122.  
  123.         // Set the ignore patterns so we don't have to generate them again.
  124.         $children->ignoreDirPatterns  = $this->ignoreDirPatterns;
  125.         $children->ignoreFilePatterns = $this->ignoreFilePatterns;
  126.         return $children;
  127.  
  128.     }//end getChildren()
  129.  
  130.  
  131.     /**
  132.      * Checks filtering rules to see if a file should be checked.
  133.      *
  134.      * Checks both file extension filters and path ignore filters.
  135.      *
  136.      * @param string $path The path to the file being checked.
  137.      *
  138.      * @return bool 
  139.      */
  140.     protected function shouldProcessFile($path)
  141.     {
  142.         // Check that the file's extension is one we are checking.
  143.         // We are strict about checking the extension and we don't
  144.         // let files through with no extension or that start with a dot.
  145.         $fileName  basename($path);
  146.         $fileParts explode('.'$fileName);
  147.         if ($fileParts[0=== $fileName || $fileParts[0=== ''{
  148.             return false;
  149.         }
  150.  
  151.         // Checking multi-part file extensions, so need to create a
  152.         // complete extension list and make sure one is allowed.
  153.         $extensions = array();
  154.         array_shift($fileParts);
  155.         foreach ($fileParts as $part{
  156.             $extensions[implode('.'$fileParts)= 1;
  157.             array_shift($fileParts);
  158.         }
  159.  
  160.         $matches array_intersect_key($extensions$this->config->extensions);
  161.         if (empty($matches=== true{
  162.             return false;
  163.         }
  164.  
  165.         return true;
  166.  
  167.     }//end shouldProcessFile()
  168.  
  169.  
  170.     /**
  171.      * Checks filtering rules to see if a path should be ignored.
  172.      *
  173.      * @param string $path The path to the file or directory being checked.
  174.      *
  175.      * @return bool 
  176.      */
  177.     protected function shouldIgnorePath($path)
  178.     {
  179.         if ($this->ignoreFilePatterns === null{
  180.             $this->ignoreDirPatterns  = array();
  181.             $this->ignoreFilePatterns = array();
  182.  
  183.             $ignorePatterns array_merge($this->config->ignored$this->ruleset->getIgnorePatterns());
  184.             foreach ($ignorePatterns as $pattern => $type{
  185.                 // If the ignore pattern ends with /* then it is ignoring an entire directory.
  186.                 if (substr($pattern-2=== '/*'{
  187.                     $this->ignoreDirPatterns[substr($pattern0-2)$type;
  188.                 else {
  189.                     $this->ignoreFilePatterns[$pattern$type;
  190.                 }
  191.             }
  192.         }
  193.  
  194.         $relativePath $path;
  195.         if (strpos($path$this->basedir=== 0{
  196.             // The +1 cuts off the directory separator as well.
  197.             $relativePath substr($path(strlen($this->basedir+ 1));
  198.         }
  199.  
  200.         if (is_dir($path=== true{
  201.             $ignorePatterns $this->ignoreDirPatterns;
  202.         else {
  203.             $ignorePatterns $this->ignoreFilePatterns;
  204.         }
  205.  
  206.         foreach ($ignorePatterns as $pattern => $type{
  207.             // Maintains backwards compatibility in case the ignore pattern does
  208.             // not have a relative/absolute value.
  209.             if (is_int($pattern=== true{
  210.                 $pattern $type;
  211.                 $type    'absolute';
  212.             }
  213.  
  214.             $replacements = array(
  215.                              '\\,' => ',',
  216.                              '*'   => '.*',
  217.                             );
  218.  
  219.             // We assume a / directory separator, as do the exclude rules
  220.             // most developers write, so we need a special case for any system
  221.             // that is different.
  222.             if (DIRECTORY_SEPARATOR === '\\'{
  223.                 $replacements['/''\\\\';
  224.             }
  225.  
  226.             $pattern strtr($pattern$replacements);
  227.  
  228.             if ($type === 'relative'{
  229.                 $testPath $relativePath;
  230.             else {
  231.                 $testPath $path;
  232.             }
  233.  
  234.             $pattern '`'.$pattern.'`i';
  235.             if (preg_match($pattern$testPath=== 1{
  236.                 return true;
  237.             }
  238.         }//end foreach
  239.  
  240.         return false;
  241.  
  242.     }//end shouldIgnorePath()
  243.  
  244.  
  245. }//end class

Documentation generated on Mon, 11 Mar 2019 15:27:28 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.