Passed
Push — master ( ee792a...6ee6fa )
by Michal
03:51
created

src/Components/Condition.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/**
4
 * `WHERE` 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
 * `WHERE` keyword parser.
16
 *
17
 * @category   Keywords
18
 *
19
 * @license    https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+
20
 */
21
class Condition extends Component
22
{
23
    /**
24
     * Logical operators that can be used to delimit expressions.
25
     *
26
     * @var array
27
     */
28
    public static $DELIMITERS = array('&&', '||', 'AND', 'OR', 'XOR');
29
30
    /**
31
     * List of allowed reserved keywords in conditions.
32
     *
33
     * @var array
34
     */
35
    public static $ALLOWED_KEYWORDS = array(
36
        'ALL' => 1,
37
        'AND' => 1,
38
        'BETWEEN' => 1,
39
        'EXISTS' => 1,
40
        'IF' => 1,
41
        'IN' => 1,
42
        'INTERVAL' => 1,
43
        'IS' => 1,
44
        'LIKE' => 1,
45
        'MATCH' => 1,
46
        'NOT IN' => 1,
47
        'NOT NULL' => 1,
48
        'NOT' => 1,
49
        'NULL' => 1,
50
        'OR' => 1,
51
        'REGEXP' => 1,
52
        'RLIKE' => 1,
53
        'XOR' => 1,
54
    );
55
56
    /**
57
     * Identifiers recognized.
58
     *
59
     * @var array
60
     */
61
    public $identifiers = array();
62
63
    /**
64
     * Whether this component is an operator.
65
     *
66
     * @var bool
67
     */
68
    public $isOperator = false;
69
70
    /**
71
     * The condition.
72
     *
73
     * @var string
74
     */
75
    public $expr;
76
77
    /**
78
     * Constructor.
79
     *
80
     * @param string $expr the condition or the operator
0 ignored issues
show
Should the type for parameter $expr 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...
81
     */
82 64
    public function __construct($expr = null)
83
    {
84 64
        $this->expr = trim($expr);
85 64
    }
86
87
    /**
88
     * @param Parser     $parser  the parser that serves as context
89
     * @param TokensList $list    the list of tokens that are being parsed
90
     * @param array      $options parameters for parsing
91
     *
92
     * @return Condition[]
93
     */
94 63
    public static function parse(Parser $parser, TokensList $list, array $options = array())
95
    {
96 63
        $ret = array();
97
98 63
        $expr = new self();
99
100
        /**
101
         * Counts brackets.
102
         *
103
         * @var int
104
         */
105 63
        $brackets = 0;
106
107
        /**
108
         * Whether there was a `BETWEEN` keyword before or not.
109
         *
110
         * It is required to keep track of them because their structure contains
111
         * the keyword `AND`, which is also an operator that delimits
112
         * expressions.
113
         *
114
         * @var bool
115
         */
116 63
        $betweenBefore = false;
117
118 63
        for (; $list->idx < $list->count; ++$list->idx) {
119
            /**
120
             * Token parsed at this moment.
121
             *
122
             * @var Token
123
             */
124 63
            $token = $list->tokens[$list->idx];
125
126
            // End of statement.
127 63
            if ($token->type === Token::TYPE_DELIMITER) {
128 32
                break;
129
            }
130
131
            // Skipping whitespaces and comments.
132 63
            if ($token->type === Token::TYPE_COMMENT) {
133 5
                continue;
134
            }
135
136
            // Replacing all whitespaces (new lines, tabs, etc.) with a single
137
            // space character.
138 63
            if ($token->type === Token::TYPE_WHITESPACE) {
139 62
                $expr->expr .= ' ';
140 62
                continue;
141
            }
142
143
            // Conditions are delimited by logical operators.
144 63
            if (in_array($token->value, static::$DELIMITERS, true)) {
145 12
                if (($betweenBefore) && ($token->value === 'AND')) {
146
                    // The syntax of keyword `BETWEEN` is hard-coded.
147 1
                    $betweenBefore = false;
148
                } else {
149
                    // The expression ended.
150 12
                    $expr->expr = trim($expr->expr);
151 12
                    if (!empty($expr->expr)) {
152 12
                        $ret[] = $expr;
153
                    }
154
155
                    // Adding the operator.
156 12
                    $expr = new self($token->value);
157 12
                    $expr->isOperator = true;
158 12
                    $ret[] = $expr;
159
160
                    // Preparing to parse another condition.
161 12
                    $expr = new self();
162 12
                    continue;
163
                }
164
            }
165
166 63
            if (($token->type === Token::TYPE_KEYWORD)
167 41
                && ($token->flags & Token::FLAG_KEYWORD_RESERVED)
168 38
                && !($token->flags & Token::FLAG_KEYWORD_FUNCTION)
169
            ) {
170 35
                if ($token->value === 'BETWEEN') {
171 1
                    $betweenBefore = true;
172
                }
173 35
                if (($brackets === 0) && (empty(static::$ALLOWED_KEYWORDS[$token->value]))) {
174 33
                    break;
175
                }
176
            }
177
178 62 View Code Duplication
            if ($token->type === Token::TYPE_OPERATOR) {
179 59
                if ($token->value === '(') {
180 11
                    ++$brackets;
181 59
                } elseif ($token->value === ')') {
182 12
                    if ($brackets == 0) {
183 1
                        break;
184
                    }
185 11
                    --$brackets;
186
                }
187
            }
188
189 62
            $expr->expr .= $token->token;
190 62
            if (($token->type === Token::TYPE_NONE)
191 62
                || (($token->type === Token::TYPE_KEYWORD)
192 10
                && (!($token->flags & Token::FLAG_KEYWORD_RESERVED)))
193 62
                || ($token->type === Token::TYPE_STRING)
194 62
                || ($token->type === Token::TYPE_SYMBOL)
195
            ) {
196 44
                if (!in_array($token->value, $expr->identifiers)) {
197 44
                    $expr->identifiers[] = $token->value;
198
                }
199
            }
200
        }
201
202
        // Last iteration was not processed.
203 63
        $expr->expr = trim($expr->expr);
204 63
        if (!empty($expr->expr)) {
205 62
            $ret[] = $expr;
206
        }
207
208 63
        --$list->idx;
209
210 63
        return $ret;
211
    }
212
213
    /**
214
     * @param Condition[] $component the component to be built
215
     * @param array       $options   parameters for building
216
     *
217
     * @return string
218
     */
219 14
    public static function build($component, array $options = array())
220
    {
221 14
        if (is_array($component)) {
222 14
            return implode(' ', $component);
223
        }
224
225 14
        return $component->expr;
226
    }
227
}
228