Passed
Push — master ( 6c3f09...c54108 )
by William
03:43
created

FunctionCall::__toString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
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 16
    public function __construct($name = null, $parameters = null)
38
    {
39 16
        $this->name = $name;
40 16
        if (is_array($parameters)) {
41 2
            $this->parameters = new ArrayObj($parameters);
42 14
        } 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 12
    public static function parse(Parser $parser, TokensList $list, array $options = [])
55
    {
56 12
        $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 12
        $state = 0;
70
71 12
        for (; $list->idx < $list->count; ++$list->idx) {
72
            /**
73
             * Token parsed at this moment.
74
             */
75 12
            $token = $list->tokens[$list->idx];
76
77
            // End of statement.
78 12
            if ($token->type === Token::TYPE_DELIMITER) {
79 2
                break;
80
            }
81
82
            // Skipping whitespaces and comments.
83 12
            if (($token->type === Token::TYPE_WHITESPACE) || ($token->type === Token::TYPE_COMMENT)) {
84 12
                continue;
85
            }
86
87 12
            if ($state === 0) {
88 12
                $ret->name = $token->value;
89 12
                $state = 1;
90 10
            } elseif ($state === 1) {
91 10
                if (($token->type === Token::TYPE_OPERATOR) && ($token->value === '(')) {
92 10
                    $ret->parameters = ArrayObj::parse($parser, $list);
93
                }
94
95 10
                break;
96
            }
97
        }
98
99 12
        return $ret;
100
    }
101
102
    /**
103
     * @param FunctionCall         $component the component to be built
104
     * @param array<string, mixed> $options   parameters for building
105
     */
106 4
    public static function build($component, array $options = []): string
107
    {
108 4
        return $component->name . $component->parameters;
109
    }
110
111
    public function __toString(): string
112
    {
113
        return static::build($this);
114
    }
115
}
116