Passed
Push — master ( e689ea...fbefdf )
by William
02:57
created

Table::getFields()   C

Complexity

Conditions 13
Paths 40

Size

Total Lines 55
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 27
CRAP Score 13.0076

Importance

Changes 0
Metric Value
cc 13
eloc 28
c 0
b 0
f 0
nc 40
nop 1
dl 0
loc 55
ccs 27
cts 28
cp 0.9643
crap 13.0076
rs 6.6166

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
 * Table utilities.
4
 */
5
6
declare(strict_types=1);
7
8
namespace PhpMyAdmin\SqlParser\Utils;
9
10
use PhpMyAdmin\SqlParser\Statements\CreateStatement;
11
12
use function is_array;
13
use function str_replace;
14
15
/**
16
 * Table utilities.
17
 */
18
class Table
19
{
20
    /**
21
     * Gets the foreign keys of the table.
22
     *
23
     * @param CreateStatement $statement the statement to be processed
24
     *
25
     * @return array
26
     */
27 16
    public static function getForeignKeys($statement)
28
    {
29 16
        if (empty($statement->fields) || (! is_array($statement->fields)) || (! $statement->options->has('TABLE'))) {
30 4
            return [];
31
        }
32
33 12
        $ret = [];
34
35 12
        foreach ($statement->fields as $field) {
36 12
            if (empty($field->key) || ($field->key->type !== 'FOREIGN KEY')) {
37 12
                continue;
38
            }
39
40 8
            $columns = [];
41 8
            foreach ($field->key->columns as $column) {
42 8
                if (! isset($column['name'])) {
43
                    continue;
44
                }
45
46 8
                $columns[] = $column['name'];
47
            }
48
49 2
            $tmp = [
50 8
                'constraint' => $field->name,
51 8
                'index_list' => $columns,
52
            ];
53
54 8
            if (! empty($field->references)) {
55 8
                $tmp['ref_db_name'] = $field->references->table->database;
56 8
                $tmp['ref_table_name'] = $field->references->table->table;
57 8
                $tmp['ref_index_list'] = $field->references->columns;
58
59 8
                $opt = $field->references->options->has('ON UPDATE');
60
61 8
                if ($opt) {
62 8
                    $tmp['on_update'] = str_replace(' ', '_', $opt);
63
                }
64
65 8
                $opt = $field->references->options->has('ON DELETE');
66
67 8
                if ($opt) {
68 4
                    $tmp['on_delete'] = str_replace(' ', '_', $opt);
69
                }
70
            }
71
72 8
            $ret[] = $tmp;
73
        }
74
75 12
        return $ret;
76
    }
77
78
    /**
79
     * Gets fields of the table.
80
     *
81
     * @param CreateStatement $statement the statement to be processed
82
     *
83
     * @return array
84
     */
85 12
    public static function getFields($statement)
86
    {
87 12
        if (empty($statement->fields) || (! is_array($statement->fields)) || (! $statement->options->has('TABLE'))) {
88 4
            return [];
89
        }
90
91 8
        $ret = [];
92
93 8
        foreach ($statement->fields as $field) {
94
            // Skipping keys.
95 8
            if (empty($field->type)) {
96 4
                continue;
97
            }
98
99 8
            $ret[$field->name] = [
100 8
                'type' => $field->type->name,
101
                'timestamp_not_null' => false,
102
            ];
103
104 8
            if (! $field->options) {
105
                continue;
106
            }
107
108 8
            if ($field->type->name === 'TIMESTAMP') {
109 4
                if ($field->options->has('NOT NULL')) {
110 4
                    $ret[$field->name]['timestamp_not_null'] = true;
111
                }
112
            }
113
114 8
            $option = $field->options->has('DEFAULT');
115
116 8
            if ($option) {
117 4
                $ret[$field->name]['default_value'] = $option;
118 4
                if ($option === 'CURRENT_TIMESTAMP') {
119 4
                    $ret[$field->name]['default_current_timestamp'] = true;
120
                }
121
            }
122
123 8
            $option = $field->options->has('ON UPDATE');
124
125 8
            if ($option === 'CURRENT_TIMESTAMP') {
126 4
                $ret[$field->name]['on_update_current_timestamp'] = true;
127
            }
128
129 8
            $option = $field->options->has('AS');
130
131 8
            if (! $option) {
132 8
                continue;
133
            }
134
135 4
            $ret[$field->name]['generated'] = true;
136 4
            $ret[$field->name]['expr'] = $option;
137
        }
138
139 8
        return $ret;
140
    }
141
}
142