Completed
Pull Request — master (#1358)
by
unknown
01:54
created

RedshiftAdapter   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 145
Duplicated Lines 31.03 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 29
lcom 1
cbo 3
dl 45
loc 145
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
F createTable() 32 110 25
A getSchemaName() 13 13 2
A getGlobalSchemaName() 0 6 2

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
namespace Phinx\Db\Adapter;
4
5
use Phinx\Db\Table\Table;
6
use Phinx\Db\Table\Column;
7
8
class RedshiftAdapter extends PostgresAdapter
9
{
10
	
11
	public function createTable(Table $table, array $columns = [], array $indexes = [])
12
	{
13
		$options = $table->getOptions();
14
		$parts = $this->getSchemaName( $table->getName() );
15
		
16
		// Add the default primary key
17
		if ( !isset( $options['id'] ) || ( isset( $options['id'] ) && $options['id'] === true ) ) {
18
			$column = new Column();
19
			$column->setName( 'id' )
20
			       ->setType( 'integer' )
21
			       ->setIdentity( true );
22
			
23
			array_unshift( $columns, $column );
24
			$options['primary_key'] = 'id';
25
		} elseif ( isset( $options['id'] ) &&
26
			is_string( $options['id'] ) ) {// Handle id => "field_name" to support AUTO_INCREMENT
27
			$column = new Column();
28
			$column->setName( $options['id'] )
29
			       ->setType( 'integer' )
30
			       ->setIdentity( true );
31
			
32
			array_unshift( $columns, $column );
33
			$options['primary_key'] = $options['id'];
34
		}
35
		
36
		// TODO - process table options like collation etc
37
		$sql = 'CREATE TABLE ';
38
		$sql .= $this->quoteTableName( $table->getName() ) . ' (';
39
		
40
		$this->columnsWithComments = [];
41 View Code Duplication
		foreach ( $columns as $column ) {
0 ignored issues
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...
42
			$sql .= $this->quoteColumnName( $column->getName() ) . ' ' . $this->getColumnSqlDefinition( $column ) .
43
				', ';
44
			
45
			// set column comments, if needed
46
			if ( $column->getComment() ) {
47
				$this->columnsWithComments[] = $column;
48
			}
49
		}
50
		
51
		// set the primary key(s)
52 View Code Duplication
		if ( isset( $options['primary_key'] ) ) {
0 ignored issues
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...
53
			$sql = rtrim( $sql );
54
			$sql .= sprintf( ' CONSTRAINT %s PRIMARY KEY (', $this->quoteColumnName( $parts['table'] . '_pkey' ) );
55
			if ( is_string( $options['primary_key'] ) ) { // handle primary_key => 'id'
56
				$sql .= $this->quoteColumnName( $options['primary_key'] );
57
			} elseif ( is_array( $options['primary_key'] ) ) { // handle primary_key => array('tag_id', 'resource_id')
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
58
				$sql .= implode( ',', array_map( [ $this, 'quoteColumnName' ], $options['primary_key'] ) );
59
			}
60
			$sql .= ')';
61
		} else {
62
			$sql = rtrim( $sql, ', ' ); // no primary keys
63
		}
64
		$sql .= ') ';
65
		
66
		// process redshift sortkeys & distkey
67
		$sortKeys = isset( $options['sortkeys'] ) ? $options['sortkeys'] : null;
68
		
69
		$distKey = isset( $options['distkey'] ) ? $options['distkey'] : null;
70
		
71
		$distStyle = isset( $options['diststyle'] ) ? $options['diststyle'] : null;
72
		
73
		$interleavedSortKey = isset( $options['interleaved'] ) ? (bool) $options['interleaved'] : false;
74
		
75
		if ( !empty( $distKey ) ) {
76
			$sql .= ' distkey(' . addslashes( $distKey ) . ')';
77
		}
78
		
79
		if ( !empty( $sortKeys ) ) {
80
			
81
			$sortKeyStr = is_array( $sortKeys ) ? addslashes( implode( ',', $sortKeys ) ) : addslashes( $sortKeys );
82
			
83
			if ( !$interleavedSortKey ) {
84
				$sql .= sprintf( ' compound sortkey (%s) ', $sortKeyStr );
85
			} else {
86
				$sql .= sprintf( ' interleaved sortkey (%s) ', $sortKeyStr );
87
			}
88
		}
89
		
90
		if ( !empty( $distStyle ) ) {
91
			$sql .= sprintf( ' diststyle %s', $distStyle );
92
		}
93
		
94
		$sql .= ';';
95
		
96
		// process column comments
97 View Code Duplication
		if ( !empty( $this->columnsWithComments ) ) {
0 ignored issues
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...
98
			foreach ( $this->columnsWithComments as $column ) {
99
				$sql .= $this->getColumnCommentSqlDefinition( $column, $table->getName() );
100
			}
101
		}
102
		
103
		// set the indexes
104
		if ( !empty( $indexes ) ) {
105
			foreach ( $indexes as $index ) {
106
				$sql .= $this->getIndexSqlDefinition( $index, $table->getName() );
107
			}
108
		}
109
		
110
		// execute the sql
111
		$this->execute( $sql );
112
		
113
		// process table comments
114 View Code Duplication
		if ( isset( $options['comment'] ) ) {
0 ignored issues
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...
115
			$sql = sprintf( 'COMMENT ON TABLE %s IS %s', $this->quoteTableName( $table->getName() ),
116
				$this->getConnection()
117
				     ->quote( $options['comment'] ) );
118
			$this->execute( $sql );
119
		}
120
	}
121
	
122
	/**
123
	 * @param string $tableName Table name
124
	 *
125
	 * @return array
126
	 */
127 View Code Duplication
	private function getSchemaName($tableName)
0 ignored issues
show
Bug introduced by
Consider using a different method name as you override a private method of the parent class.

Overwriting private methods is generally fine as long as you also use private visibility. It might still be preferable for understandability to use a different method name.

Loading history...
Duplication introduced by
This method seems to be duplicated in 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...
128
	{
129
		$schema = $this->getGlobalSchemaName();
130
		$table = $tableName;
131
		if ( false !== strpos( $tableName, '.' ) ) {
132
			list( $schema, $table ) = explode( '.', $tableName );
133
		}
134
		
135
		return [
136
			'schema' => $schema,
137
			'table'  => $table,
138
		];
139
	}
140
	
141
	/**
142
	 * Gets the schema name.
143
	 *
144
	 * @return string
145
	 */
146
	private function getGlobalSchemaName()
0 ignored issues
show
Bug introduced by
Consider using a different method name as you override a private method of the parent class.

Overwriting private methods is generally fine as long as you also use private visibility. It might still be preferable for understandability to use a different method name.

Loading history...
147
	{
148
		$options = $this->getOptions();
149
		
150
		return empty( $options['schema'] ) ? 'public' : $options['schema'];
151
	}
152
}