Completed
Push — master ( e05c46...93167f )
by Dan
03:50
created

SetOperation::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
crap 1
1
<?php
2
3
/**
4
 * `SET` keyword parser.
5
 */
6
7
namespace PhpMyAdmin\SqlParser\Components;
8
9
use PhpMyAdmin\SqlParser\Component;
10
use PhpMyAdmin\SqlParser\Parser;
11
use PhpMyAdmin\SqlParser\Token;
12
use PhpMyAdmin\SqlParser\TokensList;
13
14
/**
15
 * `SET` keyword parser.
16
 *
17
 * @category   Keywords
18
 *
19
 * @license    https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+
20
 */
21
class SetOperation extends Component
22
{
23
    /**
24
     * The name of the column that is being updated.
25
     *
26
     * @var string
27
     */
28
    public $column;
29
30
    /**
31
     * The new value.
32
     *
33
     * @var string
34
     */
35
    public $value;
36
37
    /**
38
     * Constructor.
39
     *
40
     * @param string $column Field's name..
0 ignored issues
show
Documentation introduced by
Should the type for parameter $column not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
41
     * @param string $value  New value.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $value not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
42
     */
43 26
    public function __construct($column = null, $value = null)
44
    {
45 26
        $this->column = $column;
46 26
        $this->value = $value;
47 26
    }
48
49
    /**
50
     * @param Parser     $parser  the parser that serves as context
51
     * @param TokensList $list    the list of tokens that are being parsed
52
     * @param array      $options parameters for parsing
53
     *
54
     * @return SetOperation[]
55
     */
56 26
    public static function parse(Parser $parser, TokensList $list, array $options = array())
57
    {
58 26
        $ret = array();
59
60 26
        $expr = new self();
61
62
        /**
63
         * The state of the parser.
64
         *
65
         * Below are the states of the parser.
66
         *
67
         *      0 -------------------[ column name ]-------------------> 1
68
         *
69
         *      1 ------------------------[ , ]------------------------> 0
70
         *      1 ----------------------[ value ]----------------------> 1
71
         *
72
         * @var int
73
         */
74 26
        $state = 0;
75
76 26
        for (; $list->idx < $list->count; ++$list->idx) {
77
            /**
78
             * Token parsed at this moment.
79
             *
80
             * @var Token
81
             */
82 26
            $token = $list->tokens[$list->idx];
83
84
            // End of statement.
85 26
            if ($token->type === Token::TYPE_DELIMITER) {
86 21
                break;
87
            }
88
89
            // Skipping whitespaces and comments.
90 18
            if (($token->type === Token::TYPE_WHITESPACE) || ($token->type === Token::TYPE_COMMENT)) {
91 18
                continue;
92
            }
93
94
            // No keyword is expected.
95 18
            if (($token->type === Token::TYPE_KEYWORD)
96 18
                && ($token->flags & Token::FLAG_KEYWORD_RESERVED)
97 18
                && ($state == 0)
98
            ) {
99 5
                break;
100
            }
101
102 18
            if ($state === 0) {
103 18
                if ($token->token === '=') {
104 18
                    $state = 1;
105 18
                } elseif ($token->value !== ',') {
106 18
                    $expr->column .= $token->token;
107
                }
108 18
            } elseif ($state === 1) {
109 18
                $tmp = Expression::parse(
110
                    $parser,
111
                    $list,
112
                    array(
113 18
                        'breakOnAlias' => true,
114
                    )
115
                );
116 18
                if ($tmp == null) {
117 1
                    $parser->error('Missing expression.', $token);
118 1
                    break;
119
                }
120 17
                $expr->column = trim($expr->column);
121 17
                $expr->value = $tmp->expr;
122 17
                $ret[] = $expr;
123 17
                $expr = new self();
124 17
                $state = 0;
125
            }
126
        }
127
128 26
        --$list->idx;
129
130 26
        return $ret;
131
    }
132
133
    /**
134
     * @param SetOperation|SetOperation[] $component the component to be built
135
     * @param array                       $options   parameters for building
136
     *
137
     * @return string
138
     */
139 5
    public static function build($component, array $options = array())
140
    {
141 5
        if (is_array($component)) {
142 5
            return implode(', ', $component);
143
        }
144
145 4
        return $component->column . ' = ' . $component->value;
146
    }
147
}
148