Completed
Pull Request — master (#1358)
by José
03:41 queued 01:45
created

RedshiftAdapter   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 147
Duplicated Lines 30.61 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
F createTable() 32 112 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
            $sortKeyStr = is_array($sortKeys) ? addslashes(implode(',', $sortKeys)) : addslashes($sortKeys);
81
82
            if (!$interleavedSortKey) {
83
                $sql .= sprintf(' compound sortkey (%s) ', $sortKeyStr);
84
            } else {
85
                $sql .= sprintf(' interleaved sortkey (%s) ', $sortKeyStr);
86
            }
87
        }
88
89
        if (!empty($distStyle)) {
90
            $sql .= sprintf(' diststyle %s', $distStyle);
91
        }
92
93
        $sql .= ';';
94
95
        // process column comments
96 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...
97
            foreach ($this->columnsWithComments as $column) {
98
                $sql .= $this->getColumnCommentSqlDefinition($column, $table->getName());
99
            }
100
        }
101
102
        // set the indexes
103
        if (!empty($indexes)) {
104
            foreach ($indexes as $index) {
105
                $sql .= $this->getIndexSqlDefinition($index, $table->getName());
106
            }
107
        }
108
109
        // execute the sql
110
        $this->execute($sql);
111
112
        // process table comments
113 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...
114
            $sql = sprintf(
115
                'COMMENT ON TABLE %s IS %s',
116
                $this->quoteTableName($table->getName()),
117
                $this->getConnection()
118
                     ->quote($options['comment'])
119
            );
120
            $this->execute($sql);
121
        }
122
    }
123
124
    /**
125
     * @param string $tableName Table name
126
     *
127
     * @return array
128
     */
129 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...
130
    {
131
        $schema = $this->getGlobalSchemaName();
132
        $table = $tableName;
133
        if (false !== strpos($tableName, '.')) {
134
            list( $schema, $table ) = explode('.', $tableName);
135
        }
136
137
        return [
138
            'schema' => $schema,
139
            'table'  => $table,
140
        ];
141
    }
142
143
    /**
144
     * Gets the schema name.
145
     *
146
     * @return string
147
     */
148
    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...
149
    {
150
        $options = $this->getOptions();
151
152
        return empty($options['schema']) ? 'public' : $options['schema'];
153
    }
154
}
155