Completed
Branch feature/pre-split (42159e)
by Anton
05:36
created

StateComparator::__debugInfo()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 17
nc 1
nop 0
dl 0
loc 24
rs 8.9713
c 0
b 0
f 0
1
<?php
2
/**
3
 * Spiral Framework.
4
 *
5
 * @license   MIT
6
 * @author    Anton Titov (Wolfy-J)
7
 */
8
9
namespace Spiral\Database\Schemas;
10
11
use Spiral\Database\Schemas\Prototypes\AbstractColumn;
12
use Spiral\Database\Schemas\Prototypes\AbstractIndex;
13
use Spiral\Database\Schemas\Prototypes\AbstractReference;
14
15
/**
16
 * Compares two table states.
17
 */
18
class StateComparator
19
{
20
    /**
21
     * @var TableState
22
     */
23
    private $initial = null;
24
25
    /**
26
     * @var TableState
27
     */
28
    private $current = null;
29
30
    /**
31
     * @param TableState $initial
32
     * @param TableState $current
33
     */
34
    public function __construct(TableState $initial, TableState $current)
35
    {
36
        $this->initial = $initial;
37
        $this->current = $current;
38
    }
39
40
    /**
41
     * @return bool
42
     */
43
    public function hasChanges(): bool
44
    {
45
        if ($this->isRenamed()) {
46
            return true;
47
        }
48
49
        if ($this->isPrimaryChanged()) {
50
            return true;
51
        }
52
53
        $difference = [
54
            count($this->addedColumns()),
55
            count($this->droppedColumns()),
56
            count($this->alteredColumns()),
57
            count($this->addedIndexes()),
58
            count($this->droppedIndexes()),
59
            count($this->alteredIndexes()),
60
            count($this->addedForeigns()),
61
            count($this->droppedForeigns()),
62
            count($this->alteredForeigns()),
63
        ];
64
65
        return array_sum($difference) != 0;
66
    }
67
68
    /**
69
     * @return bool
70
     */
71
    public function isRenamed(): bool
72
    {
73
        return $this->current->getName() != $this->initial->getName();
74
    }
75
76
    /**
77
     * @return bool
78
     */
79
    public function isPrimaryChanged(): bool
80
    {
81
        return $this->current->getPrimaryKeys() != $this->initial->getPrimaryKeys();
82
    }
83
84
    /**
85
     * @return AbstractColumn[]
86
     */
87 View Code Duplication
    public function addedColumns(): array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
88
    {
89
        $difference = [];
90
91
        $initialColumns = $this->initial->getColumns();
92
        foreach ($this->current->getColumns() as $name => $column) {
93
            if (!isset($initialColumns[$name])) {
94
                $difference[] = $column;
95
            }
96
        }
97
98
        return $difference;
99
    }
100
101
    /**
102
     * @return AbstractColumn[]
103
     */
104 View Code Duplication
    public function droppedColumns(): array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
105
    {
106
        $difference = [];
107
108
        $currentColumns = $this->current->getColumns();
109
        foreach ($this->initial->getColumns() as $name => $column) {
110
            if (!isset($currentColumns[$name])) {
111
                $difference[] = $column;
112
            }
113
        }
114
115
        return $difference;
116
    }
117
118
    /**
119
     * Returns array where each value contain current and initial element state.
120
     *
121
     * @return array
122
     */
123 View Code Duplication
    public function alteredColumns(): array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
124
    {
125
        $difference = [];
126
127
        $initialColumns = $this->initial->getColumns();
128
        foreach ($this->current->getColumns() as $name => $column) {
129
            if (!isset($initialColumns[$name])) {
130
                //Added into schema
131
                continue;
132
            }
133
134
            if (!$column->compare($initialColumns[$name])) {
135
                $difference[] = [$column, $initialColumns[$name]];
136
            }
137
        }
138
139
        return $difference;
140
    }
141
142
    /**
143
     * @return AbstractIndex[]
144
     */
145 View Code Duplication
    public function addedIndexes(): array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
146
    {
147
        $difference = [];
148
        foreach ($this->current->getIndexes() as $name => $index) {
149
            if (!$this->initial->knowsIndex($name)) {
150
                $difference[] = $index;
151
            }
152
        }
153
154
        return $difference;
155
    }
156
157
    /**
158
     * @return AbstractIndex[]
159
     */
160 View Code Duplication
    public function droppedIndexes(): array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
161
    {
162
        $difference = [];
163
        foreach ($this->initial->getIndexes() as $name => $index) {
164
            if (!$this->current->knowsIndex($name)) {
165
                $difference[] = $index;
166
            }
167
        }
168
169
        return $difference;
170
    }
171
172
    /**
173
     * Returns array where each value contain current and initial element state.
174
     *
175
     * @return array
176
     */
177 View Code Duplication
    public function alteredIndexes(): array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
178
    {
179
        $difference = [];
180
181
        $initialIndexes = $this->initial->getIndexes();
182
        foreach ($this->current->getIndexes() as $name => $index) {
183
            if (!$this->initial->knowsIndex($name)) {
184
                //Added into schema
185
                continue;
186
            }
187
188
            if (!$index->compare($initialIndexes[$name])) {
189
                $difference[] = [$index, $initialIndexes[$name]];
190
            }
191
        }
192
193
        return $difference;
194
    }
195
196
    /**
197
     * @return AbstractReference[]
198
     */
199 View Code Duplication
    public function addedForeigns(): array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
200
    {
201
        $difference = [];
202
        foreach ($this->current->getForeigns() as $name => $foreign) {
203
            if (!$this->initial->knowsForeign($name)) {
204
                $difference[] = $foreign;
205
            }
206
        }
207
208
        return $difference;
209
    }
210
211
    /**
212
     * @return AbstractReference[]
213
     */
214 View Code Duplication
    public function droppedForeigns(): array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
215
    {
216
        $difference = [];
217
        foreach ($this->initial->getForeigns() as $name => $foreign) {
218
            if (!$this->current->knowsForeign($name)) {
219
                $difference[] = $foreign;
220
            }
221
        }
222
223
        return $difference;
224
    }
225
226
    /**
227
     * Returns array where each value contain current and initial element state.
228
     *
229
     * @return array
230
     */
231 View Code Duplication
    public function alteredForeigns(): array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
232
    {
233
        $difference = [];
234
235
        $initialForeigns = $this->initial->getForeigns();
236
        foreach ($this->current->getForeigns() as $name => $foreign) {
237
            if (!$this->initial->knowsForeign($name)) {
238
                //Added into schema
239
                continue;
240
            }
241
242
            if (!$foreign->compare($initialForeigns[$name])) {
243
                $difference[] = [$foreign, $initialForeigns[$name]];
244
            }
245
        }
246
247
        return $difference;
248
    }
249
}
250