PHP_CodeSniffer_File::getClassProperties PHP Метод

getClassProperties() публичный Метод

The format of the array is: array( 'is_abstract' => false, // true if the abstract keyword was found. 'is_final' => false, // true if the final keyword was found. );
public getClassProperties ( integer $stackPtr ) : array
$stackPtr integer The position in the stack of the T_CLASS token to acquire the properties for.
Результат array
    public function getClassProperties($stackPtr)
    {
        if ($this->_tokens[$stackPtr]['code'] !== T_CLASS) {
            throw new PHP_CodeSniffer_Exception('$stackPtr must be of type T_CLASS');
        }
        $valid = array(T_FINAL => T_FINAL, T_ABSTRACT => T_ABSTRACT, T_WHITESPACE => T_WHITESPACE, T_COMMENT => T_COMMENT, T_DOC_COMMENT => T_DOC_COMMENT);
        $isAbstract = false;
        $isFinal = false;
        for ($i = $stackPtr - 1; $i > 0; $i--) {
            if (isset($valid[$this->_tokens[$i]['code']]) === false) {
                break;
            }
            switch ($this->_tokens[$i]['code']) {
                case T_ABSTRACT:
                    $isAbstract = true;
                    break;
                case T_FINAL:
                    $isFinal = true;
                    break;
            }
        }
        //end for
        return array('is_abstract' => $isAbstract, 'is_final' => $isFinal);
    }

Usage Example

 /**
  * Processes this test, when one of its tokens is encountered.
  *
  * @param PHP_CodeSniffer_File $phpcsFile The current file being processed.
  * @param int                  $stackPtr  The position of the current token
  *                                        in the stack passed in $tokens.
  *
  * @return void
  */
 public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
 {
     $tokens = $phpcsFile->getTokens();
     $className = $phpcsFile->findNext(T_STRING, $stackPtr);
     $name = trim($tokens[$className]['content']);
     $errorData = array(ucfirst($tokens[$stackPtr]['content']));
     $classProperties = $phpcsFile->getClassProperties($stackPtr);
     $startsWithAbstract = substr($name, 0, 8) == 'Abstract';
     if ($classProperties['is_abstract'] && !$startsWithAbstract) {
         $error = '%s name must begin with "Abstract"';
         $phpcsFile->addError($error, $stackPtr, 'StartWithAbstract', $errorData);
     }
     if (!$classProperties['is_abstract'] && $startsWithAbstract) {
         $error = '%s name must not begin with "Abstract"';
         $phpcsFile->addError($error, $stackPtr, 'StartNotWithAbstract', $errorData);
     }
 }
All Usage Examples Of PHP_CodeSniffer_File::getClassProperties