Completed
Branch feature/pre-split (f8e7b8)
by Anton
04:02
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
/**
12
 * Compares two table states.
13
 */
14
class StateComparator
15
{
16
    /**
17
     * @var TableState
18
     */
19
    private $initial = null;
20
21
    /**
22
     * @var TableState
23
     */
24
    private $current = null;
25
26
    /**
27
     * @param TableState $initial
28
     * @param TableState $current
29
     */
30
    public function __construct(TableState $initial, TableState $current)
31
    {
32
        $this->initial = $initial;
33
        $this->current = $current;
34
    }
35
36
    /**
37
     * @return bool
38
     */
39
    public function hasChanges(): bool
40
    {
41
        if ($this->current->getName() != $this->initial->getName()) {
42
            return true;
43
        }
44
45
        if ($this->current->getPrimaryKeys() != $this->initial->getPrimaryKeys()) {
46
            return true;
47
        }
48
49
        $difference = [
50
            count($this->addedColumns()),
51
            count($this->droppedColumns()),
52
            count($this->alteredColumns()),
53
            count($this->addedIndexes()),
54
            count($this->droppedIndexes()),
55
            count($this->alteredIndexes()),
56
            count($this->addedForeigns()),
57
            count($this->droppedForeigns()),
58
            count($this->alteredForeigns()),
59
        ];
60
61
        return array_sum($difference) != 0;
62
    }
63
64
    /**
65
     * @return ColumnInterface[]
66
     */
67 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...
68
    {
69
        $difference = [];
70
        foreach ($this->current->getColumns() as $name => $column) {
71
            if (!$this->initial->knowsColumn($name)) {
72
                $difference[] = $column;
73
            }
74
        }
75
76
        return $difference;
77
    }
78
79
    /**
80
     * @return ColumnInterface[]
81
     */
82 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...
83
    {
84
        $difference = [];
85
        foreach ($this->initial->getColumns() as $name => $column) {
86
            if (!$this->current->knowsColumn($name)) {
87
                $difference[] = $column;
88
            }
89
        }
90
91
        return $difference;
92
    }
93
94
    /**
95
     * Returns array where each value contain current and initial element state.
96
     *
97
     * @return array
98
     */
99 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...
100
    {
101
        $difference = [];
102
103
        $initialColumns = $this->initial->getColumns();
104
        foreach ($this->current->getColumns() as $name => $column) {
105
            if (!$this->initial->knowsColumn($name)) {
106
                //Added into schema
107
                continue;
108
            }
109
110
            if (!$column->compare($initialColumns[$name])) {
0 ignored issues
show
Documentation introduced by
$initialColumns[$name] is of type object<Spiral\Database\Schemas\ColumnInterface>, but the function expects a object<self>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
111
                $difference[] = [$column, $initialColumns[$name]];
112
            }
113
        }
114
115
        return $difference;
116
    }
117
118
    /**
119
     * @return IndexInterface[]
120
     */
121 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...
122
    {
123
        $difference = [];
124
        foreach ($this->current->getIndexes() as $name => $index) {
125
            if (!$this->initial->knowsIndex($name)) {
126
                $difference[] = $index;
127
            }
128
        }
129
130
        return $difference;
131
    }
132
133
    /**
134
     * @return IndexInterface[]
135
     */
136 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...
137
    {
138
        $difference = [];
139
        foreach ($this->initial->getIndexes() as $name => $index) {
140
            if (!$this->current->knowsIndex($name)) {
141
                $difference[] = $index;
142
            }
143
        }
144
145
        return $difference;
146
    }
147
148
    /**
149
     * Returns array where each value contain current and initial element state.
150
     *
151
     * @return array
152
     */
153 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...
154
    {
155
        $difference = [];
156
157
        $initialIndexes = $this->initial->getIndexes();
158
        foreach ($this->current->getIndexes() as $name => $index) {
159
            if (!$this->initial->knowsIndex($name)) {
160
                //Added into schema
161
                continue;
162
            }
163
164
            if (!$index->compare($initialIndexes[$name])) {
0 ignored issues
show
Documentation introduced by
$initialIndexes[$name] is of type object<Spiral\Database\Schemas\IndexInterface>, but the function expects a object<self>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
165
                $difference[] = [$index, $initialIndexes[$name]];
166
            }
167
        }
168
169
        return $difference;
170
    }
171
172
    /**
173
     * @return ReferenceInterface[]
174
     */
175 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...
176
    {
177
        $difference = [];
178
        foreach ($this->current->getForeigns() as $name => $foreign) {
179
            if (!$this->initial->knowsForeign($name)) {
180
                $difference[] = $foreign;
181
            }
182
        }
183
184
        return $difference;
185
    }
186
187
    /**
188
     * @return ReferenceInterface[]
189
     */
190 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...
191
    {
192
        $difference = [];
193
        foreach ($this->initial->getForeigns() as $name => $foreign) {
194
            if (!$this->current->knowsForeign($name)) {
195
                $difference[] = $foreign;
196
            }
197
        }
198
199
        return $difference;
200
    }
201
202
    /**
203
     * Returns array where each value contain current and initial element state.
204
     *
205
     * @return array
206
     */
207 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...
208
    {
209
        $difference = [];
210
211
        $initialForeigns = $this->initial->getForeigns();
212
        foreach ($this->current->getForeigns() as $name => $foreign) {
213
            if (!$this->initial->knowsForeign($name)) {
214
                //Added into schema
215
                continue;
216
            }
217
218
            if (!$foreign->compare($initialForeigns[$name])) {
0 ignored issues
show
Documentation introduced by
$initialForeigns[$name] is of type object<Spiral\Database\S...mas\ReferenceInterface>, but the function expects a object<self>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
219
                $difference[] = [$foreign, $initialForeigns[$name]];
220
            }
221
        }
222
223
        return $difference;
224
    }
225
226
    /**
227
     * @return array
228
     */
229
    public function __debugInfo()
230
    {
231
        return [
232
            'name'        => [
233
                'initial' => $this->initial->getName(),
234
                'current' => $this->current->getName(),
235
            ],
236
            'columns'     => [
237
                'added'   => $this->addedColumns(),
238
                'dropped' => $this->droppedColumns(),
239
                'altered' => $this->alteredColumns(),
240
            ],
241
            'indexes'     => [
242
                'added'   => $this->addedIndexes(),
243
                'dropped' => $this->droppedIndexes(),
244
                'altered' => $this->alteredIndexes(),
245
            ],
246
            'foreignKeys' => [
247
                'added'   => $this->addedForeigns(),
248
                'dropped' => $this->droppedForeigns(),
249
                'altered' => $this->alteredForeigns(),
250
            ],
251
        ];
252
    }
253
}
254