Passed
Push — master ( 9e99de...aeb52d )
by William
03:32 queued 13s
created

ExpressionArray::build()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpMyAdmin\SqlParser\Components;
6
7
use PhpMyAdmin\SqlParser\Component;
8
use PhpMyAdmin\SqlParser\Exceptions\ParserException;
9
use PhpMyAdmin\SqlParser\Parser;
10
use PhpMyAdmin\SqlParser\Token;
11
use PhpMyAdmin\SqlParser\TokensList;
12
13
use function count;
14
use function implode;
15
use function preg_match;
16
use function strlen;
17
use function substr;
18
19
/**
20
 * Parses a list of expressions delimited by a comma.
21
 */
22
final class ExpressionArray implements Component
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 Expression[]
30
     *
31
     * @throws ParserException
32
     */
33 506
    public static function parse(Parser $parser, TokensList $list, array $options = []): array
34
    {
35 506
        $ret = [];
36
37
        /**
38
         * The state of the parser.
39
         *
40
         * Below are the states of the parser.
41
         *
42
         *      0 ----------------------[ array ]---------------------> 1
43
         *
44
         *      1 ------------------------[ , ]------------------------> 0
45
         *      1 -----------------------[ else ]----------------------> (END)
46
         *
47
         * @var int
48
         */
49 506
        $state = 0;
50
51 506
        for (; $list->idx < $list->count; ++$list->idx) {
52
            /**
53
             * Token parsed at this moment.
54
             */
55 506
            $token = $list->tokens[$list->idx];
56
57
            // End of statement.
58 506
            if ($token->type === Token::TYPE_DELIMITER) {
59 230
                break;
60
            }
61
62
            // Skipping whitespaces and comments.
63 500
            if (($token->type === Token::TYPE_WHITESPACE) || ($token->type === Token::TYPE_COMMENT)) {
64 384
                continue;
65
            }
66
67
            if (
68 500
                ($token->type === Token::TYPE_KEYWORD)
69 500
                && ($token->flags & Token::FLAG_KEYWORD_RESERVED)
70 500
                && ((~$token->flags & Token::FLAG_KEYWORD_FUNCTION))
71 500
                && ($token->value !== 'DUAL')
72 500
                && ($token->value !== 'NULL')
73 500
                && ($token->value !== 'CASE')
74 500
                && ($token->value !== 'NOT')
75
            ) {
76
                // No keyword is expected.
77 420
                break;
78
            }
79
80 490
            if ($state === 0) {
81 490
                if ($token->type === Token::TYPE_KEYWORD && $token->value === 'CASE') {
82 38
                    $expr = CaseExpression::parse($parser, $list, $options);
83
                } else {
84 488
                    $expr = Expression::parse($parser, $list, $options);
85
                }
86
87 490
                if ($expr === null) {
88 2
                    break;
89
                }
90
91 488
                $ret[] = $expr;
92 488
                $state = 1;
93 190
            } elseif ($state === 1) {
94 190
                if ($token->value !== ',') {
95 20
                    break;
96
                }
97
98 180
                $state = 0;
99
            }
100
        }
101
102 506
        if ($state === 0) {
0 ignored issues
show
introduced by
The condition $state === 0 is always true.
Loading history...
103 20
            $parser->error('An expression was expected.', $list->tokens[$list->idx]);
104
        }
105
106 506
        --$list->idx;
107 506
        $retIndex = count($ret) - 1;
108 506
        if (isset($ret[$retIndex])) {
109 488
            $expr = $ret[$retIndex]->expr;
110 488
            if (preg_match('/\s*--\s.*$/', $expr, $matches)) {
111 4
                $found = $matches[0];
112 4
                $ret[$retIndex]->expr = substr($expr, 0, strlen($expr) - strlen($found));
113
            }
114
        }
115
116 506
        return $ret;
117
    }
118
119
    /**
120
     * @param Expression[]         $component the component to be built
121
     * @param array<string, mixed> $options   parameters for building
122
     */
123 76
    public static function build($component, array $options = []): string
124
    {
125 76
        $ret = [];
126 76
        foreach ($component as $frag) {
127 76
            $ret[] = $frag::build($frag);
128
        }
129
130 76
        return implode(', ', $ret);
131
    }
132
133
    public function __toString(): string
134
    {
135
        return static::build($this);
0 ignored issues
show
Bug introduced by
$this of type PhpMyAdmin\SqlParser\Components\ExpressionArray is incompatible with the type PhpMyAdmin\SqlParser\Components\Expression[] expected by parameter $component of PhpMyAdmin\SqlParser\Com...xpressionArray::build(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

135
        return static::build(/** @scrutinizer ignore-type */ $this);
Loading history...
136
    }
137
}
138