Completed
Branch feature/pre-split (7b42f5)
by Anton
03:44
created

Comparator::alteredForeigns()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 9

Duplication

Lines 18
Ratio 100 %

Importance

Changes 0
Metric Value
cc 4
eloc 9
nc 4
nop 0
dl 18
loc 18
rs 9.2
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\Core\Component;
12
13
/**
14
 * Compares two table states.
15
 */
16
class Comparator extends Component
17
{
18
    /**
19
     * @var TableState
20
     */
21
    private $initial = null;
22
23
    /**
24
     * @var TableState
25
     */
26
    private $current = null;
27
28
    /**
29
     * @param TableState $initial
30
     * @param TableState $current
31
     */
32
    public function __construct(TableState $initial, TableState $current)
33
    {
34
        $this->initial = $initial;
35
        $this->current = $current;
36
    }
37
38
    /**
39
     * @return bool
40
     */
41
    public function hasChanges()
42
    {
43
        if ($this->current->getName() != $this->initial->getName()) {
44
            return true;
45
        }
46
47
        if ($this->current->getPrimaryKeys() != $this->initial->getPrimaryKeys()) {
48
            return true;
49
        }
50
51
        $difference = [
52
            count($this->addedColumns()),
53
            count($this->droppedColumns()),
54
            count($this->alteredColumns()),
55
            count($this->addedIndexes()),
56
            count($this->droppedIndexes()),
57
            count($this->alteredIndexes()),
58
            count($this->addedForeigns()),
59
            count($this->droppedForeigns()),
60
            count($this->alteredForeigns()),
61
        ];
62
63
        return array_sum($difference) != 0;
64
    }
65
66
    /**
67
     * @return AbstractColumn[]
68
     */
69 View Code Duplication
    public function addedColumns()
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...
70
    {
71
        $difference = [];
72
        foreach ($this->current->getColumns() as $name => $column) {
73
            if (!$this->initial->knowsColumn($name)) {
74
                $difference[] = $column;
75
            }
76
        }
77
78
        return $difference;
79
    }
80
81
    /**
82
     * @return AbstractColumn[]
83
     */
84 View Code Duplication
    public function droppedColumns()
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...
85
    {
86
        $difference = [];
87
        foreach ($this->initial->getColumns() as $name => $column) {
88
            if (!$this->current->knowsColumn($name)) {
89
                $difference[] = $column;
90
            }
91
        }
92
93
        return $difference;
94
    }
95
96
    /**
97
     * Returns array where each value contain current and initial element state.
98
     *
99
     * @return array
100
     */
101 View Code Duplication
    public function alteredColumns()
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...
102
    {
103
        $difference = [];
104
105
        $initialColumns = $this->initial->getColumns();
106
        foreach ($this->current->getColumns() as $name => $column) {
107
            if (!$this->initial->knowsColumn($name)) {
108
                //Added into schema
109
                continue;
110
            }
111
112
            if (!$column->compare($initialColumns[$name])) {
0 ignored issues
show
Documentation introduced by
$initialColumns[$name] is of type object<Spiral\Database\Schemas\AbstractColumn>, 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...
113
                $difference[] = [$column, $initialColumns[$name]];
114
            }
115
        }
116
117
        return $difference;
118
    }
119
120
    /**
121
     * @return AbstractIndex[]
122
     */
123 View Code Duplication
    public function addedIndexes()
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
        foreach ($this->current->getIndexes() as $name => $index) {
127
            if (!$this->initial->knowsIndex($name)) {
128
                $difference[] = $index;
129
            }
130
        }
131
132
        return $difference;
133
    }
134
135
    /**
136
     * @return AbstractIndex[]
137
     */
138 View Code Duplication
    public function droppedIndexes()
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...
139
    {
140
        $difference = [];
141
        foreach ($this->initial->getIndexes() as $name => $index) {
142
            if (!$this->current->knowsIndex($name)) {
143
                $difference[] = $index;
144
            }
145
        }
146
147
        return $difference;
148
    }
149
150
    /**
151
     * Returns array where each value contain current and initial element state.
152
     *
153
     * @return array
154
     */
155 View Code Duplication
    public function alteredIndexes()
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...
156
    {
157
        $difference = [];
158
159
        $initialIndexes = $this->initial->getIndexes();
160
        foreach ($this->current->getIndexes() as $name => $index) {
161
            if (!$this->initial->knowsIndex($name)) {
162
                //Added into schema
163
                continue;
164
            }
165
166
            if (!$index->compare($initialIndexes[$name])) {
0 ignored issues
show
Documentation introduced by
$initialIndexes[$name] is of type object<Spiral\Database\Schemas\AbstractIndex>, 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...
167
                $difference[] = [$index, $initialIndexes[$name]];
168
            }
169
        }
170
171
        return $difference;
172
    }
173
174
    /**
175
     * @return AbstractReference[]
176
     */
177 View Code Duplication
    public function addedForeigns()
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
        foreach ($this->current->getForeigns() as $name => $foreign) {
181
            if (!$this->initial->knowsForeign($name)) {
182
                $difference[] = $foreign;
183
            }
184
        }
185
186
        return $difference;
187
    }
188
189
    /**
190
     * @return AbstractReference[]
191
     */
192 View Code Duplication
    public function droppedForeigns()
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...
193
    {
194
        $difference = [];
195
        foreach ($this->initial->getForeigns() as $name => $foreign) {
196
            if (!$this->current->knowsForeign($name)) {
197
                $difference[] = $foreign;
198
            }
199
        }
200
201
        return $difference;
202
    }
203
204
    /**
205
     * Returns array where each value contain current and initial element state.
206
     *
207
     * @return array
208
     */
209 View Code Duplication
    public function alteredForeigns()
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...
210
    {
211
        $difference = [];
212
213
        $initialForeigns = $this->initial->getForeigns();
214
        foreach ($this->current->getForeigns() as $name => $foreign) {
215
            if (!$this->initial->knowsForeign($name)) {
216
                //Added into schema
217
                continue;
218
            }
219
220
            if (!$foreign->compare($initialForeigns[$name])) {
0 ignored issues
show
Documentation introduced by
$initialForeigns[$name] is of type object<Spiral\Database\Schemas\AbstractReference>, 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...
221
                $difference[] = [$foreign, $initialForeigns[$name]];
222
            }
223
        }
224
225
        return $difference;
226
    }
227
228
    /**
229
     * @return array
230
     */
231
    public function __debugInfo()
232
    {
233
        return [
234
            'name'        => [
235
                'initial' => $this->initial->getName(),
236
                'current' => $this->current->getName(),
237
            ],
238
            'columns'     => [
239
                'added'   => $this->addedColumns(),
240
                'dropped' => $this->droppedColumns(),
241
                'altered' => $this->alteredColumns(),
242
            ],
243
            'indexes'     => [
244
                'added'   => $this->addedIndexes(),
245
                'dropped' => $this->droppedIndexes(),
246
                'altered' => $this->alteredIndexes(),
247
            ],
248
            'foreignKeys' => [
249
                'added'   => $this->addedForeigns(),
250
                'dropped' => $this->droppedForeigns(),
251
                'altered' => $this->alteredForeigns(),
252
            ],
253
        ];
254
    }
255
}
256