Completed
Branch develop (c2aa4c)
by Anton
05:17
created

Comparator::droppedForeigns()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 11
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 11
loc 11
rs 9.4286
cc 3
eloc 6
nc 3
nop 0
1
<?php
2
/**
3
 * Spiral Framework.
4
 *
5
 * @license   MIT
6
 * @author    Anton Titov (Wolfy-J)
7
 */
8
namespace Spiral\Database\Entities\Schemas;
9
10
use Spiral\Core\Component;
11
12
/**
13
 * Compares two table states.
14
 */
15
class Comparator extends Component
16
{
17
    /**
18
     * @var TableState
19
     */
20
    private $initial = null;
21
22
    /**
23
     * @var TableState
24
     */
25
    private $current = null;
26
27
    /**
28
     * @param TableState $initial
29
     * @param TableState $current
30
     */
31
    public function __construct(TableState $initial, TableState $current)
32
    {
33
        $this->initial = $initial;
34
        $this->current = $current;
35
    }
36
37
    /**
38
     * @return bool
39
     */
40
    public function hasChanges()
41
    {
42
        if ($this->current->getName() != $this->initial->getName()) {
43
            return true;
44
        }
45
46
        if ($this->current->getPrimaryKeys() != $this->initial->getPrimaryKeys()) {
47
            return true;
48
        }
49
50
        $difference = [
51
            count($this->addedColumns()),
52
            count($this->droppedColumns()),
53
            count($this->alteredColumns()),
54
            count($this->addedIndexes()),
55
            count($this->droppedIndexes()),
56
            count($this->alteredIndexes()),
57
            count($this->addedForeigns()),
58
            count($this->droppedForeigns()),
59
            count($this->alteredForeigns())
60
        ];
61
62
        return array_sum($difference) != 0;
63
    }
64
65
    /**
66
     * @return AbstractColumn[]
67
     */
68 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...
69
    {
70
        $difference = [];
71
        foreach ($this->current->getColumns() as $name => $column) {
72
            if (!$this->initial->knowsColumn($name)) {
73
                $difference[] = $column;
74
            }
75
        }
76
77
        return $difference;
78
    }
79
80
    /**
81
     * @return AbstractColumn[]
82
     */
83 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...
84
    {
85
        $difference = [];
86
        foreach ($this->initial->getColumns() as $name => $column) {
87
            if (!$this->current->knowsColumn($name)) {
88
                $difference[] = $column;
89
            }
90
        }
91
92
        return $difference;
93
    }
94
95
    /**
96
     * Returns array where each value contain current and initial element state.
97
     *
98
     * @return array
99
     */
100 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...
101
    {
102
        $difference = [];
103
104
        $initialColumns = $this->initial->getColumns();
105
        foreach ($this->current->getColumns() as $name => $column) {
106
            if (!$this->initial->knowsColumn($name)) {
107
                //Added into schema
108
                continue;
109
            }
110
111
            if (!$column->compare($initialColumns[$name])) {
0 ignored issues
show
Documentation introduced by
$initialColumns[$name] is of type object<Spiral\Database\E...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...
112
                $difference[] = [$column, $initialColumns[$name]];
113
            }
114
        }
115
116
        return $difference;
117
    }
118
119
    /**
120
     * @return AbstractIndex[]
121
     */
122 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...
123
    {
124
        $difference = [];
125
        foreach ($this->current->getIndexes() as $name => $index) {
126
            if (!$this->initial->knowsIndex($name)) {
127
                $difference[] = $index;
128
            }
129
        }
130
131
        return $difference;
132
    }
133
134
    /**
135
     * @return AbstractIndex[]
136
     */
137 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...
138
    {
139
        $difference = [];
140
        foreach ($this->initial->getIndexes() as $name => $index) {
141
            if (!$this->current->knowsIndex($name)) {
142
                $difference[] = $index;
143
            }
144
        }
145
146
        return $difference;
147
    }
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\E...\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\E...emas\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 object
230
     */
231
    public function __debugInfo()
232
    {
233
        return (object)[
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
}