Passed
Push — master ( b16987...8b6d77 )
by Maurício
03:49 queued 13s
created

FunctionCalls   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
dl 0
loc 54
ccs 20
cts 20
cp 1
rs 10
c 1
b 0
f 0
wmc 9

1 Method

Rating   Name   Duplication   Size   Complexity  
B parse() 0 47 9
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpMyAdmin\SqlParser\Parsers;
6
7
use PhpMyAdmin\SqlParser\Components\FunctionCall;
8
use PhpMyAdmin\SqlParser\Parseable;
9
use PhpMyAdmin\SqlParser\Parser;
10
use PhpMyAdmin\SqlParser\TokensList;
11
use PhpMyAdmin\SqlParser\TokenType;
12
13
/**
14
 * Parses a function call.
15
 */
16
final class FunctionCalls implements Parseable
17
{
18
    /**
19
     * @param Parser               $parser  the parser that serves as context
20
     * @param TokensList           $list    the list of tokens that are being parsed
21
     * @param array<string, mixed> $options parameters for parsing
22
     */
23 30
    public static function parse(Parser $parser, TokensList $list, array $options = []): FunctionCall
24
    {
25 30
        $ret = new FunctionCall();
26
27
        /**
28
         * The state of the parser.
29
         *
30
         * Below are the states of the parser.
31
         *
32
         *      0 ----------------------[ name ]-----------------------> 1
33
         *
34
         *      1 --------------------[ parameters ]-------------------> (END)
35
         */
36 30
        $state = 0;
37
38 30
        for (; $list->idx < $list->count; ++$list->idx) {
39
            /**
40
             * Token parsed at this moment.
41
             */
42 30
            $token = $list->tokens[$list->idx];
43
44
            // End of statement.
45 30
            if ($token->type === TokenType::Delimiter) {
46 16
                --$list->idx; // Let last token to previous one to avoid "This type of clause was previously parsed."
47 16
                break;
48
            }
49
50
            // Skipping whitespaces and comments.
51 30
            if (($token->type === TokenType::Whitespace) || ($token->type === TokenType::Comment)) {
52 30
                continue;
53
            }
54
55 30
            if ($state === 0) {
56 30
                if ($token->type === TokenType::Operator && $token->value === '(') {
57 20
                    --$list->idx; // ArrayObj needs to start with `(`
58 20
                    $state = 1;
59 20
                    continue;// do not add this token to the name
60
                }
61
62 30
                $ret->name .= $token->value;
63 20
            } elseif ($state === 1) {
64 20
                    $ret->parameters = ArrayObjs::parse($parser, $list);
65 20
                break;
66
            }
67
        }
68
69 30
        return $ret;
70
    }
71
}
72