Completed
Branch feature/pre-split (b5c37f)
by Anton
03:43
created

StateComparator   B

Complexity

Total Complexity 37

Size/Duplication

Total Lines 256
Duplicated Lines 46.88 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 120
loc 256
rs 8.6
c 0
b 0
f 0
wmc 37
lcom 1
cbo 4

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B hasChanges() 0 24 3
A isRenamed() 0 4 1
A isPrimaryChanged() 0 4 1
A addedColumns() 11 11 3
A droppedColumns() 11 11 3
A alteredColumns() 18 18 4
A addedIndexes() 11 11 3
A droppedIndexes() 11 11 3
A alteredIndexes() 18 18 4
A addedForeigns() 11 11 3
A droppedForeigns() 11 11 3
A alteredForeigns() 18 18 4
B __debugInfo() 0 24 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
        foreach ($this->current->getColumns() as $name => $column) {
91
            if (!$this->initial->knowsColumn($name)) {
92
                $difference[] = $column;
93
            }
94
        }
95
96
        return $difference;
97
    }
98
99
    /**
100
     * @return AbstractColumn[]
101
     */
102 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...
103
    {
104
        $difference = [];
105
        foreach ($this->initial->getColumns() as $name => $column) {
106
            if (!$this->current->knowsColumn($name)) {
107
                $difference[] = $column;
108
            }
109
        }
110
111
        return $difference;
112
    }
113
114
    /**
115
     * Returns array where each value contain current and initial element state.
116
     *
117
     * @return array
118
     */
119 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...
120
    {
121
        $difference = [];
122
123
        $initialColumns = $this->initial->getColumns();
124
        foreach ($this->current->getColumns() as $name => $column) {
125
            if (!$this->initial->knowsColumn($name)) {
126
                //Added into schema
127
                continue;
128
            }
129
130
            if (!$column->compare($initialColumns[$name])) {
131
                $difference[] = [$column, $initialColumns[$name]];
132
            }
133
        }
134
135
        return $difference;
136
    }
137
138
    /**
139
     * @return AbstractIndex[]
140
     */
141 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...
142
    {
143
        $difference = [];
144
        foreach ($this->current->getIndexes() as $name => $index) {
145
            if (!$this->initial->knowsIndex($name)) {
146
                $difference[] = $index;
147
            }
148
        }
149
150
        return $difference;
151
    }
152
153
    /**
154
     * @return AbstractIndex[]
155
     */
156 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...
157
    {
158
        $difference = [];
159
        foreach ($this->initial->getIndexes() as $name => $index) {
160
            if (!$this->current->knowsIndex($name)) {
161
                $difference[] = $index;
162
            }
163
        }
164
165
        return $difference;
166
    }
167
168
    /**
169
     * Returns array where each value contain current and initial element state.
170
     *
171
     * @return array
172
     */
173 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...
174
    {
175
        $difference = [];
176
177
        $initialIndexes = $this->initial->getIndexes();
178
        foreach ($this->current->getIndexes() as $name => $index) {
179
            if (!$this->initial->knowsIndex($name)) {
180
                //Added into schema
181
                continue;
182
            }
183
184
            if (!$index->compare($initialIndexes[$name])) {
185
                $difference[] = [$index, $initialIndexes[$name]];
186
            }
187
        }
188
189
        return $difference;
190
    }
191
192
    /**
193
     * @return AbstractReference[]
194
     */
195 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...
196
    {
197
        $difference = [];
198
        foreach ($this->current->getForeigns() as $name => $foreign) {
199
            if (!$this->initial->knowsForeign($name)) {
200
                $difference[] = $foreign;
201
            }
202
        }
203
204
        return $difference;
205
    }
206
207
    /**
208
     * @return AbstractReference[]
209
     */
210 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...
211
    {
212
        $difference = [];
213
        foreach ($this->initial->getForeigns() as $name => $foreign) {
214
            if (!$this->current->knowsForeign($name)) {
215
                $difference[] = $foreign;
216
            }
217
        }
218
219
        return $difference;
220
    }
221
222
    /**
223
     * Returns array where each value contain current and initial element state.
224
     *
225
     * @return array
226
     */
227 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...
228
    {
229
        $difference = [];
230
231
        $initialForeigns = $this->initial->getForeigns();
232
        foreach ($this->current->getForeigns() as $name => $foreign) {
233
            if (!$this->initial->knowsForeign($name)) {
234
                //Added into schema
235
                continue;
236
            }
237
238
            if (!$foreign->compare($initialForeigns[$name])) {
239
                $difference[] = [$foreign, $initialForeigns[$name]];
240
            }
241
        }
242
243
        return $difference;
244
    }
245
246
    /**
247
     * @return array
248
     */
249
    public function __debugInfo()
250
    {
251
        return [
252
            'name'        => [
253
                'initial' => $this->initial->getName(),
254
                'current' => $this->current->getName(),
255
            ],
256
            'columns'     => [
257
                'added'   => $this->addedColumns(),
258
                'dropped' => $this->droppedColumns(),
259
                'altered' => $this->alteredColumns(),
260
            ],
261
            'indexes'     => [
262
                'added'   => $this->addedIndexes(),
263
                'dropped' => $this->droppedIndexes(),
264
                'altered' => $this->alteredIndexes(),
265
            ],
266
            'foreignKeys' => [
267
                'added'   => $this->addedForeigns(),
268
                'dropped' => $this->droppedForeigns(),
269
                'altered' => $this->alteredForeigns(),
270
            ],
271
        ];
272
    }
273
}
274