PHP_CodeSniffer_File::getMethodParameters PHP Method

getMethodParameters() public method

Each parameter is in the following format: 0 => array( 'name' => '$var', // The variable name. 'pass_by_reference' => false, // Passed by reference. 'type_hint' => string, // Type hint for array or custom type ) Parameters with default values have an additional array index of 'default' with the value of the default as a string.
public getMethodParameters ( integer $stackPtr ) : array
$stackPtr integer The position in the stack of the T_FUNCTION token to acquire the parameters for.
return array
    public function getMethodParameters($stackPtr)
    {
        if ($this->_tokens[$stackPtr]['code'] !== T_FUNCTION) {
            throw new PHP_CodeSniffer_Exception('$stackPtr must be of type T_FUNCTION');
        }
        $opener = $this->_tokens[$stackPtr]['parenthesis_opener'];
        $closer = $this->_tokens[$stackPtr]['parenthesis_closer'];
        $vars = array();
        $currVar = null;
        $defaultStart = null;
        $paramCount = 0;
        $passByReference = false;
        $variableLength = false;
        $typeHint = '';
        for ($i = $opener + 1; $i <= $closer; $i++) {
            // Check to see if this token has a parenthesis or bracket opener. If it does
            // it's likely to be an array which might have arguments in it. This
            // could cause problems in our parsing below, so lets just skip to the
            // end of it.
            if (isset($this->_tokens[$i]['parenthesis_opener']) === true) {
                // Don't do this if it's the close parenthesis for the method.
                if ($i !== $this->_tokens[$i]['parenthesis_closer']) {
                    $i = $this->_tokens[$i]['parenthesis_closer'] + 1;
                }
            }
            if (isset($this->_tokens[$i]['bracket_opener']) === true) {
                // Don't do this if it's the close parenthesis for the method.
                if ($i !== $this->_tokens[$i]['bracket_closer']) {
                    $i = $this->_tokens[$i]['bracket_closer'] + 1;
                }
            }
            switch ($this->_tokens[$i]['code']) {
                case T_BITWISE_AND:
                    $passByReference = true;
                    break;
                case T_VARIABLE:
                    $currVar = $i;
                    break;
                case T_ELLIPSIS:
                    $variableLength = true;
                    break;
                case T_ARRAY_HINT:
                case T_CALLABLE:
                    $typeHint = $this->_tokens[$i]['content'];
                    break;
                case T_STRING:
                    // This is a string, so it may be a type hint, but it could
                    // also be a constant used as a default value.
                    $prevComma = false;
                    for ($t = $i; $t >= $opener; $t--) {
                        if ($this->_tokens[$t]['code'] === T_COMMA) {
                            $prevComma = $t;
                            break;
                        }
                    }
                    if ($prevComma !== false) {
                        $nextEquals = false;
                        for ($t = $prevComma; $t < $i; $t++) {
                            if ($this->_tokens[$t]['code'] === T_EQUAL) {
                                $nextEquals = $t;
                                break;
                            }
                        }
                        if ($nextEquals !== false) {
                            break;
                        }
                    }
                    if ($defaultStart === null) {
                        $typeHint .= $this->_tokens[$i]['content'];
                    }
                    break;
                case T_NS_SEPARATOR:
                    // Part of a type hint or default value.
                    if ($defaultStart === null) {
                        $typeHint .= $this->_tokens[$i]['content'];
                    }
                    break;
                case T_CLOSE_PARENTHESIS:
                case T_COMMA:
                    // If it's null, then there must be no parameters for this
                    // method.
                    if ($currVar === null) {
                        continue;
                    }
                    $vars[$paramCount] = array();
                    $vars[$paramCount]['name'] = $this->_tokens[$currVar]['content'];
                    if ($defaultStart !== null) {
                        $vars[$paramCount]['default'] = $this->getTokensAsString($defaultStart, $i - $defaultStart);
                    }
                    $vars[$paramCount]['pass_by_reference'] = $passByReference;
                    $vars[$paramCount]['variable_length'] = $variableLength;
                    $vars[$paramCount]['type_hint'] = $typeHint;
                    // Reset the vars, as we are about to process the next parameter.
                    $defaultStart = null;
                    $passByReference = false;
                    $variableLength = false;
                    $typeHint = '';
                    $paramCount++;
                    break;
                case T_EQUAL:
                    $defaultStart = $i + 1;
                    break;
            }
            //end switch
        }
        //end for
        return $vars;
    }

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
  */
 public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
 {
     foreach ($phpcsFile->getMethodParameters($stackPtr) as $param) {
         if (in_array($param, $this->forbiddenParameterNames) === true) {
             $error = "[PHP 5.4] {$param} is not a valid parameter name.";
             $phpcsFile->addError($error, $stackPtr);
         }
     }
 }
All Usage Examples Of PHP_CodeSniffer_File::getMethodParameters