Passed
Push — master ( 6b60b4...e29ed1 )
by William
03:22 queued 12s
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
declare(strict_types=1);
4
5
namespace PhpMyAdmin\SqlParser\Utils;
6
7
use PhpMyAdmin\SqlParser\Statements\CreateStatement;
8
9
use function is_array;
10
use function str_replace;
11
12
/**
13
 * Table utilities.
14
 */
15
class Table
16
{
17
    /**
18
     * Gets the foreign keys of the table.
19
     *
20
     * @param CreateStatement $statement the statement to be processed
21
     *
22
     * @return array<int, array<string, mixed[]|string|null>>
23
     */
24 16
    public static function getForeignKeys($statement)
25
    {
26 16
        if (empty($statement->fields) || (! is_array($statement->fields)) || (! $statement->options->has('TABLE'))) {
0 ignored issues
show
Bug introduced by
The method has() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

26
        if (empty($statement->fields) || (! is_array($statement->fields)) || (! $statement->options->/** @scrutinizer ignore-call */ has('TABLE'))) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

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