getIgnorePatterns() public method

Optionally takes a listener to get ignore patterns specified for that sniff only.
public getIgnorePatterns ( string $listener = null ) : array
$listener string The listener to get patterns for. If NULL, all patterns are returned.
return array
Beispiel #1
0
 /**
  * Runs PHP_CodeSniffer over files and directories.
  *
  * @param array $values An array of values determined from CLI args.
  *
  * @return int The number of error and warning messages shown.
  * @see    getCommandLineValues()
  */
 public function process($values = array())
 {
     if (empty($values) === true) {
         $values = $this->getCommandLineValues();
     } else {
         $values = array_merge($this->getDefaults(), $values);
         $this->values = $values;
     }
     if ($values['generator'] !== '') {
         $phpcs = new PHP_CodeSniffer($values['verbosity']);
         if ($values['standard'] === null) {
             $values['standard'] = $this->validateStandard(null);
         }
         foreach ($values['standard'] as $standard) {
             $phpcs->generateDocs($standard, $values['sniffs'], $values['generator']);
         }
         exit(0);
     }
     // If no standard is supplied, get the default.
     $values['standard'] = $this->validateStandard($values['standard']);
     foreach ($values['standard'] as $standard) {
         if (PHP_CodeSniffer::isInstalledStandard($standard) === false) {
             // They didn't select a valid coding standard, so help them
             // out by letting them know which standards are installed.
             echo 'ERROR: the "' . $standard . '" coding standard is not installed. ';
             $this->printInstalledStandards();
             exit(2);
         }
     }
     if ($values['explain'] === true) {
         foreach ($values['standard'] as $standard) {
             $this->explainStandard($standard);
         }
         exit(0);
     }
     $phpcs = new PHP_CodeSniffer($values['verbosity'], null, null, null);
     $phpcs->setCli($this);
     $phpcs->initStandard($values['standard'], $values['sniffs']);
     $values = $this->values;
     $phpcs->setTabWidth($values['tabWidth']);
     $phpcs->setEncoding($values['encoding']);
     $phpcs->setInteractive($values['interactive']);
     // Set file extensions if they were specified. Otherwise,
     // let PHP_CodeSniffer decide on the defaults.
     if (empty($values['extensions']) === false) {
         $phpcs->setAllowedFileExtensions($values['extensions']);
     }
     // Set ignore patterns if they were specified.
     if (empty($values['ignored']) === false) {
         $ignorePatterns = array_merge($phpcs->getIgnorePatterns(), $values['ignored']);
         $phpcs->setIgnorePatterns($ignorePatterns);
     }
     // Set some convenience member vars.
     if ($values['errorSeverity'] === null) {
         $this->errorSeverity = PHPCS_DEFAULT_ERROR_SEV;
     } else {
         $this->errorSeverity = $values['errorSeverity'];
     }
     if ($values['warningSeverity'] === null) {
         $this->warningSeverity = PHPCS_DEFAULT_WARN_SEV;
     } else {
         $this->warningSeverity = $values['warningSeverity'];
     }
     if (empty($values['reports']) === true) {
         $values['reports']['full'] = $values['reportFile'];
         $this->values['reports'] = $values['reports'];
     }
     // Include bootstrap files.
     foreach ($values['bootstrap'] as $bootstrap) {
         include $bootstrap;
     }
     $phpcs->processFiles($values['files'], $values['local']);
     if (empty($values['files']) === true || $values['stdin'] !== null) {
         $fileContents = $values['stdin'];
         if ($fileContents === null) {
             // Check if they are passing in the file contents.
             $handle = fopen('php://stdin', 'r');
             stream_set_blocking($handle, true);
             $fileContents = stream_get_contents($handle);
             fclose($handle);
         }
         if ($fileContents === '') {
             // No files and no content passed in.
             echo 'ERROR: You must supply at least one file or directory to process.' . PHP_EOL . PHP_EOL;
             $this->printUsage();
             exit(2);
         } else {
             $phpcs->processFile('STDIN', $fileContents);
         }
     }
     // Interactive runs don't require a final report and it doesn't really
     // matter what the retun value is because we know it isn't being read
     // by a script.
     if ($values['interactive'] === true) {
         return 0;
     }
     return $this->printErrorReport($phpcs, $values['reports'], $values['showSources'], $values['reportFile'], $values['reportWidth']);
 }
Beispiel #2
0
 /**
  * Adds an warning to the warning stack.
  *
  * @param string $warning  The error message.
  * @param int    $stackPtr The stack position where the error occurred.
  * @param string $code     A violation code unique to the sniff message.
  * @param array  $data     Replacements for the warning message.
  * @param int    $severity The severity level for this warning. A value of 0
  *                         will be converted into the default severity level.
  *
  * @return void
  */
 public function addWarning($warning, $stackPtr, $code = '', $data = array(), $severity = 0)
 {
     // Don't bother doing any processing if warnings are just going to
     // be hidden in the reports anyway.
     if ($this->phpcs->cli->warningSeverity === 0) {
         return;
     }
     // Work out which sniff generated the warning.
     if (substr($code, 0, 9) === 'Internal.') {
         // Any internal message.
         $sniff = $code;
     } else {
         $parts = explode('_', $this->_activeListener);
         if (isset($parts[3]) === true) {
             $sniff = $parts[0] . '.' . $parts[2] . '.' . $parts[3];
             // Remove "Sniff" from the end.
             $sniff = substr($sniff, 0, -5);
         } else {
             $sniff = 'unknownSniff';
         }
         if ($code !== '') {
             $sniff .= '.' . $code;
         }
     }
     // Make sure this message type has not been set to "error".
     if (isset($this->ruleset[$sniff]['type']) === true && $this->ruleset[$sniff]['type'] === 'error') {
         // Pass this off to the error handler.
         $this->addError($warning, $stackPtr, $code, $data, $severity);
         return;
     }
     // Make sure we are interested in this severity level.
     if (isset($this->ruleset[$sniff]['severity']) === true) {
         $severity = $this->ruleset[$sniff]['severity'];
     } else {
         if ($severity === 0) {
             $severity = PHPCS_DEFAULT_WARN_SEV;
         }
     }
     if ($this->phpcs->cli->warningSeverity > $severity) {
         return;
     }
     // Make sure we are not ignoring this file.
     $patterns = $this->phpcs->getIgnorePatterns($sniff);
     foreach ($patterns as $pattern => $type) {
         // While there is support for a type of each pattern
         // (absolute or relative) we don't actually support it here.
         $replacements = array('\\,' => ',', '*' => '.*');
         $pattern = strtr($pattern, $replacements);
         if (preg_match("|{$pattern}|i", $this->_file) === 1) {
             return;
         }
     }
     if ($stackPtr === null) {
         $lineNum = 1;
         $column = 1;
     } else {
         $lineNum = $this->_tokens[$stackPtr]['line'];
         $column = $this->_tokens[$stackPtr]['column'];
     }
     $this->_warningCount++;
     if ($this->_recordErrors === false) {
         if (isset($this->_warnings[$lineNum]) === false) {
             $this->_warnings[$lineNum] = 0;
         }
         $this->_warnings[$lineNum]++;
         return;
     }
     // Work out the warning message.
     if (isset($this->ruleset[$sniff]['message']) === true) {
         $warning = $this->ruleset[$sniff]['message'];
     }
     if (empty($data) === true) {
         $message = $warning;
     } else {
         $message = vsprintf($warning, $data);
     }
     if (isset($this->_warnings[$lineNum]) === false) {
         $this->_warnings[$lineNum] = array();
     }
     if (isset($this->_warnings[$lineNum][$column]) === false) {
         $this->_warnings[$lineNum][$column] = array();
     }
     $this->_warnings[$lineNum][$column][] = array('message' => $message, 'source' => $sniff, 'severity' => $severity);
 }
Beispiel #3
0
 /**
  * Adds an warning to the warning stack.
  *
  * @param string  $warning  The error message.
  * @param int     $line     The line on which the warning occurred.
  * @param int     $column   The column at which the warning occurred.
  * @param string  $code     A violation code unique to the sniff message.
  * @param array   $data     Replacements for the warning message.
  * @param int     $severity The severity level for this warning. A value of 0
  *                          will be converted into the default severity level.
  * @param boolean $fixable  Can the warning be fixed by the sniff?
  *
  * @return boolean
  */
 private function _addWarning($warning, $line, $column, $code, $data, $severity, $fixable)
 {
     if (isset(self::$_ignoredLines[$line]) === true) {
         return false;
     }
     // Work out which sniff generated the warning.
     if (substr($code, 0, 9) === 'Internal.') {
         // Any internal message.
         $sniff = $code;
         $sniffCode = $code;
     } else {
         $parts = explode('_', str_replace('\\', '_', $this->_activeListener));
         if (isset($parts[3]) === true) {
             $sniff = $parts[0] . '.' . $parts[2] . '.' . $parts[3];
             // Remove "Sniff" from the end.
             $sniff = substr($sniff, 0, -5);
         } else {
             $sniff = 'unknownSniff';
         }
         $sniffCode = $sniff;
         if ($code !== '') {
             $sniffCode .= '.' . $code;
         }
     }
     //end if
     // If we know this sniff code is being ignored for this file, return early.
     if (isset($this->_ignoredCodes[$sniffCode]) === true) {
         return false;
     }
     // Make sure this message type has not been set to "error".
     if (isset($this->ruleset[$sniffCode]['type']) === true && $this->ruleset[$sniffCode]['type'] === 'error') {
         // Pass this off to the error handler.
         return $this->_addError($warning, $line, $column, $code, $data, $severity, $fixable);
     } else {
         if ($this->phpcs->cli->warningSeverity === 0) {
             // Don't bother doing any processing as warnings are just going to
             // be hidden in the reports anyway.
             return false;
         }
     }
     // Make sure we are interested in this severity level.
     if (isset($this->ruleset[$sniffCode]['severity']) === true) {
         $severity = $this->ruleset[$sniffCode]['severity'];
     } else {
         if ($severity === 0) {
             $severity = PHPCS_DEFAULT_WARN_SEV;
         }
     }
     if ($this->phpcs->cli->warningSeverity > $severity) {
         return false;
     }
     // Make sure we are not ignoring this file.
     $patterns = $this->phpcs->getIgnorePatterns($sniffCode);
     foreach ($patterns as $pattern => $type) {
         // While there is support for a type of each pattern
         // (absolute or relative) we don't actually support it here.
         $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) . '`i';
         if (preg_match($pattern, $this->_file) === 1) {
             $this->_ignoredCodes[$sniffCode] = true;
             return false;
         }
     }
     //end foreach
     $this->_warningCount++;
     if ($fixable === true) {
         $this->_fixableCount++;
     }
     if ($this->_recordErrors === false) {
         if (isset($this->_warnings[$line]) === false) {
             $this->_warnings[$line] = 0;
         }
         $this->_warnings[$line]++;
         return true;
     }
     // Work out the warning message.
     if (isset($this->ruleset[$sniffCode]['message']) === true) {
         $warning = $this->ruleset[$sniffCode]['message'];
     }
     if (empty($data) === true) {
         $message = $warning;
     } else {
         $message = vsprintf($warning, $data);
     }
     if (isset($this->_warnings[$line]) === false) {
         $this->_warnings[$line] = array();
     }
     if (isset($this->_warnings[$line][$column]) === false) {
         $this->_warnings[$line][$column] = array();
     }
     $this->_warnings[$line][$column][] = array('message' => $message, 'source' => $sniffCode, 'severity' => $severity, 'fixable' => $fixable);
     if (PHP_CODESNIFFER_VERBOSITY > 1 && $this->fixer->enabled === true && $fixable === true) {
         @ob_end_clean();
         echo "\tW: {$message} ({$sniffCode})" . PHP_EOL;
         ob_start();
     }
     return true;
 }