Completed
Pull Request — master (#88)
by Deven
155:28 queued 90:34
created

CaseExpression::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Parses a reference to a CASE expression
5
 *
6
 * @package    SqlParser
7
 * @subpackage Components
8
 */
9
namespace SqlParser\Components;
10
11
use SqlParser\Context;
12
use SqlParser\Component;
13
use SqlParser\Parser;
14
use SqlParser\Token;
15
use SqlParser\TokensList;
16
17
18
/**
19
 * Parses a reference to a CASE expression
20
 *
21
 * @category   Components
22
 * @package    SqlParser
23
 * @subpackage Components
24
 * @license    http://opensource.org/licenses/GPL-2.0 GNU Public License
25
 */
26
class CaseExpression extends Component
27
{
28
29
    /**
30
     * The value to be compared
31
     *
32
     * @var Expression
33
     */
34
    public $value;
35
36
    /**
37
     * The conditions in WHEN clauses
38
     *
39
     * @var array
40
     */
41
    public $conditions;
42
43
    /**
44
     * The results matching with the WHEN clauses
45
     *
46
     * @var array
47
     */
48
    public $results;
49
50
    /**
51
     * The values to be compared against
52
     *
53
     * @var array
54
     */
55
    public $compare_values;
56
57
    /**
58
     * The result in ELSE section of expr
59
     *
60
     * @var array
61
     */
62
    public $else_result;
63
64
    /**
65
     * Constructor.
66
     *
67
     */
68
    public function __construct()
69
    {
70
    }
71
72
    /**
73
     *
74
     * @param Parser     $parser  The parser that serves as context.
75
     * @param TokensList $list    The list of tokens that are being parsed.
76
     *
77
     * @return Expression
0 ignored issues
show
Documentation introduced by
Should the return type not be CaseExpression?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
78
     */
79
    public static function parse(Parser $parser, TokensList $list, array $options = array())
80
    {
81
        $ret = new CaseExpression();
82
83
        /**
84
         * State of parser
85
         *
86
         * @var int $parser
87
         */
88
        $state = 0;
89
90
        /**
91
         * Syntax type (type 0 or type 1)
92
         *
93
         * @var int $type
94
         */
95
        $type = 0;
96
97
        ++$list->idx; // Skip 'CASE'
98
99
        for (; $list->idx < $list->count; ++$list->idx) {
100
101
            /**
102
             * Token parsed at this moment.
103
             *
104
             * @var Token $token
105
             */
106
            $token = $list->tokens[$list->idx];
107
108
            // Skipping whitespaces and comments.
109
            if (($token->type === Token::TYPE_WHITESPACE)
110
                || ($token->type === Token::TYPE_COMMENT)
111
            ) {
112
                continue;
113
            }
114
115
            if ($state === 0) {
116
                if ($token->type === Token::TYPE_KEYWORD
117
                    && $token->value === 'WHEN'
118
                ) {
119
                    ++$list->idx; // Skip 'WHEN'
120
                    $new_condition = Condition::parse($parser, $list);
0 ignored issues
show
Documentation introduced by
$parser is of type integer, but the function expects a object<SqlParser\Parser>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
121
                    $type = 1;
122
                    $state = 1;
123
                    $ret->conditions[] = $new_condition;
124
                } elseif ($token->type === Token::TYPE_KEYWORD
125
                    && $token->value === 'ELSE'
126
                ) {
127
                    ++$list->idx; // Skip 'ELSE'
128
                    $ret->else_result = Expression::parse($parser, $list);
0 ignored issues
show
Documentation Bug introduced by
It seems like \SqlParser\Components\Ex...::parse($parser, $list) of type object<SqlParser\Components\Expression> or null is incompatible with the declared type array of property $else_result.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
Documentation introduced by
$parser is of type integer, but the function expects a object<SqlParser\Parser>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
129
                    $state = 0; // last clause of CASE expression
130
                } elseif ($token->type === Token::TYPE_KEYWORD
131
                    && ($token->value === 'END'
132
                    || $token->value === 'end')
133
                ) {
134
                    $state = 3; // end of CASE expression
135
                    ++$list->idx;
136
                    break;
137
                } elseif ($token->type === Token::TYPE_KEYWORD) {
138
                    $parser->error(__('Unexpected keyword.'), $token);
0 ignored issues
show
Bug introduced by
The method error cannot be called on $parser (of type integer).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
139
                    break;
140
                } else {
141
                    $ret->value = Expression::parse($parser, $list);
0 ignored issues
show
Documentation introduced by
$parser is of type integer, but the function expects a object<SqlParser\Parser>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
142
                    $type = 0;
143
                    $state = 1;
144
                }
145
            } elseif ($state === 1) {
146
                if ($type === 0) {
147
                    if ($token->type === Token::TYPE_KEYWORD
148
                        && $token->value === 'WHEN'
149
                    ) {
150
                        ++$list->idx; // Skip 'WHEN'
151
                        $new_value = Expression::parse($parser, $list);
0 ignored issues
show
Documentation introduced by
$parser is of type integer, but the function expects a object<SqlParser\Parser>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
152
                        $state = 2;
153
                        $ret->compare_values[] = $new_value;
154
                    } elseif ($token->type === Token::TYPE_KEYWORD
155
                        && $token->value === 'ELSE'
156
                    ) {
157
                        ++$list->idx; // Skip 'ELSE'
158
                        $ret->else_result = Expression::parse($parser, $list);
0 ignored issues
show
Documentation introduced by
$parser is of type integer, but the function expects a object<SqlParser\Parser>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
159
                        $state = 0; // last clause of CASE expression
160
                    } elseif ($token->type === Token::TYPE_KEYWORD
161
                        && ($token->value === 'END'
162
                        || $token->value === 'end')
163
                    ) {
164
                        $state = 3; // end of CASE expression
165
                        ++$list->idx;
166
                        break;
167
                    } elseif ($token->type === Token::TYPE_KEYWORD) {
168
                        $parser->error(__('Unexpected keyword.'), $token);
0 ignored issues
show
Bug introduced by
The method error cannot be called on $parser (of type integer).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
169
                        break;
170
                    }
171 View Code Duplication
                } else {
172
                    if ($token->type === Token::TYPE_KEYWORD
173
                        && $token->value === 'THEN'
174
                    ) {
175
                        ++$list->idx; // Skip 'THEN'
176
                        $new_result = Expression::parse($parser, $list);
0 ignored issues
show
Documentation introduced by
$parser is of type integer, but the function expects a object<SqlParser\Parser>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
177
                        $state = 0;
178
                        $ret->results[] = $new_result;
179
                    } elseif ($token->type === Token::TYPE_KEYWORD) {
180
                        $parser->error(__('Unexpected keyword.'), $token);
0 ignored issues
show
Bug introduced by
The method error cannot be called on $parser (of type integer).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
181
                        break;
182
                    }
183
                }
184
            } elseif ($state === 2) {
185 View Code Duplication
                if ($type === 0) {
186
                    if ($token->type === Token::TYPE_KEYWORD
187
                        && $token->value === 'THEN'
188
                    ) {
189
                        ++$list->idx; // Skip 'THEN'
190
                        $new_result = Expression::parse($parser, $list);
0 ignored issues
show
Documentation introduced by
$parser is of type integer, but the function expects a object<SqlParser\Parser>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
191
                        $ret->results[] = $new_result;
192
                        $state = 1;
193
                    } elseif ($token->type === Token::TYPE_KEYWORD) {
194
                        $parser->error(__('Unexpected keyword.'), $token);
0 ignored issues
show
Bug introduced by
The method error cannot be called on $parser (of type integer).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
195
                        break;
196
                    }
197
                }
198
            }
199
        }
200
201
        if ($state !== 3) {
202
            $parser->error(
0 ignored issues
show
Bug introduced by
The method error cannot be called on $parser (of type integer).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
203
                __('Unexpected end of CASE expression'),
204
                $list->tokens[$list->idx - 1]
205
            );
206
        }
207
208
        --$list->idx;
209
        return $ret;
210
    }
211
212
    /**
213
     * @param Expression $component The component to be built.
214
     * @param array      $options   Parameters for building.
215
     *
216
     * @return string
217
     */
218
    public static function build($component, array $options = array())
219
    {
220
        $ret = 'CASE ';
221
        if (isset($component->value)) {
222
            // Syntax type 0
223
            $ret .= $component->value . ' ';
0 ignored issues
show
Bug introduced by
The property value does not seem to exist in SqlParser\Components\Expression.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
224 View Code Duplication
            for (
225
                $i = 0;
226
                $i < count($component->compare_values) && $i < count($component->results);
0 ignored issues
show
Bug introduced by
The property compare_values does not seem to exist in SqlParser\Components\Expression.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
Bug introduced by
The property results does not seem to exist in SqlParser\Components\Expression.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
227
                ++$i
228
            ) {
229
                $ret .= 'WHEN ' . $component->compare_values[$i] . ' ';
230
                $ret .= 'THEN ' . $component->results[$i] . ' ';
231
            }
232
        } else {
233
            // Syntax type 1
234 View Code Duplication
            for (
235
                $i = 0;
236
                $i < count($component->conditions) && $i < count($component->results);
0 ignored issues
show
Bug introduced by
The property conditions does not seem to exist in SqlParser\Components\Expression.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
237
                ++$i
238
            ) {
239
                $ret .= 'WHEN ' . Condition::build($component->conditions[$i]) . ' ';
240
                $ret .= 'THEN ' . $component->results[$i] . ' ';
241
            }
242
        }
243
        if (isset($component->else_result)) {
244
            $ret .= 'ELSE ' . $component->else_result . ' ';
0 ignored issues
show
Bug introduced by
The property else_result does not seem to exist in SqlParser\Components\Expression.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
245
        }
246
        $ret .= 'END';
247
248
        return $ret;
249
    }
250
}
251