Test Failed
Push — master ( fe71c4...38b5f5 )
by Jeroen
02:19
created

DenormalizeMetastrings::up()   C

Complexity

Conditions 12
Paths 83

Size

Total Lines 83
Code Lines 43

Duplication

Lines 26
Ratio 31.33 %

Importance

Changes 0
Metric Value
cc 12
eloc 43
nc 83
nop 0
dl 26
loc 83
rs 5.034
c 0
b 0
f 0

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
use Phinx\Migration\AbstractMigration;
4
5
class DenormalizeMetastrings extends AbstractMigration {
6
	/**
7
	 * Denormlize metastrings
8
	 *
9
	 * - Add "name" and "value" columns to "metadata" table
10
	 * - Populate these new columns from metastrings table by id
11
	 * - Add indexes
12
	 * - Drop "metastrings" table
13
	 */
14
	public function up() {
15
16
		if (!$this->hasTable('metastrings')) {
17
			return;
18
		}
19
20
		$tables = [
21
			'metadata',
22
			'annotations',
23
		];
24
25
		foreach ($tables as $table) {
26
27
			if (!$this->hasTable($table)) {
28
				continue;
29
			}
30
31
			$table = $this->table($table);
32
33 View Code Duplication
			if (!$table->hasColumn('value')) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
34
				$table->addColumn('value', 'text', [
35
					'null' => false,
36
					'after' => 'entity_guid',
37
				]);
38
			}
39
40 View Code Duplication
			if (!$table->hasIndex('value')) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
41
				$table->addIndex(['value'], [
42
					'name' => "value",
43
					'unique' => false,
44
					'limit' => 50,
45
				]);
46
			}
47
48 View Code Duplication
			if (!$table->hasColumn('name')) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
49
				$table->addColumn('name', 'text', [
50
					'null' => false,
51
					'after' => 'entity_guid',
52
				]);
53
			}
54
55 View Code Duplication
			if (!$table->hasIndex('name')) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
56
				$table->addIndex(['name'], [
57
					'name' => "name",
58
					'unique' => false,
59
					'limit' => 50,
60
				]);
61
			}
62
63
			$table->save();
64
65
			if ($table->hasColumn('name_id') && $table->hasColumn('value_id')) {
66
				$prefix = $this->getAdapter()->getOption('table_prefix');
67
68
				// move in all metastrings
69
				$this->query("
70
					UPDATE {$prefix}{$table->getName()} n_table
71
					INNER JOIN {$prefix}metastrings msn ON n_table.name_id = msn.id
72
					INNER JOIN {$prefix}metastrings msv ON n_table.value_id = msv.id
73
					SET n_table.name = msn.string,
74
						n_table.value = msv.string	
75
				");
76
77
				// drop columns and indexes
78
79
				if ($table->hasIndex('name_id')) {
80
					$table->removeIndexByName('name_id');
81
				}
82
83
				if ($table->hasIndex('value_id')) {
84
					$table->removeIndexByName('value_id');
85
				}
86
87
				$table->removeColumn('name_id');
88
				$table->removeColumn('value_id');
89
90
			}
91
92
			$table->save();
93
		}
94
95
		$this->dropTable('metastrings');
96
	}
97
98
	/**
99
	 * Normalize metastrings
100
	 *
101
	 * CREATE TABLE `prefix_metastrings` (
102
	 * `id` int(11) NOT NULL AUTO_INCREMENT,
103
	 * `string` text NOT NULL,
104
	 * PRIMARY KEY (`id`),
105
	 * KEY `string` (`string`(50))
106
	 * ) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
107
	 */
108
	public function down() {
109
110
		if ($this->hasTable('metastrtings')) {
111
			return;
112
		}
113
114
		$table = $this->table('metastrings', [
115
			'engine' => 'MyISAM',
116
			'encoding' => "utf8",
117
			'collation' => "utf8_general_ci",
118
		]);
119
120
		$table->addColumn('string', 'text', [
121
			'null' => false,
122
		]);
123
124
		$table->addIndex(['string'], [
125
			'name' => 'string',
126
			'limit' => 50,
127
		]);
128
129
		$table->save();
130
131
		$prefix = $this->getAdapter()->getOption('table_prefix');
132
133
		foreach ([
134
					 'metadata',
135
					 'annotations'
136
				 ] as $table) {
137
			$table = $this->table($table);
138
139 View Code Duplication
			if (!$table->hasColumn('name_id')) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
140
				$table->addColumn('name_id', 'integer', [
141
					'null' => false,
142
					'limit' => \Phinx\Db\Adapter\MysqlAdapter::INT_REGULAR,
143
					'precision' => 11,
144
				]);
145
			}
146
147 View Code Duplication
			if (!$table->hasColumn('value_id')) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
148
				$table->addColumn('value_id', 'integer', [
149
					'null' => false,
150
					'limit' => \Phinx\Db\Adapter\MysqlAdapter::INT_REGULAR,
151
					'precision' => 11,
152
				]);
153
			}
154
155
			if (!$table->hasIndex('name_id')) {
156
				$table->addIndex(['name_id'], [
157
					'name' => 'name_id',
158
				]);
159
			}
160
161
			if (!$table->hasIndex(['value_id'])) {
162
				$table->addIndex(['value_id'], [
163
					'name' => 'value_id',
164
				]);
165
			}
166
167
			$table->save();
168
169
			$rows = $this->fetchAll("
170
				SELECT name, value 
171
				FROM {$prefix}{$table->getName()}
172
			");
173
174
			foreach ($rows as $row) {
175
				$this->insert('metastrings', [
176
					['string' => $row['name']],
177
					['string' => $row['value']],
178
				]);
179
			}
180
181
			// move in all metastrings
182
			$this->query("
183
				UPDATE {$prefix}{$table->getName()} n_table
184
				INNER JOIN {$prefix}metastrings msn ON n_table.name = msn.string
185
				INNER JOIN {$prefix}metastrings msv ON n_table.value = msv.string
186
				SET n_table.name_id = msn.id,
187
					n_table.value_id = msv.id	
188
			");
189
190
			if ($table->hasIndex('name')) {
191
				$table->removeIndexByName('name');
192
			}
193
194
			if ($table->hasIndex('value')) {
195
				$table->removeIndexByName('value');
196
			}
197
198
			if ($table->hasColumn('name')) {
199
				$table->removeColumn('name');
200
			}
201
202
			if ($table->hasColumn('value')) {
203
				$table->removeColumn('value');
204
			}
205
206
		}
207
	}
208
209
210
}
211