Completed
Pull Request — master (#3885)
by Grégoire
37:52 queued 34:57
created

_getPortableTableColumnDefinition()   C

Complexity

Conditions 14
Paths 168

Size

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