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

Breadcrumb

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

function InvalidRegexPatternRule::extractPatterns

Return value

string[]

1 call to InvalidRegexPatternRule::extractPatterns()
InvalidRegexPatternRule::processNode in vendor/composer/pcre/src/PHPStan/InvalidRegexPatternRule.php

File

vendor/composer/pcre/src/PHPStan/InvalidRegexPatternRule.php, line 51

Class

InvalidRegexPatternRule
Copy of PHPStan's RegularExpressionPatternRule

Namespace

Composer\Pcre\PHPStan

Code

private function extractPatterns(StaticCall $node, Scope $scope) : array {
    if (!$node->class instanceof FullyQualified) {
        return [];
    }
    $isRegex = $node->class
        ->toString() === Regex::class;
    $isPreg = $node->class
        ->toString() === Preg::class;
    if (!$isRegex && !$isPreg) {
        return [];
    }
    if (!$node->name instanceof Node\Identifier || !Preg::isMatch('{^(match|isMatch|grep|replace|split)}', $node->name->name)) {
        return [];
    }
    $functionName = $node->name->name;
    if (!isset($node->getArgs()[0])) {
        return [];
    }
    $patternNode = $node->getArgs()[0]->value;
    $patternType = $scope->getType($patternNode);
    $patternStrings = [];
    foreach ($patternType->getConstantStrings() as $constantStringType) {
        if ($functionName === 'replaceCallbackArray') {
            continue;
        }
        $patternStrings[] = $constantStringType->getValue();
    }
    foreach ($patternType->getConstantArrays() as $constantArrayType) {
        if (in_array($functionName, [
            'replace',
            'replaceCallback',
        ], true)) {
            foreach ($constantArrayType->getValueTypes() as $arrayKeyType) {
                foreach ($arrayKeyType->getConstantStrings() as $constantString) {
                    $patternStrings[] = $constantString->getValue();
                }
            }
        }
        if ($functionName !== 'replaceCallbackArray') {
            continue;
        }
        foreach ($constantArrayType->getKeyTypes() as $arrayKeyType) {
            foreach ($arrayKeyType->getConstantStrings() as $constantString) {
                $patternStrings[] = $constantString->getValue();
            }
        }
    }
    return $patternStrings;
}
RSS feed
Powered by Drupal