Completed
Push — master ( 578d3a...04a929 )
by Maurício
34s queued 14s
created

SetOperations::parse()   C

Complexity

Conditions 14
Paths 8

Size

Total Lines 84
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 40
CRAP Score 14

Importance

Changes 0
Metric Value
cc 14
eloc 40
nc 8
nop 3
dl 0
loc 84
ccs 40
cts 40
cp 1
crap 14
rs 6.2666
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
declare(strict_types=1);
4
5
namespace PhpMyAdmin\SqlParser\Components\Parsers;
6
7
use PhpMyAdmin\SqlParser\Components\Expression;
8
use PhpMyAdmin\SqlParser\Components\SetOperation;
9
use PhpMyAdmin\SqlParser\Parseable;
10
use PhpMyAdmin\SqlParser\Parser;
11
use PhpMyAdmin\SqlParser\Token;
12
use PhpMyAdmin\SqlParser\TokensList;
13
use PhpMyAdmin\SqlParser\TokenType;
14
15
use function implode;
16
use function in_array;
17
use function trim;
18
19
/**
20
 * `SET` keyword parser.
21
 */
22
final class SetOperations implements Parseable
23
{
24
    /**
25
     * @param Parser               $parser  the parser that serves as context
26
     * @param TokensList           $list    the list of tokens that are being parsed
27
     * @param array<string, mixed> $options parameters for parsing
28
     *
29
     * @return SetOperation[]
30
     */
31 120
    public static function parse(Parser $parser, TokensList $list, array $options = []): array
32
    {
33 120
        $ret = [];
34
35 120
        $expr = new SetOperation();
36
37
        /**
38
         * The state of the parser.
39
         *
40
         * Below are the states of the parser.
41
         *
42
         *      0 ---------------------[ col_name ]--------------------> 0
43
         *      0 ---------------------[ = or := ]---------------------> 1
44
         *      1 -----------------------[ value ]---------------------> 1
45
         *      1 ------------------------[ , ]------------------------> 0
46
         */
47 120
        $state = 0;
48
49
        /**
50
         * Token when the parser has seen the latest comma
51
         */
52 120
        $commaLastSeenAt = null;
53
54 120
        for (; $list->idx < $list->count; ++$list->idx) {
55
            /**
56
             * Token parsed at this moment.
57
             */
58 120
            $token = $list->tokens[$list->idx];
59
60
            // End of statement.
61 120
            if ($token->type === TokenType::Delimiter) {
62 90
                break;
63
            }
64
65
            // Skipping whitespaces and comments.
66 100
            if (($token->type === TokenType::Whitespace) || ($token->type === TokenType::Comment)) {
67 90
                continue;
68
            }
69
70
            // No keyword is expected.
71
            if (
72 100
                ($token->type === TokenType::Keyword)
73 100
                && ($token->flags & Token::FLAG_KEYWORD_RESERVED)
74 100
                && ($state === 0)
75
            ) {
76 32
                break;
77
            }
78
79 100
            if ($state === 0) {
80 100
                if (in_array($token->token, ['=', ':='], true)) {
81 100
                    $state = 1;
82 98
                } elseif ($token->value !== ',') {
83 98
                    $expr->column .= $token->token;
84 24
                } elseif ($token->value === ',') {
85 62
                    $commaLastSeenAt = $token;
86
                }
87
            } else {
88 100
                $tmp = Expression::parse(
89 100
                    $parser,
90 100
                    $list,
91 100
                    ['breakOnAlias' => true],
92 100
                );
93 100
                if ($tmp === null) {
94 2
                    $parser->error('Missing expression.', $token);
95 2
                    break;
96
                }
97
98 98
                $expr->column = trim($expr->column);
99 98
                $expr->value = $tmp->expr;
100 98
                $ret[] = $expr;
101 98
                $expr = new SetOperation();
102 98
                $state = 0;
103 98
                $commaLastSeenAt = null;
104
            }
105
        }
106
107 120
        --$list->idx;
108
109
        // We saw a comma, but didn't see a column-value pair after it
110 120
        if ($commaLastSeenAt !== null) {
0 ignored issues
show
introduced by
The condition $commaLastSeenAt !== null is always false.
Loading history...
111 2
            $parser->error('Unexpected token.', $commaLastSeenAt);
112
        }
113
114 120
        return $ret;
115
    }
116
117
    /** @param SetOperation[] $component the component to be built */
118 38
    public static function buildAll(array $component): string
119
    {
120 38
        return implode(', ', $component);
121
    }
122
}
123