Passed
Branch add-more-test-schema (1ebac6)
by Wilmer
02:54
created

AbstractSchemaProvider::constraints()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 116
Code Lines 90

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 90
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 116
rs 8.2181

How to fix   Long Method   

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 Yiisoft\Db\Tests\Provider;
6
7
use PDO;
8
use Yiisoft\Db\Constraint\CheckConstraint;
9
use Yiisoft\Db\Constraint\Constraint;
10
use Yiisoft\Db\Constraint\ForeignKeyConstraint;
11
use Yiisoft\Db\Constraint\IndexConstraint;
12
use Yiisoft\Db\Schema\Schema;
13
use Yiisoft\Db\Tests\Support\AnyValue;
14
15
abstract class AbstractSchemaProvider
16
{
17
    public function columns(): array
18
    {
19
        return [];
20
    }
21
22
    public function columnsTypeChar(): array
23
    {
24
        return [
25
            ['char_col', 'char', 100, 'char(100)'],
26
            ['char_col2', 'string', 100, 'varchar(100)'],
27
            ['char_col3', 'text', null, 'text'],
28
        ];
29
    }
30
31
    public function constraints(): array
32
    {
33
        return [
34
            '1: primary key' => [
35
                'T_constraints_1',
36
                Schema::PRIMARY_KEY,
37
                (new Constraint())->name(AnyValue::getInstance())->columnNames(['C_id']),
38
            ],
39
            '1: check' => [
40
                'T_constraints_1',
41
                Schema::CHECKS,
42
                [
43
                    (new CheckConstraint())
44
                        ->name(AnyValue::getInstance())
45
                        ->columnNames(['C_check'])
46
                        ->expression("C_check <> ''"),
47
                ],
48
            ],
49
            '1: unique' => [
50
                'T_constraints_1',
51
                Schema::UNIQUES,
52
                [(new Constraint())->name('CN_unique')->columnNames(['C_unique'])],
53
            ],
54
            '1: index' => [
55
                'T_constraints_1',
56
                Schema::INDEXES,
57
                [
58
                    (new IndexConstraint())
59
                        ->name(AnyValue::getInstance())
60
                        ->columnNames(['C_id'])
61
                        ->unique(true)
62
                        ->primary(true),
63
                    (new IndexConstraint())
64
                        ->name('CN_unique')
65
                        ->columnNames(['C_unique'])
66
                        ->primary(false)
67
                        ->unique(true),
68
                ],
69
            ],
70
            '1: default' => ['T_constraints_1', Schema::DEFAULT_VALUES, false],
71
72
            '2: primary key' => [
73
                'T_constraints_2',
74
                Schema::PRIMARY_KEY,
75
                (new Constraint())->name('CN_pk')->columnNames(['C_id_1', 'C_id_2']),
76
            ],
77
            '2: unique' => [
78
                'T_constraints_2',
79
                Schema::UNIQUES,
80
                [(new Constraint())->name('CN_constraints_2_multi')->columnNames(['C_index_2_1', 'C_index_2_2'])],
81
            ],
82
            '2: index' => [
83
                'T_constraints_2',
84
                Schema::INDEXES,
85
                [
86
                    (new IndexConstraint())
87
                        ->name(AnyValue::getInstance())
88
                        ->columnNames(['C_id_1', 'C_id_2'])
89
                        ->unique(true)
90
                        ->primary(true),
91
                    (new IndexConstraint())
92
                        ->name('CN_constraints_2_single')
93
                        ->columnNames(['C_index_1'])
94
                        ->primary(false)
95
                        ->unique(false),
96
                    (new IndexConstraint())
97
                        ->name('CN_constraints_2_multi')
98
                        ->columnNames(['C_index_2_1', 'C_index_2_2'])
99
                        ->primary(false)
100
                        ->unique(true),
101
                ],
102
            ],
103
            '2: check' => ['T_constraints_2', Schema::CHECKS, []],
104
            '2: default' => ['T_constraints_2', Schema::DEFAULT_VALUES, false],
105
106
            '3: primary key' => ['T_constraints_3', Schema::PRIMARY_KEY, null],
107
            '3: foreign key' => [
108
                'T_constraints_3',
109
                Schema::FOREIGN_KEYS,
110
                [
111
                    (new ForeignKeyConstraint())
112
                        ->name('CN_constraints_3')
113
                        ->columnNames(['C_fk_id_1', 'C_fk_id_2'])
114
                        ->foreignTableName('T_constraints_2')
115
                        ->foreignColumnNames(['C_id_1', 'C_id_2'])
116
                        ->onDelete('CASCADE')
117
                        ->onUpdate('CASCADE'),
118
                ],
119
            ],
120
            '3: unique' => ['T_constraints_3', Schema::UNIQUES, []],
121
            '3: index' => [
122
                'T_constraints_3',
123
                Schema::INDEXES,
124
                [
125
                    (new IndexConstraint())
126
                        ->name('CN_constraints_3')
127
                        ->columnNames(['C_fk_id_1', 'C_fk_id_2'])
128
                        ->unique(false)
129
                        ->primary(false),
130
                ],
131
            ],
132
            '3: check' => ['T_constraints_3', Schema::CHECKS, []],
133
            '3: default' => ['T_constraints_3', Schema::DEFAULT_VALUES, false],
134
135
            '4: primary key' => [
136
                'T_constraints_4',
137
                Schema::PRIMARY_KEY,
138
                (new Constraint())->name(AnyValue::getInstance())->columnNames(['C_id']),
139
            ],
140
            '4: unique' => [
141
                'T_constraints_4',
142
                Schema::UNIQUES,
143
                [(new Constraint())->name('CN_constraints_4')->columnNames(['C_col_1', 'C_col_2'])],
144
            ],
145
            '4: check' => ['T_constraints_4', Schema::CHECKS, []],
146
            '4: default' => ['T_constraints_4', Schema::DEFAULT_VALUES, false],
147
        ];
148
    }
149
150
    public function pdoAttributes(): array
151
    {
152
        return [[[PDO::ATTR_EMULATE_PREPARES => true]], [[PDO::ATTR_EMULATE_PREPARES => false]]];
153
    }
154
155
    public function tableSchema(): array
156
    {
157
        return [
158
            ['negative_default_values', 'negative_default_values'],
159
            ['profile', 'profile'],
160
        ];
161
    }
162
163
    public function tableSchemaCachePrefixes(): array
164
    {
165
        $configs = [
166
            ['prefix' => '', 'name' => 'type'],
167
            ['prefix' => '', 'name' => '{{%type}}'],
168
            ['prefix' => 'ty', 'name' => '{{%pe}}'],
169
        ];
170
171
        $data = [];
172
173
        foreach ($configs as $config) {
174
            foreach ($configs as $testConfig) {
175
                if ($config === $testConfig) {
176
                    continue;
177
                }
178
179
                $description = sprintf(
180
                    "%s (with '%s' prefix) against %s (with '%s' prefix)",
181
                    $config['name'],
182
                    $config['prefix'],
183
                    $testConfig['name'],
184
                    $testConfig['prefix']
185
                );
186
                $data[$description] = [
187
                    $config['prefix'],
188
                    $config['name'],
189
                    $testConfig['prefix'],
190
                    $testConfig['name'],
191
                ];
192
            }
193
        }
194
195
        return $data;
196
    }
197
}
198