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

Breadcrumb

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

function AbstractPatternSniff::parse

Parses a pattern string into an array of pattern steps.

Parameters

string $pattern The pattern to parse.:

Return value

array The parsed pattern array.

See also

createSkipPattern()

createTokenPattern()

1 call to AbstractPatternSniff::parse()
AbstractPatternSniff::register in vendor/squizlabs/php_codesniffer/src/Sniffs/AbstractPatternSniff.php
Registers the tokens to listen to.

File

vendor/squizlabs/php_codesniffer/src/Sniffs/AbstractPatternSniff.php, line 773

Class

AbstractPatternSniff

Namespace

PHP_CodeSniffer\Sniffs

Code

private function parse($pattern) {
    $patterns = [];
    $length = strlen($pattern);
    $lastToken = 0;
    $firstToken = 0;
    for ($i = 0; $i < $length; $i++) {
        $specialPattern = false;
        $isLastChar = $i === $length - 1;
        $oldFirstToken = $firstToken;
        if (substr($pattern, $i, 3) === '...') {
            // It's a skip pattern. The skip pattern requires the
            // content of the token in the "from" position and the token
            // to skip to.
            $specialPattern = $this->createSkipPattern($pattern, $i - 1);
            $lastToken = $i - $firstToken;
            $firstToken = $i + 3;
            $i += 2;
            if ($specialPattern['to'] !== 'unknown') {
                $firstToken++;
            }
        }
        else {
            if (substr($pattern, $i, 3) === 'abc') {
                $specialPattern = [
                    'type' => 'string',
                ];
                $lastToken = $i - $firstToken;
                $firstToken = $i + 3;
                $i += 2;
            }
            else {
                if (substr($pattern, $i, 3) === 'EOL') {
                    $specialPattern = [
                        'type' => 'newline',
                    ];
                    $lastToken = $i - $firstToken;
                    $firstToken = $i + 3;
                    $i += 2;
                }
            }
        }
        
        //end if
        if ($specialPattern !== false || $isLastChar === true) {
            // If we are at the end of the string, don't worry about a limit.
            if ($isLastChar === true) {
                // Get the string from the end of the last skip pattern, if any,
                // to the end of the pattern string.
                $str = substr($pattern, $oldFirstToken);
            }
            else {
                // Get the string from the end of the last special pattern,
                // if any, to the start of this special pattern.
                if ($lastToken === 0) {
                    // Note that if the last special token was zero characters ago,
                    // there will be nothing to process so we can skip this bit.
                    // This happens if you have something like: EOL... in your pattern.
                    $str = '';
                }
                else {
                    $str = substr($pattern, $oldFirstToken, $lastToken);
                }
            }
            if ($str !== '') {
                $tokenPatterns = $this->createTokenPattern($str);
                foreach ($tokenPatterns as $tokenPattern) {
                    $patterns[] = $tokenPattern;
                }
            }
            // Make sure we don't skip the last token.
            if ($isLastChar === false && $i === $length - 1) {
                $i--;
            }
        }
        
        //end if
        // Add the skip pattern *after* we have processed
        // all the tokens from the end of the last skip pattern
        // to the start of this skip pattern.
        if ($specialPattern !== false) {
            $patterns[] = $specialPattern;
        }
    }
    
    //end for
    return $patterns;
}

API Navigation

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