Completed
Push — master ( f5fb34...c0a0d3 )
by mark
02:30 queued 01:05
created

Plan::gatherCreates()   A

Complexity

Conditions 4
Paths 1

Size

Total Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 33
rs 9.392
c 0
b 0
f 0
cc 4
nc 1
nop 1
1
<?php
2
/**
3
 * Phinx
4
 *
5
 * (The MIT license)
6
 *
7
 * Permission is hereby granted, free of charge, to any person obtaining a copy
8
 * of this software and associated * documentation files (the "Software"), to
9
 * deal in the Software without restriction, including without limitation the
10
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
11
 * sell copies of the Software, and to permit persons to whom the Software is
12
 * furnished to do so, subject to the following conditions:
13
 *
14
 * The above copyright notice and this permission notice shall be included in
15
 * all copies or substantial portions of the Software.
16
 *
17
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
23
 * IN THE SOFTWARE.
24
 */
25
namespace Phinx\Db\Plan;
26
27
use ArrayObject;
28
use Phinx\Db\Action\AddColumn;
29
use Phinx\Db\Action\AddForeignKey;
30
use Phinx\Db\Action\AddIndex;
31
use Phinx\Db\Action\ChangeColumn;
32
use Phinx\Db\Action\ChangeComment;
33
use Phinx\Db\Action\ChangePrimaryKey;
34
use Phinx\Db\Action\CreateTable;
35
use Phinx\Db\Action\DropForeignKey;
36
use Phinx\Db\Action\DropIndex;
37
use Phinx\Db\Action\DropTable;
38
use Phinx\Db\Action\RemoveColumn;
39
use Phinx\Db\Action\RenameColumn;
40
use Phinx\Db\Action\RenameTable;
41
use Phinx\Db\Adapter\AdapterInterface;
42
use Phinx\Db\Plan\Solver\ActionSplitter;
43
use Phinx\Db\Table\Table;
44
45
/**
46
 * A Plan takes an Intent and transforms int into a sequence of
47
 * instructions that can be correctly executed by an AdapterInterface.
48
 *
49
 * The main focus of Plan is to arrange the actions in the most efficient
50
 * way possible for the database.
51
 */
52
class Plan
53
{
54
55
    /**
56
     * List of tables to be created
57
     *
58
     * @var \Phinx\Db\Plan\NewTable[]
59
     */
60
    protected $tableCreates = [];
61
62
    /**
63
     * List of table updates
64
     *
65
     * @var \Phinx\Db\Plan\AlterTable[]
66
     */
67
    protected $tableUpdates = [];
68
69
    /**
70
     * List of table removals or renames
71
     *
72
     * @var \Phinx\Db\Plan\AlterTable[]
73
     */
74
    protected $tableMoves = [];
75
76
    /**
77
     * List of index additions or removals
78
     *
79
     * @var \Phinx\Db\Plan\AlterTable[]
80
     */
81
    protected $indexes = [];
82
83
    /**
84
     * List of constraint additions or removals
85
     *
86
     * @var \Phinx\Db\Plan\AlterTable[]
87
     */
88
    protected $constraints = [];
89
90
    /**
91
     * List of dropped columns
92
     *
93
     * @var \Phinx\Db\Plan\AlterTable[]
94
     */
95
    protected $columnRemoves = [];
96
97
    /**
98
     * Constructor
99
     *
100
     * @param Intent $intent All the actions that should be executed
101
     */
102
    public function __construct(Intent $intent)
103
    {
104
        $this->createPlan($intent->getActions());
0 ignored issues
show
Documentation introduced by
$intent->getActions() is of type array<integer,object<Phinx\Db\Action\Action>>, but the function expects a object<Phinx\Db\Plan\Intent>.

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...
105
    }
106
107
    /**
108
     * Parses the given Intent and creates the separate steps to execute
109
     *
110
     * @param Intent $actions The actions to use for the plan
111
     * @return void
112
     */
113
    protected function createPlan($actions)
114
    {
115
        $this->gatherCreates($actions);
0 ignored issues
show
Documentation introduced by
$actions is of type object<Phinx\Db\Plan\Intent>, but the function expects a array<integer,object<Phinx\Db\Action\Action>>.

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...
116
        $this->gatherUpdates($actions);
0 ignored issues
show
Documentation introduced by
$actions is of type object<Phinx\Db\Plan\Intent>, but the function expects a array<integer,object<Phinx\Db\Action\Action>>.

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...
117
        $this->gatherTableMoves($actions);
0 ignored issues
show
Documentation introduced by
$actions is of type object<Phinx\Db\Plan\Intent>, but the function expects a array<integer,object<Phinx\Db\Action\Action>>.

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...
118
        $this->gatherIndexes($actions);
0 ignored issues
show
Documentation introduced by
$actions is of type object<Phinx\Db\Plan\Intent>, but the function expects a array<integer,object<Phinx\Db\Action\Action>>.

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...
119
        $this->gatherConstraints($actions);
0 ignored issues
show
Documentation introduced by
$actions is of type object<Phinx\Db\Plan\Intent>, but the function expects a array<integer,object<Phinx\Db\Action\Action>>.

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...
120
        $this->resolveConflicts();
121
    }
122
123
    /**
124
     * Returns a nested list of all the steps to execute
125
     *
126
     * @return AlterTable[][]
127
     */
128 View Code Duplication
    protected function updatesSequence()
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...
129
    {
130
        return [
131
            $this->tableUpdates,
132
            $this->constraints,
133
            $this->indexes,
134
            $this->columnRemoves,
135
            $this->tableMoves,
136
        ];
137
    }
138
139
    /**
140
     * Returns a nested list of all the steps to execute in inverse order
141
     *
142
     * @return AlterTable[][]
143
     */
144 View Code Duplication
    protected function inverseUpdatesSequence()
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...
145
    {
146
        return [
147
            $this->constraints,
148
            $this->tableMoves,
149
            $this->indexes,
150
            $this->columnRemoves,
151
            $this->tableUpdates
152
        ];
153
    }
154
155
    /**
156
     * Executes this plan using the given AdapterInterface
157
     *
158
     * @param AdapterInterface $executor The executor object for the plan
159
     * @return void
160
     */
161 View Code Duplication
    public function execute(AdapterInterface $executor)
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...
162
    {
163
        foreach ($this->tableCreates as $newTable) {
164
            $executor->createTable($newTable->getTable(), $newTable->getColumns(), $newTable->getIndexes());
165
        }
166
167
        collection($this->updatesSequence())
168
            ->unfold()
169
            ->each(function ($updates) use ($executor) {
170
                $executor->executeActions($updates->getTable(), $updates->getActions());
171
            });
172
    }
173
174
    /**
175
     * Executes the inverse plan (rollback the actions) with the given AdapterInterface:w
176
     *
177
     * @param AdapterInterface $executor The executor object for the plan
178
     * @return void
179
     */
180 View Code Duplication
    public function executeInverse(AdapterInterface $executor)
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...
181
    {
182
        collection($this->inverseUpdatesSequence())
183
            ->unfold()
184
            ->each(function ($updates) use ($executor) {
185
                $executor->executeActions($updates->getTable(), $updates->getActions());
186
            });
187
188
        foreach ($this->tableCreates as $newTable) {
189
            $executor->createTable($newTable->getTable(), $newTable->getColumns(), $newTable->getIndexes());
190
        }
191
    }
192
193
    /**
194
     * Deletes certain actions from the plan if they are found to be conflicting or redundant.
195
     *
196
     * @return void
197
     */
198
    protected function resolveConflicts()
199
    {
200
        $moveActions = collection($this->tableMoves)
201
            ->unfold(function ($move) {
202
                return $move->getActions();
203
            });
204
205
        foreach ($moveActions as $action) {
206
            if ($action instanceof DropTable) {
207
                $this->tableUpdates = $this->forgetTable($action->getTable(), $this->tableUpdates);
208
                $this->constraints = $this->forgetTable($action->getTable(), $this->constraints);
209
                $this->indexes = $this->forgetTable($action->getTable(), $this->indexes);
210
                $this->columnRemoves = $this->forgetTable($action->getTable(), $this->columnRemoves);
211
            }
212
        }
213
214
        // Columns that are dropped will automatically cause the indexes to be dropped as well
215
        foreach ($this->columnRemoves as $columnRemove) {
216
            foreach ($columnRemove->getActions() as $action) {
217
                if ($action instanceof RemoveColumn) {
218
                    list($this->indexes) = $this->forgetDropIndex(
219
                        $action->getTable(),
220
                        [$action->getColumn()->getName()],
221
                        $this->indexes
222
                    );
223
                }
224
            }
225
        }
226
227
        $this->tableUpdates = collection($this->tableUpdates)
228
            // Renaming a column and then changing the renamed column is something people do,
229
            // but it is a conflicting action. Luckily solving the conflict can be done by moving
230
            // the ChangeColumn action to another AlterTable
231
            ->unfold(new ActionSplitter(RenameColumn::class, ChangeColumn::class, function (RenameColumn $a, ChangeColumn $b) {
232
                return $a->getNewName() == $b->getColumnName();
233
            }))
234
            ->toList();
235
236
        $this->constraints = collection($this->constraints)
237
            ->map(function (AlterTable $alter) {
238
                // Dropping indexes used by foreign keys is a conflict, but one we can resolve
239
                // if the foreign key is also scheduled to be dropped. If we can find such a a case,
240
                // we force the execution of the index drop after the foreign key is dropped.
241
                return $this->remapContraintAndIndexConflicts($alter);
242
            })
243
            // Changing constraint properties sometimes require dropping it and then
244
            // creating it again with the new stuff. Unfortunately, we have already bundled
245
            // everything together in as few AlterTable statements as we could, so we need to
246
            // resolve this conflict manually
247
            ->unfold(new ActionSplitter(DropForeignKey::class, AddForeignKey::class, function (DropForeignKey $a, AddForeignKey $b) {
248
                return $a->getForeignKey()->getColumns() === $b->getForeignKey()->getColumns();
249
            }))
250
            ->toList();
251
    }
252
253
    /**
254
     * Deletes all actions related to the given table and keeps the
255
     * rest
256
     *
257
     * @param Table $table The table to find in the list of actions
258
     * @param AlterTable[] $actions The actions to transform
259
     * @return AlterTable[] The list of actions without actions for the given table
260
     */
261
    protected function forgetTable(Table $table, $actions)
262
    {
263
        $result = [];
264
        foreach ($actions as $action) {
265
            if ($action->getTable()->getName() === $table->getName()) {
266
                continue;
267
            }
268
            $result[] = $action;
269
        }
270
271
        return $result;
272
    }
273
274
    /**
275
     * Finds all DropForeignKey actions in an AlterTable and moves
276
     * all conflicting DropIndex action in `$this->indexes` into the
277
     * given AlterTable.
278
     *
279
     * @param AlterTable $alter The collection of actions to inspect
280
     * @return AlterTable The updated AlterTable object. This function
281
     * has the side effect of changing the `$this->indexes` property.
282
     */
283
    protected function remapContraintAndIndexConflicts(AlterTable $alter)
284
    {
285
        $newAlter = new AlterTable($alter->getTable());
286
        collection($alter->getActions())
287
            ->unfold(function ($action) {
288
                if (!$action instanceof DropForeignKey) {
289
                    return [$action];
290
                }
291
292
                list($this->indexes, $dropIndexActions) = $this->forgetDropIndex(
293
                    $action->getTable(),
294
                    $action->getForeignKey()->getColumns(),
295
                    $this->indexes
296
                );
297
298
                $return = [$action];
299
                if (!empty($dropIndexActions)) {
300
                    $return = array_merge($return, $dropIndexActions);
301
                }
302
303
                return $return;
304
            })
305
            ->each(function ($action) use ($newAlter) {
306
                $newAlter->addAction($action);
307
            });
308
309
        return $newAlter;
310
    }
311
312
    /**
313
     * Deletes any DropIndex actions for the given table and exact columns
314
     *
315
     * @param Table $table The table to find in the list of actions
316
     * @param string[] $columns The column names to match
317
     * @param AlterTable[] $actions The actions to transform
318
     * @return array A tuple containing the list of actions without actions for dropping the index
319
     * and a list of drop index actions that were removed.
320
     */
321 View Code Duplication
    protected function forgetDropIndex(Table $table, array $columns, array $actions)
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...
322
    {
323
        $dropIndexActions = new ArrayObject();
324
        $indexes = collection($actions)
325
            ->map(function ($alter) use ($table, $columns, $dropIndexActions) {
326
                if ($alter->getTable()->getName() !== $table->getName()) {
327
                    return $alter;
328
                }
329
330
                $newAlter = new AlterTable($table);
331
                collection($alter->getActions())
332
                    ->map(function ($action) use ($columns) {
333
                        if (!$action instanceof DropIndex) {
334
                            return [$action, null];
335
                        }
336
                        if ($action->getIndex()->getColumns() === $columns) {
337
                            return [null, $action];
338
                        }
339
340
                        return [$action, null];
341
                    })
342
                    ->each(function ($tuple) use ($newAlter, $dropIndexActions) {
343
                        list($action, $dropIndex) = $tuple;
344
                        if ($action) {
345
                            $newAlter->addAction($action);
346
                        }
347
                        if ($dropIndex) {
348
                            $dropIndexActions->append($dropIndex);
349
                        }
350
                    });
351
352
                return $newAlter;
353
            })
354
            ->toArray();
355
356
        return [$indexes, $dropIndexActions->getArrayCopy()];
357
    }
358
359
    /**
360
     * Deletes any RemoveColumn actions for the given table and exact columns
361
     *
362
     * @param Table $table The table to find in the list of actions
363
     * @param string[] $columns The column names to match
364
     * @param AlterTable[] $actions The actions to transform
365
     * @return array A tuple containing the list of actions without actions for removing the column
366
     * and a list of remove column actions that were removed.
367
     */
368 View Code Duplication
    protected function forgetRemoveColumn(Table $table, array $columns, array $actions)
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...
369
    {
370
        $removeColumnActions = new ArrayObject();
371
        $indexes = collection($actions)
372
            ->map(function ($alter) use ($table, $columns, $removeColumnActions) {
373
                if ($alter->getTable()->getName() !== $table->getName()) {
374
                    return $alter;
375
                }
376
377
                $newAlter = new AlterTable($table);
378
                collection($alter->getActions())
379
                    ->map(function ($action) use ($columns) {
380
                        if (!$action instanceof RemoveColumn) {
381
                            return [$action, null];
382
                        }
383
                        if (in_array($action->getColumn(), $columns)) {
384
                            return [null, $action];
385
                        }
386
387
                        return [$action, null];
388
                    })
389
                    ->each(function ($tuple) use ($newAlter, $removeColumnActions) {
390
                        list($action, $removeColumn) = $tuple;
391
                        if ($action) {
392
                            $newAlter->addAction($action);
393
                        }
394
                        if ($removeColumn) {
395
                            $removeColumnActions->append($removeColumn);
396
                        }
397
                    });
398
399
                return $newAlter;
400
            })
401
            ->toArray();
402
403
        return [$indexes, $removeColumnActions->getArrayCopy()];
404
    }
405
406
    /**
407
     * Collects all table creation actions from the given intent
408
     *
409
     * @param \Phinx\Db\Action\Action[] $actions The actions to parse
410
     * @return void
411
     */
412
    protected function gatherCreates($actions)
413
    {
414
        collection($actions)
415
            ->filter(function ($action) {
416
                return $action instanceof CreateTable;
417
            })
418
            ->map(function ($action) {
419
                return [$action->getTable()->getName(), new NewTable($action->getTable())];
420
            })
421
            ->each(function ($step) {
422
                $this->tableCreates[$step[0]] = $step[1];
423
            });
424
425
        collection($actions)
426
            ->filter(function ($action) {
427
                return $action instanceof AddColumn
428
                    || $action instanceof AddIndex;
429
            })
430
            ->filter(function ($action) {
431
                return isset($this->tableCreates[$action->getTable()->getName()]);
432
            })
433
            ->each(function ($action) {
434
                $table = $action->getTable();
435
436
                if ($action instanceof AddColumn) {
437
                    $this->tableCreates[$table->getName()]->addColumn($action->getColumn());
438
                }
439
440
                if ($action instanceof AddIndex) {
441
                    $this->tableCreates[$table->getName()]->addIndex($action->getIndex());
442
                }
443
            });
444
    }
445
446
    /**
447
     * Collects all alter table actions from the given intent
448
     *
449
     * @param \Phinx\Db\Action\Action[] $actions The actions to parse
450
     * @return void
451
     */
452
    protected function gatherUpdates($actions)
453
    {
454
        collection($actions)
455
            ->filter(function ($action) {
456
                return $action instanceof AddColumn
457
                    || $action instanceof ChangeColumn
458
                    || $action instanceof RemoveColumn
459
                    || $action instanceof RenameColumn;
460
            })
461
            // We are only concerned with table changes
462
            ->reject(function ($action) {
463
                return isset($this->tableCreates[$action->getTable()->getName()]);
464
            })
465
            ->each(function ($action) {
466
                $table = $action->getTable();
467
                $name = $table->getName();
468
469
                if ($action instanceof RemoveColumn) {
470
                    if (!isset($this->columnRemoves[$name])) {
471
                        $this->columnRemoves[$name] = new AlterTable($table);
472
                    }
473
                    $this->columnRemoves[$name]->addAction($action);
474
                } else {
475
                    if (!isset($this->tableUpdates[$name])) {
476
                        $this->tableUpdates[$name] = new AlterTable($table);
477
                    }
478
                    $this->tableUpdates[$name]->addAction($action);
479
                }
480
            });
481
    }
482
483
    /**
484
     * Collects all alter table drop and renames from the given intent
485
     *
486
     * @param \Phinx\Db\Action\Action[] $actions The actions to parse
487
     * @return void
488
     */
489
    protected function gatherTableMoves($actions)
490
    {
491
        collection($actions)
492
            ->filter(function ($action) {
493
                return $action instanceof DropTable
494
                    || $action instanceof RenameTable
495
                    || $action instanceof ChangePrimaryKey
496
                    || $action instanceof ChangeComment;
497
            })
498 View Code Duplication
            ->each(function ($action) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
499
                $table = $action->getTable();
500
                $name = $table->getName();
501
502
                if (!isset($this->tableMoves[$name])) {
503
                    $this->tableMoves[$name] = new AlterTable($table);
504
                }
505
506
                $this->tableMoves[$name]->addAction($action);
507
            });
508
    }
509
510
    /**
511
     * Collects all index creation and drops from the given intent
512
     *
513
     * @param \Phinx\Db\Action\Action[] $actions The actions to parse
514
     * @return void
515
     */
516
    protected function gatherIndexes($actions)
517
    {
518
        collection($actions)
519
            ->filter(function ($action) {
520
                return $action instanceof AddIndex
521
                    || $action instanceof DropIndex;
522
            })
523
            ->reject(function ($action) {
524
                // Indexes for new tables are created inline
525
                // so we don't wan't them here too
526
                return isset($this->tableCreates[$action->getTable()->getName()]);
527
            })
528 View Code Duplication
            ->each(function ($action) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
529
                $table = $action->getTable();
530
                $name = $table->getName();
531
532
                if (!isset($this->indexes[$name])) {
533
                    $this->indexes[$name] = new AlterTable($table);
534
                }
535
536
                $this->indexes[$name]->addAction($action);
537
            });
538
    }
539
540
    /**
541
     * Collects all foreign key creation and drops from the given intent
542
     *
543
     * @param \Phinx\Db\Action\Action[] $actions The actions to parse
544
     * @return void
545
     */
546
    protected function gatherConstraints($actions)
547
    {
548
        collection($actions)
549
            ->filter(function ($action) {
550
                return $action instanceof AddForeignKey
551
                    || $action instanceof DropForeignKey;
552
            })
553 View Code Duplication
            ->each(function ($action) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
554
                $table = $action->getTable();
555
                $name = $table->getName();
556
557
                if (!isset($this->constraints[$name])) {
558
                    $this->constraints[$name] = new AlterTable($table);
559
                }
560
561
                $this->constraints[$name]->addAction($action);
562
            });
563
    }
564
}
565