PHP_CodeSniffer_File::getMethodProperties PHP Method

getMethodProperties() public method

The format of the array is: array( 'scope' => 'public', // public private or protected 'scope_specified' => true, // true is scope keyword was found. 'is_abstract' => false, // true if the abstract keyword was found. 'is_final' => false, // true if the final keyword was found. 'is_static' => false, // true if the static keyword was found. 'is_closure' => false, // true if no name is found. );
public getMethodProperties ( integer $stackPtr ) : array
$stackPtr integer The position in the stack of the T_FUNCTION token to acquire the properties for.
return array
    public function getMethodProperties($stackPtr)
    {
        if ($this->_tokens[$stackPtr]['code'] !== T_FUNCTION) {
            throw new PHP_CodeSniffer_Exception('$stackPtr must be of type T_FUNCTION');
        }
        $valid = array(T_PUBLIC => T_PUBLIC, T_PRIVATE => T_PRIVATE, T_PROTECTED => T_PROTECTED, T_STATIC => T_STATIC, T_FINAL => T_FINAL, T_ABSTRACT => T_ABSTRACT, T_WHITESPACE => T_WHITESPACE, T_COMMENT => T_COMMENT, T_DOC_COMMENT => T_DOC_COMMENT);
        $scope = 'public';
        $scopeSpecified = false;
        $isAbstract = false;
        $isFinal = false;
        $isStatic = false;
        $isClosure = $this->isAnonymousFunction($stackPtr);
        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_ABSTRACT:
                    $isAbstract = true;
                    break;
                case T_FINAL:
                    $isFinal = true;
                    break;
                case T_STATIC:
                    $isStatic = true;
                    break;
            }
            //end switch
        }
        //end for
        return array('scope' => $scope, 'scope_specified' => $scopeSpecified, 'is_abstract' => $isAbstract, 'is_final' => $isFinal, 'is_static' => $isStatic, 'is_closure' => $isClosure);
    }

Usage Example

 /**
  * Processes the tokens within the scope.
  *
  * @param PHP_CodeSniffer_File $phpcsFile The file being processed.
  * @param int                  $stackPtr  The position where this token was
  *                                        found.
  * @param int                  $currScope The position of the current scope.
  *
  * @return void
  */
 protected function processTokenWithinScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $currScope)
 {
     $methodName = $phpcsFile->getDeclarationName($stackPtr);
     if ($methodName === null) {
         // Ignore closures.
         return;
     }
     $className = $phpcsFile->getDeclarationName($currScope);
     $errorData = array($className . '::' . $methodName);
     // Is this a magic method. IE. is prefixed with "__".
     if (preg_match('|^__|', $methodName) !== 0) {
         $magicPart = substr($methodName, 2);
         if (in_array($magicPart, $this->magicMethods) === false) {
             $error = 'Method name "%s" is invalid; only PHP magic methods should be prefixed with a double underscore';
             $phpcsFile->addError($error, $stackPtr, 'MethodDoubleUnderscore', $errorData);
         }
         return;
     }
     $methodProps = $phpcsFile->getMethodProperties($stackPtr);
     $scope = $methodProps['scope'];
     $scopeSpecified = $methodProps['scope_specified'];
     // Methods should not contain underscores.
     if (strpos($methodName, '_') !== false) {
         if ($scopeSpecified === true) {
             $error = '%s method name "%s" is not in lowerCamel format, it must not contain underscores';
             $data = array(ucfirst($scope), $errorData[0]);
             $phpcsFile->addError($error, $stackPtr, 'ScopeNotLowerCamel', $data);
         } else {
             $error = 'Method name "%s" is not in lowerCamel format, it must not contain underscores';
             $phpcsFile->addError($error, $stackPtr, 'NotLowerCamel', $errorData);
         }
     }
 }
All Usage Examples Of PHP_CodeSniffer_File::getMethodProperties