Completed
Pull Request — master (#1414)
by
unknown
04:38 queued 01:00
created

Table::saveData()   B

Complexity

Conditions 6
Paths 7

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 6.0131

Importance

Changes 0
Metric Value
dl 0
loc 28
ccs 13
cts 14
cp 0.9286
rs 8.8497
c 0
b 0
f 0
cc 6
nc 7
nop 0
crap 6.0131
1
<?php
2
/**
3
 * Phinx
4
 *
5
 * (The MIT license)
6
 * Copyright (c) 2015 Rob Morgan
7
 *
8
 * Permission is hereby granted, free of charge, to any person obtaining a copy
9
 * of this software and associated * documentation files (the "Software"), to
10
 * deal in the Software without restriction, including without limitation the
11
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
12
 * sell copies of the Software, and to permit persons to whom the Software is
13
 * furnished to do so, subject to the following conditions:
14
 *
15
 * The above copyright notice and this permission notice shall be included in
16
 * all copies or substantial portions of the Software.
17
 *
18
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24
 * IN THE SOFTWARE.
25
 *
26
 * @package    Phinx
27
 * @subpackage Phinx\Db
28
 */
29
namespace Phinx\Db;
30
31
use Phinx\Db\Action\AddColumn;
32
use Phinx\Db\Action\AddForeignKey;
33
use Phinx\Db\Action\AddIndex;
34
use Phinx\Db\Action\ChangeColumn;
35
use Phinx\Db\Action\CreateTable;
36
use Phinx\Db\Action\DropColumn;
37
use Phinx\Db\Action\DropForeignKey;
38
use Phinx\Db\Action\DropIndex;
39
use Phinx\Db\Action\DropTable;
40
use Phinx\Db\Action\RemoveColumn;
41
use Phinx\Db\Action\RenameColumn;
42
use Phinx\Db\Action\RenameTable;
43
use Phinx\Db\Adapter\AdapterInterface;
44
use Phinx\Db\Plan\Intent;
45
use Phinx\Db\Plan\Plan;
46
use Phinx\Db\Table\Column;
47
use Phinx\Db\Table\Table as TableValue;
48
49
/**
50
 *
51
 * This object is based loosely on: http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/Table.html.
52
 */
53
class Table
54
{
55
    /**
56
     * @var \Phinx\Db\Table\Table
57
     */
58
    protected $table;
59
60
    /**
61
     * @var \Phinx\Db\Adapter\AdapterInterface
62
     */
63
    protected $adapter;
64
65
    /**
66
     * @var \Phinx\Db\Plan\Intent
67
     */
68
    protected $actions;
69
70
    /**
71
     * @var array
72
     */
73
    protected $data = [];
74
75
    /**
76
     * Class Constructor.
77
     *
78
     * @param string $name Table Name
79
     * @param array $options Options
80
     * @param \Phinx\Db\Adapter\AdapterInterface $adapter Database Adapter
81
     */
82
    public function __construct($name, $options = [], AdapterInterface $adapter = null)
83
    {
84 239
        $this->table = new TableValue($name, $options);
85
        $this->actions = new Intent();
86 239
87 239
        if ($adapter !== null) {
88
            $this->setAdapter($adapter);
89 239
        }
90 231
    }
91 231
92 239
    /**
93
     * Gets the table name.
94
     *
95
     * @return string|null
96
     */
97
    public function getName()
98
    {
99
        return $this->table->getName();
100 239
    }
101
102 239
    /**
103 239
     * Gets the table options.
104
     *
105
     * @return array
106
     */
107
    public function getOptions()
108
    {
109
        return $this->table->getOptions();
110
    }
111 215
112
    /**
113 215
     * Gets the table name and options as an object
114
     *
115
     * @return \Phinx\Db\Table\Table
116
     */
117
    public function getTable()
118
    {
119
        return $this->table;
120
    }
121
122 239
    /**
123
     * Sets the database adapter.
124 239
     *
125 239
     * @param \Phinx\Db\Adapter\AdapterInterface $adapter Database Adapter
126
     * @return \Phinx\Db\Table
127
     */
128
    public function setAdapter(AdapterInterface $adapter)
129
    {
130
        $this->adapter = $adapter;
131
132
        return $this;
133 189
    }
134
135 189
    /**
136
     * Gets the database adapter.
137
     *
138
     * @return \Phinx\Db\Adapter\AdapterInterface|null
139
     */
140
    public function getAdapter()
141
    {
142
        if (!$this->adapter) {
143
            throw new \RuntimeException('There is no database adapter set yet, cannot proceed');
144 231
        }
145
146 231
        return $this->adapter;
147 231
    }
148
149
    /**
150
     * Does the table have pending actions?
151
     *
152
     * @return bool
153
     */
154
    public function hasPendingActions()
155 225
    {
156
        return count($this->actions->getActions()) > 0 || count($this->data) > 0;
157 225
    }
158
159
    /**
160
     * Does the table exist?
161
     *
162
     * @return bool
163
     */
164
    public function exists()
165 195
    {
166
        return $this->getAdapter()->hasTable($this->getName());
167 195
    }
168
169
    /**
170
     * Drops the database table.
171
     *
172
     * @return \Phinx\Db\Table
173
     */
174
    public function drop()
175 1
    {
176
        $this->actions->addAction(new DropTable($this->table));
177 1
178 1
        return $this;
179
    }
180
181
    /**
182
     * Renames the database table.
183
     *
184
     * @param string $newTableName New Table Name
185
     * @return \Phinx\Db\Table
186 3
     */
187
    public function rename($newTableName)
188 3
    {
189 3
        $this->actions->addAction(new RenameTable($this->table, $newTableName));
190 3
191
        return $this;
192
    }
193
194
    /**
195
     * Gets an array of the table columns.
196
     *
197
     * @return \Phinx\Db\Table\Column[]
198
     */
199
    public function getColumns()
200
    {
201
        return $this->getAdapter()->getColumns($this->getName());
202
    }
203
204
    /**
205
     * Gets a table column if it exists.
206
     *
207
     * @param string $name Column name
208
     * @return \Phinx\Db\Table\Column|null
209
     */
210
    public function getColumn($name)
211 10
    {
212
        $columns = array_filter(
213 10
            $this->getColumns(),
214
            function ($column) use ($name) {
215
                return $column->getName() === $name;
216
            }
217
        );
218
219
        return array_pop($columns);
220
    }
221
222 196
    /**
223
     * Sets an array of data to be inserted.
224 196
     *
225 196
     * @param array $data Data
226
     * @return \Phinx\Db\Table
227
     */
228
    public function setData($data)
229
    {
230
        $this->data = $data;
231
232
        return $this;
233 204
    }
234
235 204
    /**
236
     * Gets the data waiting to be inserted.
237
     *
238
     * @return array
239
     */
240
    public function getData()
241
    {
242
        return $this->data;
243
    }
244 196
245
    /**
246 196
     * Resets all of the pending data to be inserted
247 196
     *
248
     * @return void
249
     */
250
    public function resetData()
251
    {
252
        $this->setData([]);
253
    }
254
255 191
    /**
256
     * Resets all of the pending table changes.
257 191
     *
258
     * @return void
259
     */
260
    public function reset()
261
    {
262
        $this->actions = new Intent();
263
        $this->resetData();
264
    }
265
266 196
    /**
267
     * Add a table column.
268 196
     *
269 196
     * Type can be: string, text, integer, float, decimal, datetime, timestamp,
270
     * time, date, binary, boolean.
271
     *
272
     * Valid options can be: limit, default, null, precision or scale.
273
     *
274
     * @param string|\Phinx\Db\Table\Column $columnName Column Name
275
     * @param string|\Phinx\Util\Literal $type Column Type
276
     * @param array $options Column Options
277 192
     * @throws \RuntimeException
278
     * @throws \InvalidArgumentException
279 192
     * @return \Phinx\Db\Table
280
     */
281
    public function addColumn($columnName, $type = null, $options = [])
282
    {
283 View Code Duplication
        if ($columnName instanceof Column) {
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...
284
            $action = new AddColumn($this->table, $columnName);
285
        } else {
286
            $action = AddColumn::build($this->table, $columnName, $type, $options);
287
        }
288 196
289
        // Delegate to Adapters to check column type
290 196
        if (!$this->getAdapter()->isValidColumnType($action->getColumn())) {
291 196
            throw new \InvalidArgumentException(sprintf(
292
                'An invalid column type "%s" was specified for column "%s".',
293
                $type,
294
                $action->getColumn()->getName()
295
            ));
296
        }
297
298
        $this->actions->addAction($action);
299 197
300
        return $this;
301 197
    }
302
303
    /**
304
     * Remove a table column.
305
     *
306
     * @param string $columnName Column Name
307
     * @return \Phinx\Db\Table
308
     */
309 196
    public function removeColumn($columnName)
310
    {
311 196
        $action = RemoveColumn::build($this->table, $columnName);
312 196
        $this->actions->addAction($action);
313 196
314 196
        return $this;
315 196
    }
316
317
    /**
318
     * Rename a table column.
319
     *
320
     * @param string $oldName Old Column Name
321
     * @param string $newName New Column Name
322
     * @return \Phinx\Db\Table
323
     */
324
    public function renameColumn($oldName, $newName)
325
    {
326
        $action = RenameColumn::build($this->table, $oldName, $newName);
327
        $this->actions->addAction($action);
328
329
        return $this;
330
    }
331
332 210
    /**
333
     * Change a table column type.
334
     *
335 210
     * @param string        $columnName    Column Name
336 1
     * @param string|\Phinx\Db\Table\Column|\Phinx\Util\Literal $newColumnType New Column Type
337
     * @param array         $options       Options
338
     * @return \Phinx\Db\Table
339
     */
340 209
    public function changeColumn($columnName, $newColumnType, array $options = [])
341 207
    {
342 207 View Code Duplication
        if ($newColumnType instanceof Column) {
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...
343 207
            $action = new ChangeColumn($this->table, $columnName, $newColumnType);
344 207
        } else {
345 207
            $action = ChangeColumn::build($this->table, $columnName, $newColumnType, $options);
346 2
        }
347
        $this->actions->addAction($action);
348
349
        return $this;
350 209
    }
351 1
352 1
    /**
353 1
     * Checks to see if a column exists.
354 1
     *
355 1
     * @param string $columnName Column Name
356
     * @return bool
357
     */
358 208
    public function hasColumn($columnName)
359 208
    {
360
        return $this->getAdapter()->hasColumn($this->getName(), $columnName);
361
    }
362
363
    /**
364
     * Add an index to a database table.
365
     *
366
     * In $options you can specific unique = true/false or name (index name).
367
     *
368 1
     * @param string|array|\Phinx\Db\Table\Index $columns Table Column(s)
369
     * @param array $options Index Options
370 1
     * @return \Phinx\Db\Table
371 1
     */
372
    public function addIndex($columns, array $options = [])
373
    {
374
        $action = AddIndex::build($this->table, $columns, $options);
375
        $this->actions->addAction($action);
376
377
        return $this;
378
    }
379
380
    /**
381 4
     * Removes the given index from a table.
382
     *
383 4
     * @param array $columns Columns
384 4
     * @return \Phinx\Db\Table
385
     */
386
    public function removeIndex(array $columns)
387
    {
388
        $action = DropIndex::build($this->table, $columns);
389
        $this->actions->addAction($action);
390
391
        return $this;
392
    }
393
394
    /**
395 17
     * Removes the given index identified by its name from a table.
396
     *
397
     * @param string $name Index name
398 17
     * @return \Phinx\Db\Table
399 4
     */
400 4
    public function removeIndexByName($name)
401 4
    {
402 4
        $action = DropIndex::buildFromName($this->table, $name);
403 13
        $this->actions->addAction($action);
404
405
        return $this;
406
    }
407 17
408 15
    /**
409 15
     * Checks to see if an index exists.
410
     *
411 17
     * @param string|array $columns Columns
412 17
     * @return bool
413
     */
414
    public function hasIndex($columns)
415
    {
416
        return $this->getAdapter()->hasIndex($this->getName(), $columns);
417
    }
418
419
    /**
420
     * Checks to see if an index specified by name exists.
421 89
     *
422
     * @param string $indexName
423 89
     * @return bool
424
     */
425
    public function hasIndexByName($indexName)
426
    {
427
        return $this->getAdapter()->hasIndexByName($this->getName(), $indexName);
428
    }
429
430
    /**
431
     * Add a foreign key to a database table.
432
     *
433
     * In $options you can specify on_delete|on_delete = cascade|no_action ..,
434
     * on_update, constraint = constraint name.
435 29
     *
436
     * @param string|array $columns Columns
437
     * @param string|\Phinx\Db\Table $referencedTable   Referenced Table
438 29
     * @param string|array $referencedColumns Referenced Columns
439 28
     * @param array $options Options
440 28
     * @return \Phinx\Db\Table
441 22
     */
442 22 View Code Duplication
    public function addForeignKey($columns, $referencedTable, $referencedColumns = ['id'], $options = [])
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...
443 28
    {
444 28
        $action = AddForeignKey::build($this->table, $columns, $referencedTable, $referencedColumns, $options);
0 ignored issues
show
Bug introduced by
It seems like $columns defined by parameter $columns on line 442 can also be of type array; however, Phinx\Db\Action\AddForeignKey::build() does only seem to accept string|array<integer,string>, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
Bug introduced by
It seems like $referencedTable defined by parameter $referencedTable on line 442 can also be of type object<Phinx\Db\Table>; however, Phinx\Db\Action\AddForeignKey::build() does only seem to accept object<Phinx\Db\Table\Table>|string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
445 28
        $this->actions->addAction($action);
446 1
447
        return $this;
448
    }
449 29
450 29
    /**
451
     * Add a foreign key to a database table with a given name.
452
     *
453
     * In $options you can specify on_delete|on_delete = cascade|no_action ..,
454
     * on_update, constraint = constraint name.
455
     *
456
     * @param string $name The constraint name
457
     * @param string|array $columns Columns
458
     * @param string|\Phinx\Db\Table $referencedTable   Referenced Table
459 1
     * @param string|array $referencedColumns Referenced Columns
460
     * @param array $options Options
461 1
     * @return \Phinx\Db\Table
462 1
     */
463 View Code Duplication
    public function addForeignKeyWithName($name, $columns, $referencedTable, $referencedColumns = ['id'], $options = [])
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...
464
    {
465
        $action = AddForeignKey::build(
466
            $this->table,
467
            $columns,
0 ignored issues
show
Bug introduced by
It seems like $columns defined by parameter $columns on line 463 can also be of type array; however, Phinx\Db\Action\AddForeignKey::build() does only seem to accept string|array<integer,string>, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
468
            $referencedTable,
0 ignored issues
show
Bug introduced by
It seems like $referencedTable defined by parameter $referencedTable on line 463 can also be of type object<Phinx\Db\Table>; however, Phinx\Db\Action\AddForeignKey::build() does only seem to accept object<Phinx\Db\Table\Table>|string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
469
            $referencedColumns,
470
            $options,
471 1
            $name
472
        );
473 1
        $this->actions->addAction($action);
474 1
475
        return $this;
476
    }
477
478
    /**
479
     * Removes the given foreign key from the table.
480
     *
481
     * @param string|array $columns    Column(s)
482
     * @param null|string  $constraint Constraint names
483
     * @return \Phinx\Db\Table
484 12
     */
485
    public function dropForeignKey($columns, $constraint = null)
486 12
    {
487
        $action = DropForeignKey::build($this->table, $columns, $constraint);
0 ignored issues
show
Bug introduced by
It seems like $columns defined by parameter $columns on line 485 can also be of type array; however, Phinx\Db\Action\DropForeignKey::build() does only seem to accept string|array<integer,string>, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
488
        $this->actions->addAction($action);
489
490
        return $this;
491
    }
492
493
    /**
494
     * Checks to see if a foreign key exists.
495
     *
496
     * @param  string|array $columns    Column(s)
497
     * @param  null|string  $constraint Constraint names
498
     * @return bool
499
     */
500
    public function hasForeignKey($columns, $constraint = null)
501 8
    {
502
        return $this->getAdapter()->hasForeignKey($this->getName(), $columns, $constraint);
0 ignored issues
show
Documentation introduced by
$columns is of type string|array, but the function expects a array<integer,string>.

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...
503 8
    }
504 4
505 4
    /**
506 8
     * Add timestamp columns created_at and updated_at to the table.
507 8
     *
508
     * @param string|null $createdAt    Alternate name for the created_at column
509
     * @param string|null $updatedAt    Alternate name for the updated_at column
510 8
     * @param bool        $withTimezone Whether to set the timezone option on the added columns
511
     *
512 8
     * @return \Phinx\Db\Table
513 8
     */
514 8
    public function addTimestamps($createdAt = 'created_at', $updatedAt = 'updated_at', $withTimezone = false)
515 8
    {
516
        $createdAt = is_null($createdAt) ? 'created_at' : $createdAt;
517 8
        $updatedAt = is_null($updatedAt) ? 'updated_at' : $updatedAt;
518
519
        $this->addColumn($createdAt, 'timestamp', [
520
                   'default' => 'CURRENT_TIMESTAMP',
521
                   'update' => '',
522
                   'timezone' => $withTimezone,
523
             ])
524
             ->addColumn($updatedAt, 'timestamp', [
525
                 'null' => true,
526
                 'default' => null,
527 1
                 'timezone' => $withTimezone,
528
             ]);
529 1
530 1
        return $this;
531 1
    }
532 1
533
    /**
534
     * Alias that always sets $withTimezone to true
535 1
     * @see addTimestamps
536
     *
537
     * @param string|null $createdAt Alternate name for the created_at column
538 1
     * @param string|null $updatedAt Alternate name for the updated_at column
539
     *
540
     * @return \Phinx\Db\Table
541
     */
542
    public function addTimestampsWithTimezone($createdAt = null, $updatedAt = null)
543
    {
544
        $this->addTimestamps($createdAt, $updatedAt, true);
545
546
        return $this;
547
    }
548 1
549
    /**
550 1
     * Insert data into the table.
551
     *
552
     * @param array $data array of data in the form:
553
     *              array(
554
     *                  array("col1" => "value1", "col2" => "anotherValue1"),
555
     *                  array("col2" => "value2", "col2" => "anotherValue2"),
556
     *              )
557
     *              or array("col1" => "value1", "col2" => "anotherValue1")
558
     *
559
     * @return \Phinx\Db\Table
560
     */
561 15
    public function insert($data)
562
    {
563 15
        // handle array of array situations
564 15
        if (isset($data[0]) && is_array($data[0])) {
565 15
            foreach ($data as $row) {
566 15
                $this->data[] = $row;
567
            }
568 15
569 15
            return $this;
570 15
        }
571
        $this->data[] = $data;
572 15
573
        return $this;
574 15
    }
575
576
    /**
577
     * Creates a table from the object instance.
578
     *
579
     * @return void
580
     */
581
    public function create()
582
    {
583
        $this->executeActions(false);
584
        $this->saveData();
585
        $this->reset(); // reset pending changes
586
    }
587
588
    /**
589 17
     * Updates a table from the object instance.
590
     *
591
     * @throws \RuntimeException
592 17
     * @return void
593 11
     */
594 11
    public function update()
595 11
    {
596 11
        $this->executeActions(true);
597
        $this->saveData();
598 8
        $this->reset(); // reset pending changes
599 8
    }
600
601
    /**
602
     * Commit the pending data waiting for insertion.
603
     *
604
     * @return void
605
     */
606
    public function saveData()
607 196
    {
608
        $rows = $this->getData();
609 196
        if (empty($rows)) {
610 196
            return;
611 196
        }
612 196
613
        $bulk = true;
614
        $row = current($rows);
615
        $c = array_keys($row);
616
        foreach ($this->getData() as $row) {
617
            $k = array_keys($row);
618
            if ($k != $c) {
619
                $bulk = false;
620 46
                break;
621
            }
622 46
        }
623
624
        if ($bulk) {
625
            $this->getAdapter()->bulkinsert($this->table, $this->getData());
626
        } else {
627 46
            foreach ($this->getData() as $row) {
628 38
                $this->getAdapter()->insert($this->table, $row);
629 46
            }
630
        }
631 46
632 6
        $this->resetData();
633 46
    }
634
635 46
    /**
636 3
     * Immediately truncates the table. This operation cannot be undone
637 46
     *
638
     * @return void
639 46
     */
640 46
    public function truncate()
641 46
    {
642
        $this->getAdapter()->truncateTable($this->getName());
643
    }
644
645
    /**
646
     * Commits the table changes.
647
     *
648 196
     * If the table doesn't exist it is created otherwise it is updated.
649
     *
650 196
     * @return void
651 196
     */
652 192
    public function save()
653
    {
654
        if ($this->exists()) {
655 12
            $this->update(); // update the table
656 12
        } else {
657 12
            $this->create(); // create the table
658 12
        }
659 12
    }
660 12
661 1
    /**
662 1
     * Executes all the pending actions for this table
663
     *
664 12
     * @param bool $exists Whether or not the table existed prior to executing this method
665
     * @return void
666 12
     */
667 11
    protected function executeActions($exists)
668 11
    {
669 1
        // Renaming a table is tricky, specially when running a reversible migration
670 1
        // down. We will just assume the table already exists if the user commands a
671 1
        // table rename.
672
        $renamed = collection($this->actions->getActions())
673 12
            ->filter(function ($action) {
674
                return $action instanceof RenameTable;
675
            })
676
            ->first();
677
678
        if ($renamed) {
679
            $exists = true;
680 2
        }
681
682 2
        // If the table does not exist, the last command in the chain needs to be
683 2
        // a CreateTable action.
684
        if (!$exists) {
685
            $this->actions->addAction(new CreateTable($this->table));
686
        }
687
688
        $plan = new Plan($this->actions);
689
        $plan->execute($this->getAdapter());
0 ignored issues
show
Bug introduced by
It seems like $this->getAdapter() can be null; however, execute() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
690
    }
691
}
692