Passed
Push — master ( b16987...8b6d77 )
by Maurício
03:49 queued 13s
created

Key::parse()   D

Complexity

Conditions 31
Paths 3

Size

Total Lines 131
Code Lines 65

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 60
CRAP Score 31

Importance

Changes 0
Metric Value
cc 31
eloc 65
c 0
b 0
f 0
nc 3
nop 3
dl 0
loc 131
ccs 60
cts 60
cp 1
crap 31
rs 4.1666

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\Context;
9
10
use function implode;
11
use function trim;
12
13
/**
14
 * Parses the definition of a key.
15
 *
16
 * Used for parsing `CREATE TABLE` statement.
17
 */
18
final class Key implements Component
19
{
20
    /**
21
     * The name of this key.
22
     *
23
     * @var string
24
     */
25
    public $name;
26
27
    /**
28
     * The key columns
29
     *
30
     * @var array<int, array<string, int|string>>
31
     * @phpstan-var array{name?: string, length?: int, order?: string}[]
32
     */
33
    public $columns;
34
35
    /**
36
     * The type of this key.
37
     *
38
     * @var string
39
     */
40
    public $type;
41
42
    /**
43
     * The expression if the Key is not using column names
44
     *
45
     * @var string|null
46
     */
47
    public $expr = null;
48
49
    /**
50
     * The options of this key or null if none where found.
51
     *
52
     * @var OptionsArray|null
53
     */
54
    public $options;
55
56
    /**
57
     * @param string                                $name    the name of the key
58
     * @param array<int, array<string, int|string>> $columns the columns covered by this key
59
     * @param string                                $type    the type of this key
60
     * @param OptionsArray                          $options the options of this key
61
     * @phpstan-param array{name?: string, length?: int, order?: string}[] $columns
62
     */
63 68
    public function __construct(
64
        string|null $name = null,
65
        array $columns = [],
66
        string|null $type = null,
67
        OptionsArray|null $options = null,
68
    ) {
69 68
        $this->name = $name;
70 68
        $this->columns = $columns;
71 68
        $this->type = $type;
72 68
        $this->options = $options;
73
    }
74
75 36
    public function build(): string
76
    {
77 36
        $ret = $this->type . ' ';
78 36
        if (! empty($this->name)) {
79 30
            $ret .= Context::escape($this->name) . ' ';
80
        }
81
82 36
        if ($this->expr !== null) {
83 12
            return $ret . '(' . $this->expr . ') ' . $this->options;
84
        }
85
86 26
        $columns = [];
87 26
        foreach ($this->columns as $column) {
88 24
            $tmp = '';
89 24
            if (isset($column['name'])) {
90 24
                $tmp .= Context::escape($column['name']);
91
            }
92
93 24
            if (isset($column['length'])) {
94 10
                $tmp .= '(' . $column['length'] . ')';
95
            }
96
97 24
            if (isset($column['order'])) {
98 8
                $tmp .= ' ' . $column['order'];
99
            }
100
101 24
            $columns[] = $tmp;
102
        }
103
104 26
        $ret .= '(' . implode(',', $columns) . ') ' . $this->options;
105
106 26
        return trim($ret);
107
    }
108
109 12
    public function __toString(): string
110
    {
111 12
        return $this->build();
112
    }
113
}
114