Completed
Pull Request — master (#90)
by Deven
104:46 queued 39:40
created

IntoKeyword::parse()   D

Complexity

Conditions 18
Paths 6

Size

Total Lines 83
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 39
CRAP Score 18

Importance

Changes 0
Metric Value
cc 18
eloc 40
nc 6
nop 3
dl 0
loc 83
ccs 39
cts 39
cp 1
crap 18
rs 4.8829
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
 * `INTO` 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
 * `INTO` keyword parser.
18
 *
19
 * @category   Keywords
20
 * @package    SqlParser
21
 * @subpackage Components
22
 * @license    https://www.gnu.org/licenses/gpl-2.0.txt GPL-2.0+
23
 */
24
class IntoKeyword extends Component
25
{
26
27
    /**
28
     * Type of target (OUTFILE or SYMBOL).
29
     *
30
     * @var string
31
     */
32
    public $type;
33
34
    /**
35
     * The destination, which can be a table or a file.
36
     *
37
     * @var string|Expression
38
     */
39
    public $dest;
40
41
    /**
42
     * The name of the columns.
43
     *
44
     * @var array
45
     */
46
    public $columns;
47
48
    /**
49
     * The values to be selected into (SELECT .. INTO @var1)
50
     *
51
     * @var ExpressionArray
52
     */
53
    public $values;
54
55
    /**
56 15
     * @param Parser     $parser  The parser that serves as context.
57
     * @param TokensList $list    The list of tokens that are being parsed.
58 15
     * @param array      $options Parameters for parsing.
59
     *
60
     * @return IntoKeyword
61
     */
62
    public static function parse(Parser $parser, TokensList $list, array $options = array())
63
    {
64
        $ret = new IntoKeyword();
65
66
        /**
67
         * The state of the parser.
68
         *
69
         * Below are the states of the parser.
70
         *
71
         *      0 -----------------------[ name ]----------------------> 1
72
         *      0 ---------------------[ OUTFILE ]---------------------> 2
73
         *
74 15
         *      1 ------------------------[ ( ]------------------------> (END)
75
         *
76 15
         *      2 ---------------------[ filename ]--------------------> 1
77
         *
78
         * @var int $state
79
         */
80
        $state = 0;
81
82 15
        for (; $list->idx < $list->count; ++$list->idx) {
83
            /**
84
             * Token parsed at this moment.
85 15
             *
86 1
             * @var Token $token
87
             */
88
            $token = $list->tokens[$list->idx];
89
90 15
            // End of statement.
91 13
            if ($token->type === Token::TYPE_DELIMITER) {
92
                break;
93
            }
94 15
95 9
            // Skipping whitespaces and comments.
96 6
            if (($token->type === Token::TYPE_WHITESPACE) || ($token->type === Token::TYPE_COMMENT)) {
97 6
                continue;
98 6
            }
99
100
            if (($token->type === Token::TYPE_KEYWORD) && ($token->flags & Token::FLAG_KEYWORD_RESERVED)) {
101
                if (($state === 0) && ($token->value === 'OUTFILE')) {
102 3
                    $ret->type = 'OUTFILE';
103
                    $state = 2;
104
                    continue;
105 14
                }
106 9
107 9
                // No other keyword is expected.
108 9
                break;
109
            }
110 9
111 9
            if ($state === 0) {
112
                if ((isset($options['fromInsert'])
113 9
                    && $options['fromInsert'])
114 9
                    || (isset($options['fromReplace'])
115 14
                    && $options['fromReplace'])
116 6
                ) {
117 6
                    $ret->dest = Expression::parse(
118 6
                        $parser,
119 6
                        $list,
120 6
                        array(
121 5
                            'parseField' => 'table',
122 5
                            'breakOnAlias' => true,
123 5
                        )
124 5
                    );
125
                } else {
126 9
                    $ret->values = ExpressionArray::parse($parser, $list);
0 ignored issues
show
Documentation Bug introduced by
It seems like \SqlParser\Components\Ex...::parse($parser, $list) of type array<integer,object<Sql...Components\Expression>> is incompatible with the declared type object<SqlParser\Components\ExpressionArray> of property $values.

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...
127
                }
128 15
                $state = 1;
129 15
            } elseif ($state === 1) {
130
                if (($token->type === Token::TYPE_OPERATOR) && ($token->value === '(')) {
131
                    $ret->columns = ArrayObj::parse($parser, $list)->values;
132
                    ++$list->idx;
133
                }
134
                break;
135
            } elseif ($state === 2) {
136
                $ret->dest = $token->value;
137
                ++$list->idx;
138 3
                break;
139
            }
140 3
        }
141 2
142 2
        --$list->idx;
143 2
        return $ret;
144
    }
145 1
146
    /**
147
     * @param IntoKeyword $component The component to be built.
148
     * @param array       $options   Parameters for building.
149
     *
150
     * @return string
151
     */
152
    public static function build($component, array $options = array())
153
    {
154
        if ($component->dest instanceof Expression) {
155
            $columns = !empty($component->columns) ?
156
                '(`' . implode('`, `', $component->columns) . '`)' : '';
157
            return $component->dest . $columns;
158
        } elseif (isset($component->values)) {
159
            return ExpressionArray::build($component->values);
0 ignored issues
show
Documentation introduced by
$component->values is of type object<SqlParser\Components\ExpressionArray>, but the function expects a array<integer,object<Sql...Components\Expression>>.

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...
160
        } else {
161
            return 'OUTFILE "' . $component->dest . '"';
162
        }
163
    }
164
}