Passed
Pull Request — master (#3233)
by Sergey
12:33
created

SchemaDiff   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 187
Duplicated Lines 0 %

Test Coverage

Coverage 88.24%

Importance

Changes 0
Metric Value
wmc 25
eloc 59
dl 0
loc 187
rs 10
c 0
b 0
f 0
ccs 45
cts 51
cp 0.8824

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A toSaveSql() 0 3 1
A toSql() 0 3 1
F _toSql() 0 76 22
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
    /**
64
     * @var \Doctrine\DBAL\Schema\View[]
65
     */
66
    public $newViews = [];
67
68
    /**
69
     * @var \Doctrine\DBAL\Schema\View[]
70
     */
71
    public $changedViews = [];
72
73
    /**
74
     * @var \Doctrine\DBAL\Schema\View[]
75
     */
76
    public $removedViews = [];
77
78
    /**
79
     * Constructs an SchemaDiff object.
80
     *
81
     * @param Table[]     $newTables
82
     * @param TableDiff[] $changedTables
83
     * @param Table[]     $removedTables
84
     */
85 628
    public function __construct($newTables = [], $changedTables = [], $removedTables = [], ?Schema $fromSchema = null)
86
    {
87 628
        $this->newTables     = $newTables;
88 628
        $this->changedTables = $changedTables;
89 628
        $this->removedTables = $removedTables;
90 628
        $this->fromSchema    = $fromSchema;
91 628
    }
92
93
    /**
94
     * The to save sql mode ensures that the following things don't happen:
95
     *
96
     * 1. Tables are deleted
97
     * 2. Sequences are deleted
98
     * 3. Foreign Keys which reference tables that would otherwise be deleted.
99
     *
100
     * This way it is ensured that assets are deleted which might not be relevant to the metadata schema at all.
101
     *
102
     * @return string[]
103
     */
104 20
    public function toSaveSql(AbstractPlatform $platform)
105
    {
106 20
        return $this->_toSql($platform, true);
107
    }
108
109
    /**
110
     * @return string[]
111
     */
112 28
    public function toSql(AbstractPlatform $platform)
113
    {
114 28
        return $this->_toSql($platform, false);
115
    }
116
117
    /**
118
     * @param bool $saveMode
119
     *
120
     * @return string[]
121
     */
122 48
    protected function _toSql(AbstractPlatform $platform, $saveMode = false)
123
    {
124 48
        $sql = [];
125
126 48
        if ($platform->supportsSchemas()) {
127 28
            foreach ($this->newNamespaces as $newNamespace) {
128 28
                $sql[] = $platform->getCreateSchemaSQL($newNamespace);
129
            }
130
        }
131
132 48
        if ($platform->supportsForeignKeyConstraints() && $saveMode === false) {
133 8
            foreach ($this->orphanedForeignKeys as $orphanedForeignKey) {
134
                $sql[] = $platform->getDropForeignKeySQL($orphanedForeignKey, $orphanedForeignKey->getLocalTable());
135
            }
136
        }
137
138 48
        if ($platform->supportsSequences() === true) {
0 ignored issues
show
introduced by
The condition $platform->supportsSequences() === true is always false.
Loading history...
139 28
            foreach ($this->changedSequences as $sequence) {
140 20
                $sql[] = $platform->getAlterSequenceSQL($sequence);
141
            }
142
143 28
            if ($saveMode === false) {
144 8
                foreach ($this->removedSequences as $sequence) {
145
                    $sql[] = $platform->getDropSequenceSQL($sequence);
146
                }
147
            }
148
149 28
            foreach ($this->newSequences as $sequence) {
150 20
                $sql[] = $platform->getCreateSequenceSQL($sequence);
151
            }
152
        }
153
154 48
        if ($platform->supportsViews() == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
introduced by
The condition $platform->supportsViews() == true is always true.
Loading history...
155 48
            if ($saveMode === false) {
156 28
                foreach ($this->removedViews as $view) {
157
                    $sql[] = $platform->getDropViewSQL($view->getName());
158
                }
159
            }
160 48
            foreach ($this->changedViews as $view) {
161
                $sql[] = $platform->getDropViewSQL($view->getName());
162
                $sql[] = $platform->getCreateViewSQL($view->getName(), $view->getSql());
163
            }
164
165 48
            foreach ($this->newViews as $view) {
166 20
                $sql[] = $platform->getCreateViewSQL($view->getName(), $view->getSql());
167
            }
168
        }
169
170 48
        $foreignKeySql = [];
171 48
        foreach ($this->newTables as $table) {
172 40
            $sql = array_merge(
173 40
                $sql,
174 40
                $platform->getCreateTableSQL($table, AbstractPlatform::CREATE_INDEXES)
175
            );
176
177 40
            if (! $platform->supportsForeignKeyConstraints()) {
178 20
                continue;
179
            }
180
181 20
            foreach ($table->getForeignKeys() as $foreignKey) {
182 20
                $foreignKeySql[] = $platform->getCreateForeignKeySQL($foreignKey, $table);
183
            }
184
        }
185 48
        $sql = array_merge($sql, $foreignKeySql);
186
187 48
        if ($saveMode === false) {
188 28
            foreach ($this->removedTables as $table) {
189
                $sql[] = $platform->getDropTableSQL($table);
190
            }
191
        }
192
193 48
        foreach ($this->changedTables as $tableDiff) {
194 20
            $sql = array_merge($sql, $platform->getAlterTableSQL($tableDiff));
195
        }
196
197 48
        return $sql;
198
    }
199
}
200