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

OrderKeywords   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 29
dl 0
loc 79
ccs 28
cts 28
cp 1
rs 10
c 0
b 0
f 0
wmc 14

2 Methods

Rating   Name   Duplication   Size   Complexity  
A buildAll() 0 3 1
C parse() 0 64 13
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\OrderKeyword;
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
 * `ORDER BY` keyword parser.
18
 */
19
final class OrderKeywords 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 OrderKeyword[]
27
     */
28 56
    public static function parse(Parser $parser, TokensList $list, array $options = []): array
29
    {
30 56
        $ret = [];
31
32 56
        $expr = new OrderKeyword();
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 56
        $state = 0;
45
46 56
        for (; $list->idx < $list->count; ++$list->idx) {
47
            /**
48
             * Token parsed at this moment.
49
             */
50 56
            $token = $list->tokens[$list->idx];
51
52
            // End of statement.
53 56
            if ($token->type === TokenType::Delimiter) {
54 24
                break;
55
            }
56
57
            // Skipping whitespaces and comments.
58 56
            if (($token->type === TokenType::Whitespace) || ($token->type === TokenType::Comment)) {
59 56
                continue;
60
            }
61
62 56
            if ($state === 0) {
63 56
                $expr->expr = Expression::parse($parser, $list);
64 56
                $state = 1;
65
            } else {
66
                if (
67 44
                    ($token->type === TokenType::Keyword)
68 44
                    && (($token->keyword === 'ASC') || ($token->keyword === 'DESC'))
69
                ) {
70 34
                    $expr->type = $token->keyword;
71 36
                } elseif (($token->type === TokenType::Operator) && ($token->value === ',')) {
72 10
                    if (! empty($expr->expr)) {
73 10
                        $ret[] = $expr;
74
                    }
75
76 10
                    $expr = new OrderKeyword();
77 10
                    $state = 0;
78
                } else {
79 34
                    break;
80
                }
81
            }
82
        }
83
84
        // Last iteration was not processed.
85 56
        if (! empty($expr->expr)) {
86 56
            $ret[] = $expr;
87
        }
88
89 56
        --$list->idx;
90
91 56
        return $ret;
92
    }
93
94
    /** @param OrderKeyword[] $component the component to be built */
95 20
    public static function buildAll(array $component): string
96
    {
97 20
        return implode(', ', $component);
98
    }
99
}
100