Passed
Push — master ( 4bc1c4...2778f9 )
by William
03:23 queued 11s
created

FunctionCall   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Test Coverage

Coverage 93.33%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 28
c 1
b 0
f 1
dl 0
loc 100
ccs 28
cts 30
cp 0.9333
rs 10
wmc 14

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 3
B parse() 0 49 9
A build() 0 3 1
A __toString() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpMyAdmin\SqlParser\Components;
6
7
use PhpMyAdmin\SqlParser\Component;
8
use PhpMyAdmin\SqlParser\Parser;
9
use PhpMyAdmin\SqlParser\Token;
10
use PhpMyAdmin\SqlParser\TokensList;
11
12
use function is_array;
13
14
/**
15
 * Parses a function call.
16
 */
17
final class FunctionCall implements Component
18
{
19
    /**
20
     * The name of this function.
21
     *
22
     * @var string|null
23
     */
24
    public $name;
25
26
    /**
27
     * The list of parameters.
28
     *
29
     * @var ArrayObj|null
30
     */
31
    public $parameters;
32
33
    /**
34
     * @param string|null            $name       the name of the function to be called
35
     * @param string[]|ArrayObj|null $parameters the parameters of this function
36
     */
37 34
    public function __construct($name = null, $parameters = null)
38
    {
39 34
        $this->name = $name;
40 34
        if (is_array($parameters)) {
41 2
            $this->parameters = new ArrayObj($parameters);
42 32
        } elseif ($parameters instanceof ArrayObj) {
43 2
            $this->parameters = $parameters;
44
        }
45
    }
46
47
    /**
48
     * @param Parser               $parser  the parser that serves as context
49
     * @param TokensList           $list    the list of tokens that are being parsed
50
     * @param array<string, mixed> $options parameters for parsing
51
     *
52
     * @return FunctionCall
53
     */
54 30
    public static function parse(Parser $parser, TokensList $list, array $options = [])
55
    {
56 30
        $ret = new static();
57
58
        /**
59
         * The state of the parser.
60
         *
61
         * Below are the states of the parser.
62
         *
63
         *      0 ----------------------[ name ]-----------------------> 1
64
         *
65
         *      1 --------------------[ parameters ]-------------------> (END)
66
         *
67
         * @var int
68
         */
69 30
        $state = 0;
70
71 30
        for (; $list->idx < $list->count; ++$list->idx) {
72
            /**
73
             * Token parsed at this moment.
74
             */
75 30
            $token = $list->tokens[$list->idx];
76
77
            // End of statement.
78 30
            if ($token->type === Token::TYPE_DELIMITER) {
79 16
                --$list->idx; // Let last token to previous one to avoid "This type of clause was previously parsed."
80 16
                break;
81
            }
82
83
            // Skipping whitespaces and comments.
84 30
            if (($token->type === Token::TYPE_WHITESPACE) || ($token->type === Token::TYPE_COMMENT)) {
85 30
                continue;
86
            }
87
88 30
            if ($state === 0) {
89 30
                if ($token->type === Token::TYPE_OPERATOR && $token->value === '(') {
90 20
                    --$list->idx; // ArrayObj needs to start with `(`
91 20
                    $state = 1;
92 20
                    continue;// do not add this token to the name
93
                }
94
95 30
                $ret->name .= $token->value;
96 20
            } elseif ($state === 1) {
97 20
                    $ret->parameters = ArrayObj::parse($parser, $list);
98 20
                break;
99
            }
100
        }
101
102 30
        return $ret;
103
    }
104
105
    /**
106
     * @param FunctionCall         $component the component to be built
107
     * @param array<string, mixed> $options   parameters for building
108
     */
109 4
    public static function build($component, array $options = []): string
110
    {
111 4
        return $component->name . $component->parameters;
112
    }
113
114
    public function __toString(): string
115
    {
116
        return static::build($this);
117
    }
118
}
119