Source for file Filter.php
Documentation is available at Filter.php
* A base filter class for filtering out files and folders during a run.
* @author Greg Sherwood <gsherwood@squiz.net>
* @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
namespace PHP_CodeSniffer\Filters;
use PHP_CodeSniffer\Util;
use PHP_CodeSniffer\Ruleset;
use PHP_CodeSniffer\Config;
class Filter extends \RecursiveFilterIterator
* The top-level path we are filtering.
protected $basedir = null;
* The config data for the run.
* @var \PHP_CodeSniffer\Config
protected $config = null;
* The ruleset used for the run.
* @var \PHP_CodeSniffer\Ruleset
protected $ruleset = null;
* A list of ignore patterns that apply to directories only.
protected $ignoreDirPatterns = null;
* A list of ignore patterns that apply to files only.
protected $ignoreFilePatterns = null;
* @param \RecursiveIterator $iterator The iterator we are using to get file paths.
* @param string $basedir The top-level path we are filtering.
* @param \PHP_CodeSniffer\Config $config The config data for the run.
* @param \PHP_CodeSniffer\Ruleset $ruleset The ruleset used for the run.
public function __construct ($iterator, $basedir, Config $config, Ruleset $ruleset)
parent ::__construct ($iterator);
$this->basedir = $basedir;
$this->ruleset = $ruleset;
* Check whether the current element of the iterator is acceptable.
* Files are checked for allowed extensions and ignore patterns.
* Directories are checked for ignore patterns only.
$filePath = Util\Common ::realpath ($this->current ());
if ($filePath === false ) {
if (is_dir($filePath) === true ) {
if ($this->config->local === true ) {
} else if ($this->shouldProcessFile ($filePath) === false ) {
if ($this->shouldIgnorePath ($filePath) === true ) {
* Returns an iterator for the current entry.
* Ensures that the ignore patterns are preserved so they don't have
* to be generated each time.
* @return \RecursiveIterator
public function getChildren ()
new \RecursiveDirectoryIterator ($this->current (), (\RecursiveDirectoryIterator ::SKIP_DOTS | \FilesystemIterator ::FOLLOW_SYMLINKS )),
// Set the ignore patterns so we don't have to generate them again.
$children->ignoreDirPatterns = $this->ignoreDirPatterns;
$children->ignoreFilePatterns = $this->ignoreFilePatterns;
* Checks filtering rules to see if a file should be checked.
* Checks both file extension filters and path ignore filters.
* @param string $path The path to the file being checked.
protected function shouldProcessFile ($path)
// Check that the file's extension is one we are checking.
// We are strict about checking the extension and we don't
// let files through with no extension or that start with a dot.
$fileParts = explode('.', $fileName);
if ($fileParts[0 ] === $fileName || $fileParts[0 ] === '') {
// Checking multi-part file extensions, so need to create a
// complete extension list and make sure one is allowed.
foreach ($fileParts as $part) {
$extensions[implode('.', $fileParts)] = 1;
if (empty ($matches) === true ) {
}//end shouldProcessFile()
* Checks filtering rules to see if a path should be ignored.
* @param string $path The path to the file or directory being checked.
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;
$this->ignoreFilePatterns[$pattern] = $type;
if (strpos($path, $this->basedir) === 0 ) {
// The +1 cuts off the directory separator as well.
$relativePath = substr($path, (strlen($this->basedir) + 1 ));
$ignorePatterns = $this->ignoreDirPatterns;
$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 ) {
// We assume a / directory separator, as do the exclude rules
// most developers write, so we need a special case for any system
if (DIRECTORY_SEPARATOR === '\\') {
$replacements['/'] = '\\\\';
$pattern = strtr($pattern, $replacements);
if ($type === 'relative') {
$testPath = $relativePath;
$pattern = '`'. $pattern. '`i';
}//end shouldIgnorePath()
Documentation generated on Mon, 11 Mar 2019 15:27:28 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|