PHP_CodeSniffer_File::hasCondition PHP Method

hasCondition() public method

Determine if the passed token has a condition of one of the passed types.
public hasCondition ( integer $stackPtr, integer | array $types ) : boolean
$stackPtr integer The position of the token we are checking.
$types integer | array The type(s) of tokens to search for.
return boolean
    public function hasCondition($stackPtr, $types)
    {
        // Check for the existence of the token.
        if (isset($this->_tokens[$stackPtr]) === false) {
            return false;
        }
        // Make sure the token has conditions.
        if (isset($this->_tokens[$stackPtr]['conditions']) === false) {
            return false;
        }
        $types = (array) $types;
        $conditions = $this->_tokens[$stackPtr]['conditions'];
        foreach ($types as $type) {
            if (in_array($type, $conditions) === true) {
                // We found a token with the required type.
                return true;
            }
        }
        return false;
    }

Usage Example

 /**
  * Processes this test, when one of its tokens is encountered.
  *
  * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
  * @param int                  $stackPtr  The position of the current token in the
  *                                        stack passed in $tokens.
  *
  * @return void
  */
 protected function processVariable(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
 {
     $tokens = $phpcsFile->getTokens();
     $varName = ltrim($tokens[$stackPtr]['content'], '$');
     $phpReservedVars = array('_SERVER', '_GET', '_POST', '_REQUEST', '_SESSION', '_ENV', '_COOKIE', '_FILES', 'GLOBALS', 'http_response_header', 'HTTP_RAW_POST_DATA', 'php_errormsg');
     // If it's a php reserved var, then its ok.
     if (in_array($varName, $phpReservedVars) === true) {
         return;
     }
     // There is no way for us to know if the var is public or private,
     // so we have to ignore a leading underscore if there is one and just
     // check the main part of the variable name.
     $originalVarName = $varName;
     if (substr($varName, 0, 1) === '_') {
         $objOperator = $phpcsFile->findPrevious(array(T_WHITESPACE), $stackPtr - 1, null, true);
         if ($tokens[$objOperator]['code'] === T_DOUBLE_COLON) {
             // The variable lives within a class, and is referenced like
             // this: MyClass::$_variable, so we don't know its scope.
             $inClass = true;
         } else {
             $inClass = $phpcsFile->hasCondition($stackPtr, array(T_CLASS, T_INTERFACE, T_TRAIT));
         }
         if ($inClass === true) {
             $varName = substr($varName, 1);
         }
     }
     if (PHP_CodeSniffer::isCamelCaps($varName, false, true, false) === false) {
         $error = 'Variable "%s" is not in valid camel caps format';
         $data = array($originalVarName);
         $phpcsFile->addError($error, $stackPtr, 'NotCamelCaps', $data);
     }
 }
All Usage Examples Of PHP_CodeSniffer_File::hasCondition