Passed
Pull Request — master (#288)
by
unknown
03:48
created

SchemaNormalizer::normalizeColumn()   F

Complexity

Conditions 13
Paths 4096

Size

Total Lines 43
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 13
eloc 27
c 1
b 0
f 0
nc 4096
nop 2
dl 0
loc 43
rs 2.45

How to fix   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
namespace TheCodingMachine\TDBM\SchemaVersionControl;
4
5
use Doctrine\DBAL\Schema\Column;
6
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
7
use Doctrine\DBAL\Schema\Index;
8
use Doctrine\DBAL\Schema\Schema;
9
use Doctrine\DBAL\Schema\Table;
10
11
/**
12
 * Database schema normalizer.
13
 *
14
 * Given an instance of Schema, it will construct a deep associative array to describe it. Such an array will then be
15
 * easy to serialize.
16
 */
17
class SchemaNormalizer
18
{
19
    /** @var Schema */
20
    protected $schema;
21
22
    /**
23
     * Normalize a Schema object into an array descriptor
24
     * @param Schema $schema
25
     * @return array
26
     */
27
    public function normalize(Schema $schema): array
28
    {
29
        $this->schema = $schema;
30
        $schemaDesc = [];
31
        $schemaDesc['tables'] = [];
32
        foreach ($schema->getTables() as $table) {
33
            $schemaDesc['tables'][$table->getName()] = $this->normalizeTable($table);
34
        }
35
        return $schemaDesc;
36
    }
37
38
    protected function normalizeTable(Table $table)
39
    {
40
        $tableDesc = [];
41
42
        if ($table->hasPrimaryKey()) {
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\DBAL\Schema\Table::hasPrimaryKey() has been deprecated: Use {@see getPrimaryKey()} instead. ( Ignorable by Annotation )

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

42
        if (/** @scrutinizer ignore-deprecated */ $table->hasPrimaryKey()) {

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
43
            $pk_columns = $table->getPrimaryKey()->getUnquotedColumns();
44
        } else {
45
            $pk_columns = [];
46
        }
47
48
        if ($table->hasOption('comment') && $table->getOption('comment')) {
49
            $tableDesc['comment'] = $table->getOption('comment');
50
        }
51
52
        // list columns
53
        foreach ($table->getColumns() as $columnName => $column) {
54
            $tableDesc['columns'][$column->getName()] = $this->normalizeColumn($column, in_array($column->getName(), $pk_columns));
55
        }
56
57
        // list indexes
58
        foreach ($table->getIndexes() as $index) {
59
            if (!$index->isPrimary()) {
60
                $tableDesc['indexes'][$index->getName()] = $this->normalizeIndex($index);
61
            }
62
        }
63
64
        // list foreign keys
65
        foreach ($table->getForeignKeys() as $foreignKey) {
66
            $tableDesc['foreign_keys'][$foreignKey->getName()] = $this->normalizeForeignKeyConstraint($foreignKey);
67
        }
68
69
        return $tableDesc;
70
    }
71
72
    protected function normalizeColumn(Column $column, bool $isPrimaryKey)
73
    {
74
        $columnDesc = [];
75
        if ($isPrimaryKey) {
76
            $columnDesc['primary_key'] = $isPrimaryKey;
77
        }
78
        $columnDesc['type'] = $column->getType()->getName();
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\DBAL\Types\Type::getName() has been deprecated: this method will be removed in Doctrine DBAL 4.0, use {@see TypeRegistry::lookupName()} instead. ( Ignorable by Annotation )

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

78
        $columnDesc['type'] = /** @scrutinizer ignore-deprecated */ $column->getType()->getName();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
79
        if ($column->getUnsigned()) {
80
            $columnDesc['unsigned'] = $column->getUnsigned();
81
        }
82
        if ($column->getFixed()) {
83
            $columnDesc['fixed'] = $column->getFixed();
84
        }
85
        if ($column->getLength() !== null) {
86
            $columnDesc['length'] = $column->getLength();
87
        }
88
        if ($column->getPrecision() !== 10) {
89
            $columnDesc['precision'] = $column->getPrecision();
90
        }
91
        if ($column->getScale() !== 0) {
92
            $columnDesc['scale'] = $column->getScale();
93
        }
94
        if ($column->getNotnull()) {
95
            $columnDesc['not_null'] = $column->getNotnull();
96
        }
97
        if ($column->getDefault() !== null) {
98
            $columnDesc['default'] = $column->getDefault();
99
        }
100
        if ($column->getAutoincrement()) {
101
            $columnDesc['auto_increment'] = $column->getAutoincrement();
102
        }
103
        if ($column->getComment() !== null) {
104
            $columnDesc['comment'] = $column->getComment();
105
        }
106
        if (!empty($column->getCustomSchemaOptions())) {
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\DBAL\Schema\Col...etCustomSchemaOptions() has been deprecated: Use {@link getPlatformOptions()} instead ( Ignorable by Annotation )

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

106
        if (!empty(/** @scrutinizer ignore-deprecated */ $column->getCustomSchemaOptions())) {

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
107
            $columnDesc['custom'] = $column->getCustomSchemaOptions();
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\DBAL\Schema\Col...etCustomSchemaOptions() has been deprecated: Use {@link getPlatformOptions()} instead ( Ignorable by Annotation )

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

107
            $columnDesc['custom'] = /** @scrutinizer ignore-deprecated */ $column->getCustomSchemaOptions();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
108
        }
109
110
        if (count($columnDesc) > 1) {
111
            return $columnDesc;
112
        }
113
114
        return $columnDesc['type'];
115
    }
116
117
    protected function normalizeForeignKeyConstraint(ForeignKeyConstraint $foreignKeyConstraint)
118
    {
119
        $constraintDesc = [];
120
        if (count($foreignKeyConstraint->getColumns()) > 1) {
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\DBAL\Schema\For...onstraint::getColumns() has been deprecated: Use {@see getLocalColumns()} instead. ( Ignorable by Annotation )

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

120
        if (count(/** @scrutinizer ignore-deprecated */ $foreignKeyConstraint->getColumns()) > 1) {

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
121
            $constraintDesc['columns'] = $foreignKeyConstraint->getColumns();
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\DBAL\Schema\For...onstraint::getColumns() has been deprecated: Use {@see getLocalColumns()} instead. ( Ignorable by Annotation )

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

121
            $constraintDesc['columns'] = /** @scrutinizer ignore-deprecated */ $foreignKeyConstraint->getColumns();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
122
        } else {
123
            $constraintDesc['column'] = $foreignKeyConstraint->getColumns()[0];
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\DBAL\Schema\For...onstraint::getColumns() has been deprecated: Use {@see getLocalColumns()} instead. ( Ignorable by Annotation )

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

123
            $constraintDesc['column'] = /** @scrutinizer ignore-deprecated */ $foreignKeyConstraint->getColumns()[0];

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
124
        }
125
126
        $constraintDesc['references'] = $this->normalizeForeignReference($foreignKeyConstraint);
127
        if (!empty($foreignKeyConstraint->getOptions())) {
128
            $constraintDesc = array_merge($constraintDesc, $foreignKeyConstraint->getOptions());
129
        }
130
        return $constraintDesc;
131
    }
132
133
    protected function normalizeForeignReference(ForeignKeyConstraint $foreignKeyConstraint)
134
    {
135
        $referenceDesc = [];
136
        $foreignTableName = $foreignKeyConstraint->getForeignTableName();
137
        $foreignTable = $this->schema->getTable($foreignTableName);
138
        if ($foreignTable->hasPrimaryKey()
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\DBAL\Schema\Table::hasPrimaryKey() has been deprecated: Use {@see getPrimaryKey()} instead. ( Ignorable by Annotation )

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

138
        if (/** @scrutinizer ignore-deprecated */ $foreignTable->hasPrimaryKey()

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
139
            && $foreignTable->getPrimaryKeyColumns() == $foreignKeyConstraint->getForeignColumns()) {
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\DBAL\Schema\Table::getPrimaryKeyColumns() has been deprecated: Use {@see getPrimaryKey()} and {@see Index::getColumns()} instead. ( Ignorable by Annotation )

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

139
            && /** @scrutinizer ignore-deprecated */ $foreignTable->getPrimaryKeyColumns() == $foreignKeyConstraint->getForeignColumns()) {

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
140
            $referenceDesc = $foreignKeyConstraint->getForeignTableName();
141
        } else {
142
            $referenceDesc['table'] = $foreignKeyConstraint->getForeignTableName();
143
            $fkColumns = $foreignKeyConstraint->getForeignColumns();
144
            if (count($fkColumns) > 1) {
145
                $referenceDesc['columns'] = $fkColumns;
146
            } else {
147
                $referenceDesc['column'] = $fkColumns[0];
148
            }
149
        }
150
        return $referenceDesc;
151
    }
152
153
    protected function normalizeIndex(Index $index)
154
    {
155
        $indexDesc = [];
156
        $columns = $index->getColumns();
157
        if (count($columns) > 1) {
158
            $indexDesc['columns'] = $index->getColumns();
159
        } else {
160
            $indexDesc['column'] = $index->getColumns()[0];
161
        }
162
        if ($index->isUnique()) {
163
            $indexDesc['unique'] = $index->isUnique();
164
        }
165
        if ($index->isPrimary()) {
166
            $indexDesc['primary'] = $index->isPrimary();
167
        }
168
        if (!empty($index->getOptions())) {
169
            $indexDesc = array_merge($indexDesc, $index->getOptions());
170
        }
171
        return $indexDesc;
172
    }
173
}
174