PHP_CodeSniffer_File::getMemberProperties PHP Method

getMemberProperties() public method

The format of the array is: array( 'scope' => 'public', // public private or protected 'is_static' => false, // true if the static keyword was found. );
public getMemberProperties ( integer $stackPtr ) : array
$stackPtr integer The position in the stack of the T_VARIABLE token to acquire the properties for.
return array
    public function getMemberProperties($stackPtr)
    {
        if ($this->_tokens[$stackPtr]['code'] !== T_VARIABLE) {
            throw new PHP_CodeSniffer_Exception('$stackPtr must be of type T_VARIABLE');
        }
        $conditions = array_keys($this->_tokens[$stackPtr]['conditions']);
        $ptr = array_pop($conditions);
        if (isset($this->_tokens[$ptr]) === false || $this->_tokens[$ptr]['code'] !== T_CLASS && $this->_tokens[$ptr]['code'] !== T_TRAIT) {
            if (isset($this->_tokens[$ptr]) === true && $this->_tokens[$ptr]['code'] === T_INTERFACE) {
                // T_VARIABLEs in interfaces can actually be method arguments
                // but they wont be seen as being inside the method because there
                // are no scope openers and closers for abstract methods. If it is in
                // parentheses, we can be pretty sure it is a method argument.
                if (isset($this->_tokens[$stackPtr]['nested_parenthesis']) === false || empty($this->_tokens[$stackPtr]['nested_parenthesis']) === true) {
                    $error = 'Possible parse error: interfaces may not include member vars';
                    $this->addWarning($error, $stackPtr, 'Internal.ParseError.InterfaceHasMemberVar');
                    return array();
                }
            } else {
                throw new PHP_CodeSniffer_Exception('$stackPtr is not a class member var');
            }
        }
        $valid = array(T_PUBLIC => T_PUBLIC, T_PRIVATE => T_PRIVATE, T_PROTECTED => T_PROTECTED, T_STATIC => T_STATIC, T_WHITESPACE => T_WHITESPACE, T_COMMENT => T_COMMENT, T_DOC_COMMENT => T_DOC_COMMENT, T_VARIABLE => T_VARIABLE, T_COMMA => T_COMMA);
        $scope = 'public';
        $scopeSpecified = false;
        $isStatic = false;
        for ($i = $stackPtr - 1; $i > 0; $i--) {
            if (isset($valid[$this->_tokens[$i]['code']]) === false) {
                break;
            }
            switch ($this->_tokens[$i]['code']) {
                case T_PUBLIC:
                    $scope = 'public';
                    $scopeSpecified = true;
                    break;
                case T_PRIVATE:
                    $scope = 'private';
                    $scopeSpecified = true;
                    break;
                case T_PROTECTED:
                    $scope = 'protected';
                    $scopeSpecified = true;
                    break;
                case T_STATIC:
                    $isStatic = true;
                    break;
            }
        }
        //end for
        return array('scope' => $scope, 'scope_specified' => $scopeSpecified, 'is_static' => $isStatic);
    }

Usage Example

 /**
  * @param PHP_CodeSniffer_File $phpcsFile
  * @param int $stackPtr
  * @return void
  */
 protected function processMemberVar(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
 {
     $tokens = $phpcsFile->getTokens();
     $varName = ltrim($tokens[$stackPtr]['content'], '$');
     $memberProps = $phpcsFile->getMemberProperties($stackPtr);
     $public = $memberProps['scope'] === 'public';
     if ($public === TRUE) {
         if (substr($varName, 0, 1) === '_') {
             $error = 'Public member variable "%s" must not contain a leading underscore';
             $data = array($varName);
             $phpcsFile->addError($error, $stackPtr, 'PublicHasUnderscore', $data);
             return;
         }
     } else {
         // private
         return;
         // if (substr($varName, 0, 1) !== '_') {
         // 	$scope = ucfirst($memberProps['scope']);
         // 	$error = '%s member variable "%s" must contain a leading underscore';
         // 	$data  = array(
         // 		$scope,
         // 		$varName,
         // 	);
         // 	$phpcsFile->addError($error, $stackPtr, 'PrivateNoUnderscore', $data);
         // 	return;
         // }
     }
     if (PHP_CodeSniffer::isCamelCaps($varName, FALSE, $public, FALSE) === FALSE) {
         $error = 'Variable "%s" is not in valid camel caps format';
         $data = array($varName);
         $phpcsFile->addError($error, $stackPtr, 'MemberVarNotCamelCaps', $data);
     }
 }
All Usage Examples Of PHP_CodeSniffer_File::getMemberProperties