Completed
Pull Request — master (#6563)
by Joas
14:47
created

BigIntMigration   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 37
Duplicated Lines 10.81 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 4
loc 37
rs 10
wmc 4
lcom 0
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
getColumnsByTable() 0 1 ?
A changeSchema() 4 20 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * @copyright Copyright (c) 2017 Joas Schilling <[email protected]>
4
 *
5
 * @license GNU AGPL version 3 or any later version
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Affero General Public License as
9
 * published by the Free Software Foundation, either version 3 of the
10
 * License, or (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 *
20
 */
21
22
namespace OCP\Migration;
23
24
use Doctrine\DBAL\Schema\Schema;
25
use Doctrine\DBAL\Types\Type;
26
27
/**
28
 * @since 13.0.0
29
 */
30
abstract class BigIntMigration extends SimpleMigrationStep {
31
32
	/**
33
	 * @return array Returns an array with the following structure
34
	 * ['table1' => ['column1', 'column2'], ...]
35
	 * @since 13.0.0
36
	 */
37
	abstract protected function getColumnsByTable();
38
39
	/**
40
	 * @param IOutput $output
41
	 * @param \Closure $schemaClosure The `\Closure` returns a `Schema`
42
	 * @param array $options
43
	 * @return null|Schema
44
	 * @since 13.0.0
45
	 */
46
	public function changeSchema(IOutput $output, \Closure $schemaClosure, array $options) {
47
		/** @var Schema $schema */
48
		$schema = $schemaClosure();
49
50
		$tables = $this->getColumnsByTable();
51
52
		foreach ($tables as $tableName => $columns) {
53
			$table = $schema->getTable($tableName);
54
55
			foreach ($columns as $columnName) {
56
				$column = $table->getColumn($columnName);
57 View Code Duplication
				if ($column->getType()->getName() !== Type::BIGINT) {
58
					$column->setType(Type::getType(Type::BIGINT));
59
					$column->setOptions(['length' => 20]);
60
				}
61
			}
62
		}
63
64
		return $schema;
65
	}
66
}
67