Completed
Pull Request — develop (#3533)
by
unknown
16:31 queued 01:26
created

_getPortableTableColumnDefinition()   C

Complexity

Conditions 13
Paths 112

Size

Total Lines 53
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 31
CRAP Score 13.7211

Importance

Changes 0
Metric Value
eloc 40
dl 0
loc 53
ccs 31
cts 37
cp 0.8378
rs 6.5166
c 0
b 0
f 0
cc 13
nc 112
nop 1
crap 13.7211

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