Table::getFields()   C
last analyzed

Complexity

Conditions 13
Paths 40

Size

Total Lines 54
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 28
CRAP Score 13.0069

Importance

Changes 0
Metric Value
cc 13
eloc 27
c 0
b 0
f 0
nc 40
nop 1
dl 0
loc 54
rs 6.6166
ccs 28
cts 29
cp 0.9655
crap 13.0069

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
     * @return list<(string|string[]|null)[]>
0 ignored issues
show
Bug introduced by
The type PhpMyAdmin\SqlParser\Utils\list was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
21
     * @psalm-return list<array{
22
     *  constraint: string|null,
23
     *  index_list: (int|string)[],
24
     *  ref_db_name?: string|null,
25
     *  ref_table_name?: string|null,
26
     *  ref_index_list?: string[],
27
     *  on_update?: string|string[],
28
     *  on_delete?: string|string[],
29
     * }>
30
     */
31 8
    public static function getForeignKeys(CreateStatement $statement): array
32
    {
33 8
        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

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