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

GroupKeywords::parse()   C

Complexity

Conditions 13
Paths 6

Size

Total Lines 64
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 26
CRAP Score 13

Importance

Changes 0
Metric Value
cc 13
eloc 28
nc 6
nop 3
dl 0
loc 64
ccs 26
cts 26
cp 1
crap 13
rs 6.6166
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\GroupKeyword;
9
use PhpMyAdmin\SqlParser\Parseable;
10
use PhpMyAdmin\SqlParser\Parser;
11
use PhpMyAdmin\SqlParser\TokensList;
12
use PhpMyAdmin\SqlParser\TokenType;
13
14
use function implode;
15
16
/**
17
 * `GROUP BY` keyword parser.
18
 */
19
final class GroupKeywords implements Parseable
20
{
21
    /**
22
     * @param Parser               $parser  the parser that serves as context
23
     * @param TokensList           $list    the list of tokens that are being parsed
24
     * @param array<string, mixed> $options parameters for parsing
25
     *
26
     * @return GroupKeyword[]
27
     */
28 38
    public static function parse(Parser $parser, TokensList $list, array $options = []): array
29
    {
30 38
        $ret = [];
31
32 38
        $expr = new GroupKeyword();
33
34
        /**
35
         * The state of the parser.
36
         *
37
         * Below are the states of the parser.
38
         *
39
         *      0 --------------------[ expression ]-------------------> 1
40
         *
41
         *      1 ------------------------[ , ]------------------------> 0
42
         *      1 -------------------[ ASC / DESC ]--------------------> 1
43
         */
44 38
        $state = 0;
45
46 38
        for (; $list->idx < $list->count; ++$list->idx) {
47
            /**
48
             * Token parsed at this moment.
49
             */
50 38
            $token = $list->tokens[$list->idx];
51
52
            // End of statement.
53 38
            if ($token->type === TokenType::Delimiter) {
54 16
                break;
55
            }
56
57
            // Skipping whitespaces and comments.
58 38
            if (($token->type === TokenType::Whitespace) || ($token->type === TokenType::Comment)) {
59 38
                continue;
60
            }
61
62 38
            if ($state === 0) {
63 38
                $expr->expr = Expression::parse($parser, $list);
64 38
                $state = 1;
65
            } else {
66
                if (
67 30
                    ($token->type === TokenType::Keyword)
68 30
                    && (($token->keyword === 'ASC') || ($token->keyword === 'DESC'))
69
                ) {
70 2
                    $expr->type = $token->keyword;
71 30
                } elseif (($token->type === TokenType::Operator) && ($token->value === ',')) {
72 14
                    if (! empty($expr->expr)) {
73 14
                        $ret[] = $expr;
74
                    }
75
76 14
                    $expr = new GroupKeyword();
77 14
                    $state = 0;
78
                } else {
79 24
                    break;
80
                }
81
            }
82
        }
83
84
        // Last iteration was not processed.
85 38
        if (! empty($expr->expr)) {
86 38
            $ret[] = $expr;
87
        }
88
89 38
        --$list->idx;
90
91 38
        return $ret;
92
    }
93
94
    /** @param GroupKeyword[] $component the component to be built */
95 32
    public static function buildAll(array $component): string
96
    {
97 32
        return implode(', ', $component);
98
    }
99
}
100