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

Array2d::parse()   C

Complexity

Conditions 14
Paths 10

Size

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