function SandboxDatabaseUpdatesValidator::tokensMatchFunctionNamePattern

Determines if a set of tokens contain a function name matching a pattern.

Parameters

array[] $tokens: A set of three tokens, part of a stream returned by token_get_all().

string $pattern: If the tokens declare a named function, a regular expression to test the function name against.

Return value

bool TRUE if the given tokens declare a function whose name matches the given pattern; FALSE otherwise.

See also

token_get_all()

1 call to SandboxDatabaseUpdatesValidator::tokensMatchFunctionNamePattern()
SandboxDatabaseUpdatesValidator::getUpdateFunctions in core/modules/package_manager/src/Validator/SandboxDatabaseUpdatesValidator.php
Returns a list of all update functions for a module.

File

core/modules/package_manager/src/Validator/SandboxDatabaseUpdatesValidator.php, line 157

Class

SandboxDatabaseUpdatesValidator
Flags a warning if there are database updates in a staged update.

Namespace

Drupal\package_manager\Validator

Code

private function tokensMatchFunctionNamePattern(array $tokens, string $pattern) : bool {
    if (count($tokens) !== 3 || !Inspector::assertAllStrictArrays($tokens)) {
        return FALSE;
    }
    // A named function declaration will always be a T_FUNCTION (the word
    // `function`), followed by T_WHITESPACE (or the code would be syntactically
    // invalid), followed by a T_STRING (the function name). This will ignore
    // anonymous functions, but match class methods (although class methods are
    // highly unlikely to match the naming patterns of update hooks).
    $names = array_map('token_name', array_column($tokens, 0));
    if ($names === [
        'T_FUNCTION',
        'T_WHITESPACE',
        'T_STRING',
    ]) {
        return (bool) preg_match($pattern, $tokens[2][1]);
    }
    return FALSE;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.