Completed
Push — master ( a6ddea...8de474 )
by Michal
03:57
created

SetOperation::parse()   C

Complexity

Conditions 13
Paths 4

Size

Total Lines 74
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 37
CRAP Score 13.003

Importance

Changes 8
Bugs 4 Features 0
Metric Value
c 8
b 4
f 0
dl 0
loc 74
ccs 37
cts 38
cp 0.9737
rs 5.3888
cc 13
eloc 34
nc 4
nop 3
crap 13.003

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
 * `SET` keyword parser.
5
 *
6
 * @package    SqlParser
7
 * @subpackage Components
8
 */
9
namespace SqlParser\Components;
10
11
use SqlParser\Component;
12
use SqlParser\Parser;
13
use SqlParser\Token;
14
use SqlParser\TokensList;
15
16
/**
17
 * `SET` keyword parser.
18
 *
19
 * @category   Keywords
20
 * @package    SqlParser
21
 * @subpackage Components
22
 * @author     Dan Ungureanu <[email protected]>
23
 * @license    http://opensource.org/licenses/GPL-2.0 GNU Public License
24
 */
25
class SetOperation extends Component
26
{
27
28
    /**
29
     * The name of the column that is being updated.
30
     *
31
     * @var string
32
     */
33
    public $column;
34
35
    /**
36
     * The new value.
37
     *
38
     * @var string
39
     */
40
    public $value;
41
42
    /**
43
     * @param Parser     $parser  The parser that serves as context.
44
     * @param TokensList $list    The list of tokens that are being parsed.
45
     * @param array      $options Parameters for parsing.
46
     *
47
     * @return SetOperation[]
48
     */
49 8
    public static function parse(Parser $parser, TokensList $list, array $options = array())
50
    {
51 8
        $ret = array();
52
53 8
        $expr = new SetOperation();
54
55
        /**
56
         * The state of the parser.
57
         *
58
         * Below are the states of the parser.
59
         *
60
         *      0 -------------------[ column name ]-------------------> 1
61
         *
62
         *      1 ------------------------[ , ]------------------------> 0
63
         *      1 ----------------------[ value ]----------------------> 1
64
         *
65
         * @var int $state
66
         */
67 8
        $state = 0;
68
69 8
        for (; $list->idx < $list->count; ++$list->idx) {
70
            /**
71
             * Token parsed at this moment.
72
             *
73
             * @var Token $token
74
             */
75 8
            $token = $list->tokens[$list->idx];
76
77
            // End of statement.
78 8
            if ($token->type === Token::TYPE_DELIMITER) {
79 4
                break;
80
            }
81
82
            // Skipping whitespaces and comments.
83 8
            if (($token->type === Token::TYPE_WHITESPACE) || ($token->type === Token::TYPE_COMMENT)) {
84 8
                continue;
85
            }
86
87
            // No keyword is expected.
88 8
            if (($token->type === Token::TYPE_KEYWORD)
89 8
                && ($token->flags & Token::FLAG_KEYWORD_RESERVED)
90 8
                && ($state == 0)
91 8
            ) {
92 4
                break;
93
            }
94
95 8
            if ($state === 0) {
96 8
                if ($token->token === '=') {
97 8
                    $state = 1;
98 8
                } elseif ($token->value !== ',') {
99 8
                    $expr->column .= $token->token;
100 8
                }
101 8
            } elseif ($state === 1) {
102 8
                $tmp = Expression::parse(
103 8
                    $parser,
104 8
                    $list,
105
                    array(
106 8
                        'breakOnAlias' => true,
107
                    )
108 8
                );
109 8
                if ($tmp == null) {
110
                    break;
111
                }
112 8
                $expr->column = trim($expr->column);
113 8
                $expr->value = $tmp->expr;
114 8
                $ret[] = $expr;
115 8
                $expr = new SetOperation();
116 8
                $state = 0;
117 8
            }
118 8
        }
119
120 8
        --$list->idx;
121 8
        return $ret;
122
    }
123
124
    /**
125
     * @param SetOperation|SetOperation[] $component The component to be built.
126
     * @param array                       $options   Parameters for building.
127
     *
128
     * @return string
129
     */
130 1
    public static function build($component, array $options = array())
131
    {
132 1
        if (is_array($component)) {
133 1
            return implode(', ', $component);
134
        } else {
135 1
            return $component->column . ' = ' . $component->value;
136
        }
137
    }
138
}
139