Failed Conditions
Push — master ( 656579...2742cd )
by Marco
11:55
created

_getPortableTableColumnDefinition()   D

Complexity

Conditions 14
Paths 224

Size

Total Lines 64
Code Lines 48

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 210

Importance

Changes 0
Metric Value
dl 0
loc 64
rs 4.9992
c 0
b 0
f 0
ccs 0
cts 44
cp 0
cc 14
eloc 48
nc 224
nop 1
crap 210

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
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace Doctrine\DBAL\Schema;
21
22
/**
23
 * IBM Db2 Schema Manager.
24
 *
25
 * @link   www.doctrine-project.org
26
 * @since  1.0
27
 * @author Benjamin Eberlei <[email protected]>
28
 */
29
class DB2SchemaManager extends AbstractSchemaManager
30
{
31
    /**
32
     * {@inheritdoc}
33
     *
34
     * Apparently creator is the schema not the user who created it:
35
     * {@link http://publib.boulder.ibm.com/infocenter/dzichelp/v2r2/index.jsp?topic=/com.ibm.db29.doc.sqlref/db2z_sysibmsystablestable.htm}
36
     */
37 1
    public function listTableNames()
38
    {
39 1
        $sql = $this->_platform->getListTablesSQL();
40 1
        $sql .= " AND CREATOR = UPPER('".$this->_conn->getUsername()."')";
41
42 1
        $tables = $this->_conn->fetchAll($sql);
43
44 1
        return $this->filterAssetNames($this->_getPortableTablesList($tables));
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    protected function _getPortableTableColumnDefinition($tableColumn)
51
    {
52
        $tableColumn = array_change_key_case($tableColumn, \CASE_LOWER);
53
54
        $length = null;
55
        $fixed = null;
56
        $unsigned = false;
57
        $scale = false;
58
        $precision = false;
59
60
        $default = null;
61
62
        if (null !== $tableColumn['default'] && 'NULL' != $tableColumn['default']) {
63
            $default = trim($tableColumn['default'], "'");
64
        }
65
66
        $type = $this->_platform->getDoctrineTypeMapping($tableColumn['typename']);
67
68
        if (isset($tableColumn['comment'])) {
69
            $type = $this->extractDoctrineTypeFromComment($tableColumn['comment'], $type);
70
            $tableColumn['comment'] = $this->removeDoctrineTypeFromComment($tableColumn['comment'], $type);
71
        }
72
73
        switch (strtolower($tableColumn['typename'])) {
74
            case 'varchar':
75
                $length = $tableColumn['length'];
76
                $fixed = false;
77
                break;
78
            case 'character':
79
                $length = $tableColumn['length'];
80
                $fixed = true;
81
                break;
82
            case 'clob':
83
                $length = $tableColumn['length'];
84
                break;
85
            case 'decimal':
86
            case 'double':
87
            case 'real':
88
                $scale = $tableColumn['scale'];
89
                $precision = $tableColumn['length'];
90
                break;
91
        }
92
93
        $options = [
94
            'length'        => $length,
95
            'unsigned'      => (bool) $unsigned,
96
            'fixed'         => (bool) $fixed,
97
            'default'       => $default,
98
            'autoincrement' => (boolean) $tableColumn['autoincrement'],
99
            'notnull'       => (bool) ($tableColumn['nulls'] == 'N'),
100
            'scale'         => null,
101
            'precision'     => null,
102
            'comment'       => isset($tableColumn['comment']) && $tableColumn['comment'] !== ''
103
                ? $tableColumn['comment']
104
                : null,
105
            'platformOptions' => [],
106
        ];
107
108
        if ($scale !== null && $precision !== null) {
109
            $options['scale'] = $scale;
110
            $options['precision'] = $precision;
111
        }
112
113
        return new Column($tableColumn['colname'], \Doctrine\DBAL\Types\Type::getType($type), $options);
114
    }
115
116
    /**
117
     * {@inheritdoc}
118
     */
119 1
    protected function _getPortableTablesList($tables)
120
    {
121 1
        $tableNames = [];
122 1
        foreach ($tables as $tableRow) {
123 1
            $tableRow = array_change_key_case($tableRow, \CASE_LOWER);
124 1
            $tableNames[] = $tableRow['name'];
125
        }
126
127 1
        return $tableNames;
128
    }
129
130
    /**
131
     * {@inheritdoc}
132
     */
133
    protected function _getPortableTableIndexesList($tableIndexRows, $tableName = null)
134
    {
135
        foreach ($tableIndexRows as &$tableIndexRow) {
136
            $tableIndexRow = array_change_key_case($tableIndexRow, \CASE_LOWER);
137
            $tableIndexRow['primary'] = (boolean) $tableIndexRow['primary'];
138
        }
139
140
        return parent::_getPortableTableIndexesList($tableIndexRows, $tableName);
141
    }
142
143
    /**
144
     * {@inheritdoc}
145
     */
146 View Code Duplication
    protected function _getPortableTableForeignKeyDefinition($tableForeignKey)
0 ignored issues
show
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...
147
    {
148
        return new ForeignKeyConstraint(
149
            $tableForeignKey['local_columns'],
150
            $tableForeignKey['foreign_table'],
151
            $tableForeignKey['foreign_columns'],
152
            $tableForeignKey['name'],
153
            $tableForeignKey['options']
154
        );
155
    }
156
157
    /**
158
     * {@inheritdoc}
159
     */
160
    protected function _getPortableTableForeignKeysList($tableForeignKeys)
161
    {
162
        $foreignKeys = [];
163
164
        foreach ($tableForeignKeys as $tableForeignKey) {
165
            $tableForeignKey = array_change_key_case($tableForeignKey, \CASE_LOWER);
166
167
            if (!isset($foreignKeys[$tableForeignKey['index_name']])) {
168
                $foreignKeys[$tableForeignKey['index_name']] = [
169
                    'local_columns'   => [$tableForeignKey['local_column']],
170
                    'foreign_table'   => $tableForeignKey['foreign_table'],
171
                    'foreign_columns' => [$tableForeignKey['foreign_column']],
172
                    'name'            => $tableForeignKey['index_name'],
173
                    'options'         => [
174
                        'onUpdate' => $tableForeignKey['on_update'],
175
                        'onDelete' => $tableForeignKey['on_delete'],
176
                    ]
177
                ];
178 View Code Duplication
            } else {
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...
179
                $foreignKeys[$tableForeignKey['index_name']]['local_columns'][] = $tableForeignKey['local_column'];
180
                $foreignKeys[$tableForeignKey['index_name']]['foreign_columns'][] = $tableForeignKey['foreign_column'];
181
            }
182
        }
183
184
        return parent::_getPortableTableForeignKeysList($foreignKeys);
185
    }
186
187
    /**
188
     * {@inheritdoc}
189
     */
190
    protected function _getPortableForeignKeyRuleDef($def)
191
    {
192
        if ($def == "C") {
193
            return "CASCADE";
194
        } elseif ($def == "N") {
195
            return "SET NULL";
196
        }
197
198
        return null;
199
    }
200
201
    /**
202
     * {@inheritdoc}
203
     */
204
    protected function _getPortableViewDefinition($view)
205
    {
206
        $view = array_change_key_case($view, \CASE_LOWER);
207
        // sadly this still segfaults on PDO_IBM, see http://pecl.php.net/bugs/bug.php?id=17199
208
        //$view['text'] = (is_resource($view['text']) ? stream_get_contents($view['text']) : $view['text']);
0 ignored issues
show
Unused Code Comprehensibility introduced by
74% 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...
209
        if (!is_resource($view['text'])) {
210
            $pos = strpos($view['text'], ' AS ');
211
            $sql = substr($view['text'], $pos+4);
212
        } else {
213
            $sql = '';
214
        }
215
216
        return new View($view['name'], $sql);
0 ignored issues
show
Bug introduced by
It seems like $sql can also be of type false; however, parameter $sql of Doctrine\DBAL\Schema\View::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

216
        return new View($view['name'], /** @scrutinizer ignore-type */ $sql);
Loading history...
217
    }
218
}
219