Passed
Pull Request — master (#483)
by
unknown
03:01
created

Array2d::parse()   C

Complexity

Conditions 14
Paths 10

Size

Total Lines 85
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 38
CRAP Score 14

Importance

Changes 0
Metric Value
cc 14
eloc 35
nc 10
nop 3
dl 0
loc 85
ccs 38
cts 38
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;
6
7
use PhpMyAdmin\SqlParser\Component;
8
use PhpMyAdmin\SqlParser\Parser;
9
use PhpMyAdmin\SqlParser\Token;
10
use PhpMyAdmin\SqlParser\TokensList;
11
use PhpMyAdmin\SqlParser\Translator;
12
use RuntimeException;
13
14
use function count;
15
use function sprintf;
16
17
/**
18
 * `VALUES` keyword parser.
19
 */
20
final class Array2d implements Component
21
{
22
    /**
23
     * @param Parser               $parser  the parser that serves as context
24
     * @param TokensList           $list    the list of tokens that are being parsed
25
     * @param array<string, mixed> $options parameters for parsing
26
     *
27
     * @return ArrayObj[]
28
     */
29 90
    public static function parse(Parser $parser, TokensList $list, array $options = []): array
30
    {
31 90
        $ret = [];
32
33
        /**
34
         * The number of values in each set.
35
         *
36
         * @var int
37
         */
38 90
        $count = -1;
39
40
        /**
41
         * The state of the parser.
42
         *
43
         * Below are the states of the parser.
44
         *
45
         *      0 ----------------------[ array ]----------------------> 1
46
         *
47
         *      1 ------------------------[ , ]------------------------> 0
48
         *      1 -----------------------[ else ]----------------------> (END)
49
         *
50
         * @var int
51
         */
52 90
        $state = 0;
53
54 90
        for (; $list->idx < $list->count; ++$list->idx) {
55
            /**
56
             * Token parsed at this moment.
57
             */
58 90
            $token = $list->tokens[$list->idx];
59
60
            // End of statement.
61 90
            if ($token->type === Token::TYPE_DELIMITER) {
62 42
                break;
63
            }
64
65
            // Skipping whitespaces and comments.
66 90
            if (($token->type === Token::TYPE_WHITESPACE) || ($token->type === Token::TYPE_COMMENT)) {
67 82
                continue;
68
            }
69
70
            // No keyword is expected.
71 90
            if (($token->type === Token::TYPE_KEYWORD) && ($token->flags & Token::FLAG_KEYWORD_RESERVED)) {
72 42
                break;
73
            }
74
75 88
            if ($state === 0) {
76 88
                if ($token->value !== '(') {
77 2
                    break;
78
                }
79
80
                /** @var ArrayObj $arr */
81 86
                $arr = ArrayObj::parse($parser, $list, $options);
82 86
                $arrCount = count($arr->values);
83 86
                if ($count === -1) {
84 86
                    $count = $arrCount;
85 10
                } elseif ($arrCount !== $count) {
86 2
                    $parser->error(
87 2
                        sprintf(
88 2
                            Translator::gettext('%1$d values were expected, but found %2$d.'),
89 2
                            $count,
90 2
                            $arrCount
91 2
                        ),
92 2
                        $token
93 2
                    );
94
                }
95
96 86
                $ret[] = $arr;
97 86
                $state = 1;
98 18
            } elseif ($state === 1) {
99 18
                if ($token->value !== ',') {
100 6
                    break;
101
                }
102
103 12
                $state = 0;
104
            }
105
        }
106
107 90
        if ($state === 0) {
0 ignored issues
show
introduced by
The condition $state === 0 is always true.
Loading history...
108 6
            $parser->error('An opening bracket followed by a set of values was expected.', $list->tokens[$list->idx]);
109
        }
110
111 90
        --$list->idx;
112
113 90
        return $ret;
114
    }
115
116
    /**
117
     * @param Array2d $component
118
     */
119
    public static function build($component): string
120
    {
121
        throw new RuntimeException(Translator::gettext('Not implemented yet.'));
122
    }
123
124
    public function __toString(): string
125
    {
126
        return static::build($this);
127
    }
128
}
129