Completed
Push — master ( 082a1f...e8595e )
by Michal
10:33 queued 10:18
created

ExpressionArray   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 107
ccs 43
cts 43
cp 1
rs 10
c 0
b 0
f 0
wmc 20
lcom 0
cbo 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
D parse() 0 81 18
A build() 0 9 2
1
<?php
2
3
/**
4
 * Parses a list of expressions delimited by a comma.
5
 */
6
7
namespace SqlParser\Components;
8
9
use SqlParser\Component;
10
use SqlParser\Parser;
11
use SqlParser\Token;
12
use SqlParser\TokensList;
13
14
/**
15
 * Parses a list of expressions delimited by a comma.
16
 *
17
 * @category   Keywords
18
 *
19
 * @license    https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+
20
 */
21
class ExpressionArray extends Component
22
{
23
    /**
24
     * @param Parser     $parser  the parser that serves as context
25
     * @param TokensList $list    the list of tokens that are being parsed
26
     * @param array      $options parameters for parsing
27
     *
28
     * @return Expression[]
29
     */
30
    public static function parse(Parser $parser, TokensList $list, array $options = array())
31
    {
32
        $ret = array();
33
34
        /**
35 71
         * The state of the parser.
36
         *
37 71
         * Below are the states of the parser.
38
         *
39
         *      0 ----------------------[ array ]---------------------> 1
40
         *
41
         *      1 ------------------------[ , ]------------------------> 0
42
         *      1 -----------------------[ else ]----------------------> (END)
43
         *
44
         * @var int
45
         */
46
        $state = 0;
47
48
        for (; $list->idx < $list->count; ++$list->idx) {
49
            /**
50
             * Token parsed at this moment.
51 71
             *
52
             * @var Token
53 71
             */
54
            $token = $list->tokens[$list->idx];
55
56
            // End of statement.
57
            if ($token->type === Token::TYPE_DELIMITER) {
58
                break;
59 71
            }
60
61
            // Skipping whitespaces and comments.
62 71
            if (($token->type === Token::TYPE_WHITESPACE) || ($token->type === Token::TYPE_COMMENT)) {
63 30
                continue;
64
            }
65
66
            if (($token->type === Token::TYPE_KEYWORD)
67 70
                && ($token->flags & Token::FLAG_KEYWORD_RESERVED)
68 49
                && ((~$token->flags & Token::FLAG_KEYWORD_FUNCTION))
69
                && ($token->value !== 'DUAL')
70
                && ($token->value !== 'NULL')
71 70
                && ($token->value !== 'CASE')
72 70
            ) {
73 70
                // No keyword is expected.
74 70
                break;
75 70
            }
76 70
77
            if ($state === 0) {
78 51
                if ($token->type === Token::TYPE_KEYWORD
79
                    && $token->value === 'CASE'
80
                ) {
81 70
                    $expr = CaseExpression::parse($parser, $list, $options);
82 70
                } else {
83 70
                    $expr = Expression::parse($parser, $list, $options);
84 3
                }
85
86 69
                if ($expr === null) {
87 69
                    break;
88 69
                }
89 20
                $ret[] = $expr;
90 16
                $state = 1;
91 16
            } elseif ($state === 1) {
92 4
                if ($token->value === ',') {
93
                    $state = 0;
94 16
                } else {
95 69
                    break;
96
                }
97 71
            }
98 4
        }
99 4
100 4
        if ($state === 0) {
101 4
            $parser->error(
102 4
                __('An expression was expected.'),
103
                $list->tokens[$list->idx]
104 71
            );
105 71
        }
106
107
        --$list->idx;
108
109
        return $ret;
110
    }
111
112
    /**
113
     * @param Expression[] $component the component to be built
114 6
     * @param array        $options   parameters for building
115
     *
116 6
     * @return string
117 6
     */
118 6
    public static function build($component, array $options = array())
119 6
    {
120 6
        $ret = array();
121
        foreach ($component as $frag) {
122
            $ret[] = $frag::build($frag);
123
        }
124
125
        return implode($ret, ', ');
126
    }
127
}
128