Failed Conditions
Push — master ( 01c22b...e42c1f )
by Marco
79:13 queued 10s
created

_getPortableTableColumnDefinition()   C

Complexity

Conditions 14
Paths 168

Size

Total Lines 57
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 39
CRAP Score 14

Importance

Changes 0
Metric Value
eloc 42
c 0
b 0
f 0
dl 0
loc 57
ccs 39
cts 39
cp 1
rs 5.7
cc 14
nc 168
nop 1
crap 14

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
declare(strict_types=1);
4
5
namespace Doctrine\DBAL\Schema;
6
7
use Doctrine\DBAL\Platforms\DB2Platform;
8
use Doctrine\DBAL\Types\Type;
9
use const CASE_LOWER;
10
use function array_change_key_case;
11
use function is_resource;
12
use function preg_match;
13
use function str_replace;
14
use function strpos;
15
use function strtolower;
16
use function substr;
17
18
/**
19
 * IBM Db2 Schema Manager.
20
 */
21
class DB2SchemaManager extends AbstractSchemaManager
22
{
23
    /**
24
     * {@inheritdoc}
25
     *
26
     * Apparently creator is the schema not the user who created it:
27 194
     * {@link http://publib.boulder.ibm.com/infocenter/dzichelp/v2r2/index.jsp?topic=/com.ibm.db29.doc.sqlref/db2z_sysibmsystablestable.htm}
28
     */
29 194
    public function listTableNames() : array
30 194
    {
31
        $sql = $this->_platform->getListTablesSQL() . ' AND CREATOR = CURRENT_USER';
32 194
33
        $tables = $this->_conn->fetchAll($sql);
34 194
35
        return $this->filterAssetNames($this->_getPortableTablesList($tables));
36
    }
37
38
    /**
39
     * {@inheritdoc}
40 57
     */
41
    protected function _getPortableTableColumnDefinition(array $tableColumn) : Column
42 57
    {
43
        $tableColumn = array_change_key_case($tableColumn, CASE_LOWER);
44 57
45 57
        $length = $precision = $default = null;
46 57
        $scale  = 0;
47 57
        $fixed  = false;
48
49 57
        if ($tableColumn['default'] !== null && $tableColumn['default'] !== 'NULL') {
50
            $default = $tableColumn['default'];
51 57
52 57
            if (preg_match('/^\'(.*)\'$/s', $default, $matches)) {
53
                $default = str_replace("''", "'", $matches[1]);
54 57
            }
55 56
        }
56
57
        $type = $this->extractDoctrineTypeFromComment($tableColumn['comment'])
58
            ?? $this->_platform->getDoctrineTypeMapping($tableColumn['typename']);
59 57
60
        switch (strtolower($tableColumn['typename'])) {
61 57
            case 'varchar':
62 56
                $length = $tableColumn['length'];
63 56
                break;
64
            case 'character':
65
                $length = $tableColumn['length'];
66 57
                $fixed  = true;
67 57
                break;
68 54
            case 'clob':
69 54
                $length = $tableColumn['length'];
70 54
                break;
71 57
            case 'decimal':
72 54
            case 'double':
73 54
            case 'real':
74 54
                $scale     = $tableColumn['scale'];
75 57
                $precision = $tableColumn['length'];
76 54
                break;
77 54
        }
78 57
79 57
        $options = [
80 57
            'length'        => $length,
81 53
            'unsigned'      => false,
82 53
            'fixed'         => $fixed,
83 57
            'default'       => $default,
84
            'autoincrement' => (bool) $tableColumn['autoincrement'],
85
            'notnull'       => $tableColumn['nulls'] === 'N',
86
            'comment'       => isset($tableColumn['comment']) && $tableColumn['comment'] !== ''
87 57
                ? $tableColumn['comment']
88
                : null,
89 57
            'platformOptions' => [],
90 57
        ];
91 57
92 57
        if ($scale !== null && $precision !== null) {
93
            $options['scale']     = $scale;
94
            $options['precision'] = $precision;
95 57
        }
96 55
97
        return new Column($tableColumn['colname'], Type::getType($type), $options);
98
    }
99
100
    /**
101 57
     * {@inheritdoc}
102 57
     */
103 57
    protected function _getPortableTablesList(array $tables) : array
104
    {
105
        $tableNames = [];
106 57
        foreach ($tables as $tableRow) {
107
            $tableRow     = array_change_key_case($tableRow, CASE_LOWER);
108
            $tableNames[] = $tableRow['name'];
109
        }
110
111
        return $tableNames;
112 194
    }
113
114 194
    /**
115 194
     * {@inheritdoc}
116 194
     */
117 194
    protected function _getPortableTableIndexesList(array $tableIndexRows, string $tableName) : array
118
    {
119
        foreach ($tableIndexRows as &$tableIndexRow) {
120 194
            $tableIndexRow            = array_change_key_case($tableIndexRow, CASE_LOWER);
121
            $tableIndexRow['primary'] = (bool) $tableIndexRow['primary'];
122
        }
123
124
        return parent::_getPortableTableIndexesList($tableIndexRows, $tableName);
125
    }
126 57
127
    /**
128 57
     * {@inheritdoc}
129 54
     */
130 54
    protected function _getPortableTableForeignKeyDefinition(array $tableForeignKey) : ForeignKeyConstraint
131
    {
132
        return new ForeignKeyConstraint(
133 57
            $tableForeignKey['local_columns'],
134
            $tableForeignKey['foreign_table'],
135
            $tableForeignKey['foreign_columns'],
136
            $tableForeignKey['name'],
137
            $tableForeignKey['options']
138
        );
139 46
    }
140
141 46
    /**
142 46
     * {@inheritdoc}
143 46
     */
144 46
    protected function _getPortableTableForeignKeysList(array $tableForeignKeys) : array
145 46
    {
146 46
        $foreignKeys = [];
147
148
        foreach ($tableForeignKeys as $tableForeignKey) {
149
            $tableForeignKey = array_change_key_case($tableForeignKey, CASE_LOWER);
150
151
            if (! isset($foreignKeys[$tableForeignKey['index_name']])) {
152
                $foreignKeys[$tableForeignKey['index_name']] = [
153 57
                    'local_columns'   => [$tableForeignKey['local_column']],
154
                    'foreign_table'   => $tableForeignKey['foreign_table'],
155 57
                    'foreign_columns' => [$tableForeignKey['foreign_column']],
156
                    'name'            => $tableForeignKey['index_name'],
157 57
                    'options'         => [
158 46
                        'onUpdate' => $tableForeignKey['on_update'],
159
                        'onDelete' => $tableForeignKey['on_delete'],
160 46
                    ],
161 46
                ];
162 46
            } else {
163 46
                $foreignKeys[$tableForeignKey['index_name']]['local_columns'][]   = $tableForeignKey['local_column'];
164 46
                $foreignKeys[$tableForeignKey['index_name']]['foreign_columns'][] = $tableForeignKey['foreign_column'];
165 46
            }
166
        }
167 46
168 46
        return parent::_getPortableTableForeignKeysList($foreignKeys);
169
    }
170
171
    /**
172 32
     * {@inheritdoc}
173 32
     */
174
    protected function _getPortableViewDefinition(array $view) : View
175
    {
176
        $view = array_change_key_case($view, CASE_LOWER);
177 57
        // sadly this still segfaults on PDO_IBM, see http://pecl.php.net/bugs/bug.php?id=17199
178
        //$view['text'] = (is_resource($view['text']) ? stream_get_contents($view['text']) : $view['text']);
179
        if (! is_resource($view['text'])) {
180
            $pos = strpos($view['text'], ' AS ');
181
            $sql = substr($view['text'], $pos+4);
182
        } else {
183
            $sql = '';
184
        }
185
186
        return new View($view['name'], $sql);
187
    }
188
189
    public function listTableDetails(string $tableName) : Table
190
    {
191
        $table = parent::listTableDetails($tableName);
192
193
        /** @var DB2Platform $platform */
194
        $platform = $this->_platform;
195
        $sql      = $platform->getListTableCommentsSQL($tableName);
196
197
        $tableOptions = $this->_conn->fetchAssoc($sql);
198
        $table->addOption('comment', $tableOptions['REMARKS']);
199 42
200
        return $table;
201 42
    }
202
}
203