Passed
Push — master ( 0efc01...ef206b )
by Maurício
02:53
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
                $columns[] = $column['name'];
43
            }
44
45 2
            $tmp = [
46 8
                'constraint' => $field->name,
47 8
                'index_list' => $columns,
48
            ];
49
50 8
            if (! empty($field->references)) {
51 8
                $tmp['ref_db_name'] = $field->references->table->database;
52 8
                $tmp['ref_table_name'] = $field->references->table->table;
53 8
                $tmp['ref_index_list'] = $field->references->columns;
54
55 8
                $opt = $field->references->options->has('ON UPDATE');
56
57 8
                if ($opt) {
58 8
                    $tmp['on_update'] = str_replace(' ', '_', $opt);
59
                }
60
61 8
                $opt = $field->references->options->has('ON DELETE');
62
63 8
                if ($opt) {
64 4
                    $tmp['on_delete'] = str_replace(' ', '_', $opt);
65
                }
66
            }
67
68 8
            $ret[] = $tmp;
69
        }
70
71 12
        return $ret;
72
    }
73
74
    /**
75
     * Gets fields of the table.
76
     *
77
     * @param CreateStatement $statement the statement to be processed
78
     *
79
     * @return array
80
     */
81 12
    public static function getFields($statement)
82
    {
83 12
        if (empty($statement->fields) || (! is_array($statement->fields)) || (! $statement->options->has('TABLE'))) {
84 4
            return [];
85
        }
86
87 8
        $ret = [];
88
89 8
        foreach ($statement->fields as $field) {
90
            // Skipping keys.
91 8
            if (empty($field->type)) {
92 4
                continue;
93
            }
94
95 8
            $ret[$field->name] = [
96 8
                'type' => $field->type->name,
97
                'timestamp_not_null' => false,
98
            ];
99
100 8
            if (! $field->options) {
101
                continue;
102
            }
103
104 8
            if ($field->type->name === 'TIMESTAMP') {
105 4
                if ($field->options->has('NOT NULL')) {
106 4
                    $ret[$field->name]['timestamp_not_null'] = true;
107
                }
108
            }
109
110 8
            $option = $field->options->has('DEFAULT');
111
112 8
            if ($option) {
113 4
                $ret[$field->name]['default_value'] = $option;
114 4
                if ($option === 'CURRENT_TIMESTAMP') {
115 4
                    $ret[$field->name]['default_current_timestamp'] = true;
116
                }
117
            }
118
119 8
            $option = $field->options->has('ON UPDATE');
120
121 8
            if ($option === 'CURRENT_TIMESTAMP') {
122 4
                $ret[$field->name]['on_update_current_timestamp'] = true;
123
            }
124
125 8
            $option = $field->options->has('AS');
126
127 8
            if (! $option) {
128 8
                continue;
129
            }
130
131 4
            $ret[$field->name]['generated'] = true;
132 4
            $ret[$field->name]['expr'] = $option;
133
        }
134
135 8
        return $ret;
136
    }
137
}
138