Failed Conditions
Pull Request — master (#3233)
by Sergey
64:48
created

SchemaDiff::_toSql()   F

Complexity

Conditions 22
Paths 432

Size

Total Lines 76
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 27
CRAP Score 22

Importance

Changes 0
Metric Value
eloc 40
dl 0
loc 76
ccs 27
cts 27
cp 1
rs 0.7887
c 0
b 0
f 0
cc 22
nc 432
nop 2
crap 22

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
namespace Doctrine\DBAL\Schema;
4
5
use Doctrine\DBAL\Platforms\AbstractPlatform;
6
use function array_merge;
7
8
/**
9
 * Schema Diff.
10
 */
11
class SchemaDiff
12
{
13
    /** @var Schema */
14
    public $fromSchema;
15
16
    /**
17
     * All added namespaces.
18
     *
19
     * @var string[]
20
     */
21
    public $newNamespaces = [];
22
23
    /**
24
     * All removed namespaces.
25
     *
26
     * @var string[]
27
     */
28
    public $removedNamespaces = [];
29
30
    /**
31
     * All added tables.
32
     *
33
     * @var Table[]
34
     */
35
    public $newTables = [];
36
37
    /**
38
     * All changed tables.
39
     *
40
     * @var TableDiff[]
41
     */
42
    public $changedTables = [];
43
44
    /**
45
     * All removed tables.
46
     *
47
     * @var Table[]
48
     */
49
    public $removedTables = [];
50
51
    /** @var Sequence[] */
52
    public $newSequences = [];
53
54
    /** @var Sequence[] */
55
    public $changedSequences = [];
56
57
    /** @var Sequence[] */
58
    public $removedSequences = [];
59
60
    /** @var ForeignKeyConstraint[] */
61
    public $orphanedForeignKeys = [];
62
63
    /** @var View[] */
64
    public $newViews = [];
65
66
    /** @var View[] */
67
    public $changedViews = [];
68
69
    /** @var View[] */
70 559
    public $removedViews = [];
71
72 559
    /**
73 559
     * Constructs an SchemaDiff object.
74 559
     *
75 559
     * @param Table[]     $newTables
76 559
     * @param TableDiff[] $changedTables
77
     * @param Table[]     $removedTables
78
     */
79
    public function __construct($newTables = [], $changedTables = [], $removedTables = [], ?Schema $fromSchema = null)
80
    {
81
        $this->newTables     = $newTables;
82
        $this->changedTables = $changedTables;
83
        $this->removedTables = $removedTables;
84
        $this->fromSchema    = $fromSchema;
85
    }
86
87
    /**
88
     * The to save sql mode ensures that the following things don't happen:
89 19
     *
90
     * 1. Tables are deleted
91 19
     * 2. Sequences are deleted
92
     * 3. Foreign Keys which reference tables that would otherwise be deleted.
93
     *
94
     * This way it is ensured that assets are deleted which might not be relevant to the metadata schema at all.
95
     *
96
     * @return string[]
97 46
     */
98
    public function toSaveSql(AbstractPlatform $platform)
99 46
    {
100
        return $this->_toSql($platform, true);
101
    }
102
103
    /**
104
     * @return string[]
105
     */
106
    public function toSql(AbstractPlatform $platform)
107 65
    {
108
        return $this->_toSql($platform, false);
109 65
    }
110
111 65
    /**
112 46
     * @param bool $saveMode
113 46
     *
114
     * @return string[]
115
     */
116
    protected function _toSql(AbstractPlatform $platform, $saveMode = false)
117 65
    {
118 27
        $sql = [];
119 19
120
        if ($platform->supportsSchemas()) {
121
            foreach ($this->newNamespaces as $newNamespace) {
122
                $sql[] = $platform->getCreateSchemaSQL($newNamespace);
123 65
            }
124 46
        }
125 38
126
        if ($platform->supportsForeignKeyConstraints() && $saveMode === false) {
127
            foreach ($this->orphanedForeignKeys as $orphanedForeignKey) {
128 46
                $sql[] = $platform->getDropForeignKeySQL($orphanedForeignKey, $orphanedForeignKey->getLocalTable());
129 27
            }
130 19
        }
131
132
        if ($platform->supportsSequences() === true) {
0 ignored issues
show
introduced by
The condition $platform->supportsSequences() === true is always false.
Loading history...
133
            foreach ($this->changedSequences as $sequence) {
134 46
                $sql[] = $platform->getAlterSequenceSQL($sequence);
135 38
            }
136
137
            if ($saveMode === false) {
138
                foreach ($this->removedSequences as $sequence) {
139 65
                    $sql[] = $platform->getDropSequenceSQL($sequence);
140 65
                }
141 57
            }
142 57
143 57
            foreach ($this->newSequences as $sequence) {
144
                $sql[] = $platform->getCreateSequenceSQL($sequence);
145
            }
146 57
        }
147 19
148
        if ($platform->supportsViews()) {
149
            if (! $saveMode) {
150 38
                foreach ($this->removedViews as $view) {
151 38
                    $sql[] = $platform->getDropViewSQL($view->getName());
152
                }
153
            }
154 65
            foreach ($this->changedViews as $view) {
155
                $sql[] = $platform->getDropViewSQL($view->getName());
156 65
                $sql[] = $platform->getCreateViewSQL($view->getName(), $view->getSql());
157 46
            }
158 19
159
            foreach ($this->newViews as $view) {
160
                $sql[] = $platform->getCreateViewSQL($view->getName(), $view->getSql());
161
            }
162 65
        }
163 38
164
        $foreignKeySql = [];
165
        foreach ($this->newTables as $table) {
166 65
            $sql = array_merge(
167
                $sql,
168
                $platform->getCreateTableSQL($table, AbstractPlatform::CREATE_INDEXES)
169
            );
170
171
            if (! $platform->supportsForeignKeyConstraints()) {
172
                continue;
173
            }
174
175
            foreach ($table->getForeignKeys() as $foreignKey) {
176
                $foreignKeySql[] = $platform->getCreateForeignKeySQL($foreignKey, $table);
177
            }
178
        }
179
        $sql = array_merge($sql, $foreignKeySql);
180
181
        if ($saveMode === false) {
182
            foreach ($this->removedTables as $table) {
183
                $sql[] = $platform->getDropTableSQL($table);
184
            }
185
        }
186
187
        foreach ($this->changedTables as $tableDiff) {
188
            $sql = array_merge($sql, $platform->getAlterTableSQL($tableDiff));
189
        }
190
191
        return $sql;
192
    }
193
}
194