Passed
Pull Request — master (#543)
by
unknown
02:57
created

DataTypes::parse()   C

Complexity

Conditions 13
Paths 20

Size

Total Lines 54
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 13

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 13
eloc 24
nc 20
nop 3
dl 0
loc 54
ccs 25
cts 25
cp 1
crap 13
rs 6.6166
c 1
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\Parsers;
6
7
use PhpMyAdmin\SqlParser\Components\DataType;
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
14
use function strtoupper;
15
16
/**
17
 * Parses a data type.
18
 */
19
final class DataTypes implements Parseable
20
{
21
    /**
22
     * All data type options.
23
     */
24
    private const DATA_TYPE_OPTIONS = [
25
        'BINARY' => 1,
26
        'CHARACTER SET' => [
27
            2,
28
            'var',
29
        ],
30
        'CHARSET' => [
31
            2,
32
            'var',
33
        ],
34
        'COLLATE' => [
35
            3,
36
            'var',
37
        ],
38
        'UNSIGNED' => 4,
39
        'ZEROFILL' => 5,
40
    ];
41
42
    /**
43
     * @param Parser               $parser  the parser that serves as context
44
     * @param TokensList           $list    the list of tokens that are being parsed
45
     * @param array<string, mixed> $options parameters for parsing
46
     */
47 172
    public static function parse(Parser $parser, TokensList $list, array $options = []): DataType|null
48
    {
49 172
        $ret = new DataType();
50
51
        /**
52
         * The state of the parser.
53
         *
54
         * Below are the states of the parser.
55
         *
56
         *      0 -------------------[ data type ]--------------------> 1
57
         *
58
         *      1 ----------------[ size and options ]----------------> 2
59
         */
60 172
        $state = 0;
61
62 172
        for (; $list->idx < $list->count; ++$list->idx) {
63
            /**
64
             * Token parsed at this moment.
65
             */
66 170
            $token = $list->tokens[$list->idx];
67
68
            // Skipping whitespaces and comments.
69 170
            if (($token->type === TokenType::Whitespace) || ($token->type === TokenType::Comment)) {
70 82
                continue;
71
            }
72
73 170
            if ($state === 0) {
74 170
                $ret->name = strtoupper((string) $token->value);
75 170
                if (($token->type !== TokenType::Keyword) || (! ($token->flags & Token::FLAG_KEYWORD_DATA_TYPE))) {
76 4
                    $parser->error('Unrecognized data type.', $token);
77
                }
78
79 170
                $state = 1;
80 168
            } elseif ($state === 1) {
81 168
                if (($token->type === TokenType::Operator) && ($token->value === '(')) {
82 110
                    $parameters = ArrayObjs::parse($parser, $list);
83 110
                    ++$list->idx;
84 110
                    $ret->parameters = ($ret->name === 'ENUM') || ($ret->name === 'SET') ?
85 110
                        $parameters->raw : $parameters->values;
86
                }
87
88 168
                $ret->options = OptionsArrays::parse($parser, $list, self::DATA_TYPE_OPTIONS);
89 168
                ++$list->idx;
90 168
                break;
91
            }
92
        }
93
94 172
        if (empty($ret->name)) {
95 4
            return null;
96
        }
97
98 168
        --$list->idx;
99
100 168
        return $ret;
101
    }
102
}
103