Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. File.php

function File::getMethodParameters

Returns the method parameters for the specified function token.

Also supports passing in a USE token for a closure use group.

Each parameter is in the following format:

<code> 0 => array( 'name' => string, // The variable name. 'token' => integer, // The stack pointer to the variable name. 'content' => string, // The full content of the variable definition. 'has_attributes' => boolean, // Does the parameter have one or more attributes attached ? 'pass_by_reference' => boolean, // Is the variable passed by reference? 'reference_token' => integer|false, // The stack pointer to the reference operator // or FALSE if the param is not passed by reference. 'variable_length' => boolean, // Is the param of variable length through use of `...` ? 'variadic_token' => integer|false, // The stack pointer to the ... operator // or FALSE if the param is not variable length. 'type_hint' => string, // The type hint for the variable. 'type_hint_token' => integer|false, // The stack pointer to the start of the type hint // or FALSE if there is no type hint. 'type_hint_end_token' => integer|false, // The stack pointer to the end of the type hint // or FALSE if there is no type hint. 'nullable_type' => boolean, // TRUE if the type is preceded by the nullability // operator. 'comma_token' => integer|false, // The stack pointer to the comma after the param // or FALSE if this is the last param. ) </code>

Parameters with default values have additional array indexes of: 'default' => string, // The full content of the default value. 'default_token' => integer, // The stack pointer to the start of the default value. 'default_equal_token' => integer, // The stack pointer to the equals sign.

Parameters declared using PHP 8 constructor property promotion, have these additional array indexes: 'property_visibility' => string, // The property visibility as declared. 'visibility_token' => integer|false, // The stack pointer to the visibility modifier token // or FALSE if the visibility is not explicitly declared. 'property_readonly' => boolean, // TRUE if the readonly keyword was found. 'readonly_token' => integer, // The stack pointer to the readonly modifier token. // This index will only be set if the property is readonly.

Parameters

int $stackPtr The position in the stack of the function token: to acquire the parameters for.

Return value

array

Throws

\PHP_CodeSniffer\Exceptions\RuntimeException If the specified $stackPtr is not of type T_FUNCTION, T_CLOSURE, T_USE, or T_FN.

1 call to File::getMethodParameters()
File::isReference in vendor/squizlabs/php_codesniffer/src/Files/File.php
Determine if the passed token is a reference operator.

File

vendor/squizlabs/php_codesniffer/src/Files/File.php, line 1372

Class

File

Namespace

PHP_CodeSniffer\Files

Code

public function getMethodParameters($stackPtr) {
    if ($this->tokens[$stackPtr]['code'] !== T_FUNCTION && $this->tokens[$stackPtr]['code'] !== T_CLOSURE && $this->tokens[$stackPtr]['code'] !== T_USE && $this->tokens[$stackPtr]['code'] !== T_FN) {
        throw new RuntimeException('$stackPtr must be of type T_FUNCTION or T_CLOSURE or T_USE or T_FN');
    }
    if ($this->tokens[$stackPtr]['code'] === T_USE) {
        $opener = $this->findNext(T_OPEN_PARENTHESIS, $stackPtr + 1);
        if ($opener === false || isset($this->tokens[$opener]['parenthesis_owner']) === true) {
            throw new RuntimeException('$stackPtr was not a valid T_USE');
        }
    }
    else {
        if (isset($this->tokens[$stackPtr]['parenthesis_opener']) === false) {
            // Live coding or syntax error, so no params to find.
            return [];
        }
        $opener = $this->tokens[$stackPtr]['parenthesis_opener'];
    }
    if (isset($this->tokens[$opener]['parenthesis_closer']) === false) {
        // Live coding or syntax error, so no params to find.
        return [];
    }
    $closer = $this->tokens[$opener]['parenthesis_closer'];
    $vars = [];
    $currVar = null;
    $paramStart = $opener + 1;
    $defaultStart = null;
    $equalToken = null;
    $paramCount = 0;
    $hasAttributes = false;
    $passByReference = false;
    $referenceToken = false;
    $variableLength = false;
    $variadicToken = false;
    $typeHint = '';
    $typeHintToken = false;
    $typeHintEndToken = false;
    $nullableType = false;
    $visibilityToken = null;
    $readonlyToken = null;
    for ($i = $paramStart; $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 ($this->tokens[$i]['code'] !== T_TYPE_OPEN_PARENTHESIS && 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'];
                continue;
            }
        }
        if (isset($this->tokens[$i]['bracket_opener']) === true) {
            if ($i !== $this->tokens[$i]['bracket_closer']) {
                $i = $this->tokens[$i]['bracket_closer'];
                continue;
            }
        }
        switch ($this->tokens[$i]['code']) {
            case T_ATTRIBUTE:
                $hasAttributes = true;
                // Skip to the end of the attribute.
                $i = $this->tokens[$i]['attribute_closer'];
                break;
            case T_BITWISE_AND:
                if ($defaultStart === null) {
                    $passByReference = true;
                    $referenceToken = $i;
                }
                break;
            case T_VARIABLE:
                $currVar = $i;
                break;
            case T_ELLIPSIS:
                $variableLength = true;
                $variadicToken = $i;
                break;
            case T_CALLABLE:
                if ($typeHintToken === false) {
                    $typeHintToken = $i;
                }
                $typeHint .= $this->tokens[$i]['content'];
                $typeHintEndToken = $i;
                break;
            case T_SELF:
            case T_PARENT:
            case T_STATIC:
                // Self and parent are valid, static invalid, but was probably intended as type hint.
                if (isset($defaultStart) === false) {
                    if ($typeHintToken === false) {
                        $typeHintToken = $i;
                    }
                    $typeHint .= $this->tokens[$i]['content'];
                    $typeHintEndToken = $i;
                }
                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) {
                    if ($typeHintToken === false) {
                        $typeHintToken = $i;
                    }
                    $typeHint .= $this->tokens[$i]['content'];
                    $typeHintEndToken = $i;
                }
                break;
            case T_NAMESPACE:
            case T_NS_SEPARATOR:
            case T_TYPE_UNION:
            case T_TYPE_INTERSECTION:
            case T_TYPE_OPEN_PARENTHESIS:
            case T_TYPE_CLOSE_PARENTHESIS:
            case T_FALSE:
            case T_TRUE:
            case T_NULL:
                // Part of a type hint or default value.
                if ($defaultStart === null) {
                    if ($typeHintToken === false) {
                        $typeHintToken = $i;
                    }
                    $typeHint .= $this->tokens[$i]['content'];
                    $typeHintEndToken = $i;
                }
                break;
            case T_NULLABLE:
                if ($defaultStart === null) {
                    $nullableType = true;
                    $typeHint .= $this->tokens[$i]['content'];
                    $typeHintEndToken = $i;
                }
                break;
            case T_PUBLIC:
            case T_PROTECTED:
            case T_PRIVATE:
                if ($defaultStart === null) {
                    $visibilityToken = $i;
                }
                break;
            case T_READONLY:
                if ($defaultStart === null) {
                    $readonlyToken = $i;
                }
                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 2;
                }
                $vars[$paramCount] = [];
                $vars[$paramCount]['token'] = $currVar;
                $vars[$paramCount]['name'] = $this->tokens[$currVar]['content'];
                $vars[$paramCount]['content'] = trim($this->getTokensAsString($paramStart, $i - $paramStart));
                if ($defaultStart !== null) {
                    $vars[$paramCount]['default'] = trim($this->getTokensAsString($defaultStart, $i - $defaultStart));
                    $vars[$paramCount]['default_token'] = $defaultStart;
                    $vars[$paramCount]['default_equal_token'] = $equalToken;
                }
                $vars[$paramCount]['has_attributes'] = $hasAttributes;
                $vars[$paramCount]['pass_by_reference'] = $passByReference;
                $vars[$paramCount]['reference_token'] = $referenceToken;
                $vars[$paramCount]['variable_length'] = $variableLength;
                $vars[$paramCount]['variadic_token'] = $variadicToken;
                $vars[$paramCount]['type_hint'] = $typeHint;
                $vars[$paramCount]['type_hint_token'] = $typeHintToken;
                $vars[$paramCount]['type_hint_end_token'] = $typeHintEndToken;
                $vars[$paramCount]['nullable_type'] = $nullableType;
                if ($visibilityToken !== null || $readonlyToken !== null) {
                    $vars[$paramCount]['property_visibility'] = 'public';
                    $vars[$paramCount]['visibility_token'] = false;
                    $vars[$paramCount]['property_readonly'] = false;
                    if ($visibilityToken !== null) {
                        $vars[$paramCount]['property_visibility'] = $this->tokens[$visibilityToken]['content'];
                        $vars[$paramCount]['visibility_token'] = $visibilityToken;
                    }
                    if ($readonlyToken !== null) {
                        $vars[$paramCount]['property_readonly'] = true;
                        $vars[$paramCount]['readonly_token'] = $readonlyToken;
                    }
                }
                if ($this->tokens[$i]['code'] === T_COMMA) {
                    $vars[$paramCount]['comma_token'] = $i;
                }
                else {
                    $vars[$paramCount]['comma_token'] = false;
                }
                // Reset the vars, as we are about to process the next parameter.
                $currVar = null;
                $paramStart = $i + 1;
                $defaultStart = null;
                $equalToken = null;
                $hasAttributes = false;
                $passByReference = false;
                $referenceToken = false;
                $variableLength = false;
                $variadicToken = false;
                $typeHint = '';
                $typeHintToken = false;
                $typeHintEndToken = false;
                $nullableType = false;
                $visibilityToken = null;
                $readonlyToken = null;
                $paramCount++;
                break;
            case T_EQUAL:
                $defaultStart = $this->findNext(Tokens::$emptyTokens, $i + 1, null, true);
                $equalToken = $i;
                break;
        }
        
        //end switch
    }
    
    //end for
    return $vars;
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal