Completed
Pull Request — master (#112)
by Michal
374:43 queued 309:52
created

ExpressionArray::parse()   D

Complexity

Conditions 18
Paths 12

Size

Total Lines 81
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 37
CRAP Score 18

Importance

Changes 0
Metric Value
cc 18
eloc 37
nc 12
nop 3
dl 0
loc 81
ccs 37
cts 37
cp 1
crap 18
rs 4.9774
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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