Completed
Pull Request — master (#1393)
by
unknown
03:29
created

Table::reset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
ccs 3
cts 3
cp 1
cc 1
nc 1
nop 0
crap 1
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\ChangeTable;
36
use Phinx\Db\Action\CreateTable;
37
use Phinx\Db\Action\DropColumn;
38
use Phinx\Db\Action\DropForeignKey;
39
use Phinx\Db\Action\DropIndex;
40
use Phinx\Db\Action\DropTable;
41
use Phinx\Db\Action\RemoveColumn;
42
use Phinx\Db\Action\RenameColumn;
43
use Phinx\Db\Action\RenameTable;
44
use Phinx\Db\Adapter\AdapterInterface;
45
use Phinx\Db\Plan\Intent;
46
use Phinx\Db\Plan\Plan;
47
use Phinx\Db\Table\Column;
48
use Phinx\Db\Table\Table as TableValue;
49
50
/**
51
 *
52
 * This object is based loosely on: http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/Table.html.
53
 */
54
class Table
55
{
56
    /**
57
     * @var \Phinx\Db\Table\Table
58
     */
59
    protected $table;
60
61
    /**
62
     * @var \Phinx\Db\Adapter\AdapterInterface
63
     */
64
    protected $adapter;
65
66
    /**
67
     * @var \Phinx\Db\Plan\Intent
68
     */
69
    protected $actions;
70
71
    /**
72
     * @var array
73
     */
74
    protected $data = [];
75
76
    /**
77
     * Class Constuctor.
78
     *
79
     * @param string $name Table Name
80
     * @param array $options Options
81
     * @param \Phinx\Db\Adapter\AdapterInterface $adapter Database Adapter
82
     */
83
    public function __construct($name, $options = [], AdapterInterface $adapter = null)
84 239
    {
85
        $this->table = new TableValue($name, $options);
86 239
        $this->actions = new Intent();
87 239
88
        if ($adapter !== null) {
89 239
            $this->setAdapter($adapter);
90 231
        }
91 231
    }
92 239
93
    /**
94
     * Gets the table name.
95
     *
96
     * @return string|null
97
     */
98
    public function getName()
99
    {
100 239
        return $this->table->getName();
101
    }
102 239
103 239
    /**
104
     * Gets the table options.
105
     *
106
     * @return array
107
     */
108
    public function getOptions()
109
    {
110
        return $this->table->getOptions();
111 215
    }
112
113 215
    /**
114
     * Gets the table name and options as an object
115
     *
116
     * @return \Phinx\Db\Table\Table
117
     */
118
    public function getTable()
119
    {
120
        return $this->table;
121
    }
122 239
123
    /**
124 239
     * Sets the database adapter.
125 239
     *
126
     * @param \Phinx\Db\Adapter\AdapterInterface $adapter Database Adapter
127
     * @return \Phinx\Db\Table
128
     */
129
    public function setAdapter(AdapterInterface $adapter)
130
    {
131
        $this->adapter = $adapter;
132
133 189
        return $this;
134
    }
135 189
136
    /**
137
     * Gets the database adapter.
138
     *
139
     * @return \Phinx\Db\Adapter\AdapterInterface|null
140
     */
141
    public function getAdapter()
142
    {
143
        if (!$this->adapter) {
144 231
            throw new \RuntimeException('There is no database adapter set yet, cannot proceed');
145
        }
146 231
147 231
        return $this->adapter;
148
    }
149
150
    /**
151
     * Does the table exist?
152
     *
153
     * @return bool
154
     */
155 225
    public function exists()
156
    {
157 225
        return $this->getAdapter()->hasTable($this->getName());
158
    }
159
160
    /**
161
     * Drops the database table.
162
     *
163
     * @return \Phinx\Db\Table
164
     */
165 195
    public function drop()
166
    {
167 195
        $this->actions->addAction(new DropTable($this->table));
168
169
        return $this;
170
    }
171
172
    /**
173
     * Renames the database table.
174
     *
175 1
     * @param string $newTableName New Table Name
176
     * @return \Phinx\Db\Table
177 1
     */
178 1
    public function rename($newTableName)
179
    {
180
        $this->actions->addAction(new RenameTable($this->table, $newTableName));
181
182
        return $this;
183
    }
184
185
    /**
186 3
     * @param array $options Options
187
     * @return $this
188 3
     */
189 3
    public function change(array $options)
190 3
    {
191
        $this->actions->addAction(new ChangeTable($this->table, $options));
192
193
        return $this;
194
    }
195
196
    /**
197
     * Gets an array of the table columns.
198
     *
199
     * @return \Phinx\Db\Table\Column[]
200
     */
201
    public function getColumns()
202
    {
203
        return $this->getAdapter()->getColumns($this->getName());
204
    }
205
206
    /**
207
     * Sets an array of data to be inserted.
208
     *
209
     * @param array $data Data
210
     * @return \Phinx\Db\Table
211 10
     */
212
    public function setData($data)
213 10
    {
214
        $this->data = $data;
215
216
        return $this;
217
    }
218
219
    /**
220
     * Gets the data waiting to be inserted.
221
     *
222 196
     * @return array
223
     */
224 196
    public function getData()
225 196
    {
226
        return $this->data;
227
    }
228
229
    /**
230
     * Resets all of the pending data to be inserted
231
     *
232
     * @return void
233 204
     */
234
    public function resetData()
235 204
    {
236
        $this->setData([]);
237
    }
238
239
    /**
240
     * Resets all of the pending table changes.
241
     *
242
     * @return void
243
     */
244 196
    public function reset()
245
    {
246 196
        $this->actions = new Intent();
247 196
        $this->resetData();
248
    }
249
250
    /**
251
     * Add a table column.
252
     *
253
     * Type can be: string, text, integer, float, decimal, datetime, timestamp,
254
     * time, date, binary, boolean.
255 191
     *
256
     * Valid options can be: limit, default, null, precision or scale.
257 191
     *
258
     * @param string|\Phinx\Db\Table\Column $columnName Column Name
259
     * @param string|\Phinx\Util\Literal $type Column Type
260
     * @param array $options Column Options
261
     * @throws \RuntimeException
262
     * @throws \InvalidArgumentException
263
     * @return \Phinx\Db\Table
264
     */
265
    public function addColumn($columnName, $type = null, $options = [])
266 196
    {
267 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...
268 196
            $action = new AddColumn($this->table, $columnName);
269 196
        } else {
270
            $action = AddColumn::build($this->table, $columnName, $type, $options);
271
        }
272
273
        // Delegate to Adapters to check column type
274
        if (!$this->getAdapter()->isValidColumnType($action->getColumn())) {
275
            throw new \InvalidArgumentException(sprintf(
276
                'An invalid column type "%s" was specified for column "%s".',
277 192
                $type,
278
                $action->getColumn()->getName()
279 192
            ));
280
        }
281
282
        $this->actions->addAction($action);
283
284
        return $this;
285
    }
286
287
    /**
288 196
     * Remove a table column.
289
     *
290 196
     * @param string $columnName Column Name
291 196
     * @return \Phinx\Db\Table
292
     */
293
    public function removeColumn($columnName)
294
    {
295
        $action = RemoveColumn::build($this->table, $columnName);
296
        $this->actions->addAction($action);
297
298
        return $this;
299 197
    }
300
301 197
    /**
302
     * Rename a table column.
303
     *
304
     * @param string $oldName Old Column Name
305
     * @param string $newName New Column Name
306
     * @return \Phinx\Db\Table
307
     */
308
    public function renameColumn($oldName, $newName)
309 196
    {
310
        $action = RenameColumn::build($this->table, $oldName, $newName);
311 196
        $this->actions->addAction($action);
312 196
313 196
        return $this;
314 196
    }
315 196
316
    /**
317
     * Change a table column type.
318
     *
319
     * @param string        $columnName    Column Name
320
     * @param string|\Phinx\Db\Table\Column|\Phinx\Util\Literal $newColumnType New Column Type
321
     * @param array         $options       Options
322
     * @return \Phinx\Db\Table
323
     */
324
    public function changeColumn($columnName, $newColumnType, array $options = [])
325
    {
326 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...
327
            $action = new ChangeColumn($this->table, $columnName, $newColumnType);
328
        } else {
329
            $action = ChangeColumn::build($this->table, $columnName, $newColumnType, $options);
330
        }
331
        $this->actions->addAction($action);
332 210
333
        return $this;
334
    }
335 210
336 1
    /**
337
     * Checks to see if a column exists.
338
     *
339
     * @param string $columnName Column Name
340 209
     * @return bool
341 207
     */
342 207
    public function hasColumn($columnName)
343 207
    {
344 207
        return $this->getAdapter()->hasColumn($this->getName(), $columnName);
345 207
    }
346 2
347
    /**
348
     * Add an index to a database table.
349
     *
350 209
     * In $options you can specific unique = true/false or name (index name).
351 1
     *
352 1
     * @param string|array|\Phinx\Db\Table\Index $columns Table Column(s)
353 1
     * @param array $options Index Options
354 1
     * @return \Phinx\Db\Table
355 1
     */
356
    public function addIndex($columns, array $options = [])
357
    {
358 208
        $action = AddIndex::build($this->table, $columns, $options);
359 208
        $this->actions->addAction($action);
360
361
        return $this;
362
    }
363
364
    /**
365
     * Removes the given index from a table.
366
     *
367
     * @param array $columns Columns
368 1
     * @return \Phinx\Db\Table
369
     */
370 1
    public function removeIndex(array $columns)
371 1
    {
372
        $action = DropIndex::build($this->table, $columns);
373
        $this->actions->addAction($action);
374
375
        return $this;
376
    }
377
378
    /**
379
     * Removes the given index identified by its name from a table.
380
     *
381 4
     * @param string $name Index name
382
     * @return \Phinx\Db\Table
383 4
     */
384 4
    public function removeIndexByName($name)
385
    {
386
        $action = DropIndex::buildFromName($this->table, $name);
387
        $this->actions->addAction($action);
388
389
        return $this;
390
    }
391
392
    /**
393
     * Checks to see if an index exists.
394
     *
395 17
     * @param string|array $columns Columns
396
     * @return bool
397
     */
398 17
    public function hasIndex($columns)
399 4
    {
400 4
        return $this->getAdapter()->hasIndex($this->getName(), $columns);
401 4
    }
402 4
403 13
    /**
404
     * Checks to see if an index specified by name exists.
405
     *
406
     * @param string $indexName
407 17
     * @return bool
408 15
     */
409 15
    public function hasIndexByName($indexName)
410
    {
411 17
        return $this->getAdapter()->hasIndexByName($this->getName(), $indexName);
412 17
    }
413
414
    /**
415
     * Add a foreign key to a database table.
416
     *
417
     * In $options you can specify on_delete|on_delete = cascade|no_action ..,
418
     * on_update, constraint = constraint name.
419
     *
420
     * @param string|array $columns Columns
421 89
     * @param string|\Phinx\Db\Table $referencedTable   Referenced Table
422
     * @param string|array $referencedColumns Referenced Columns
423 89
     * @param array $options Options
424
     * @return \Phinx\Db\Table
425
     */
426 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...
427
    {
428
        $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 426 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 426 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...
429
        $this->actions->addAction($action);
430
431
        return $this;
432
    }
433
434
    /**
435 29
     * Add a foreign key to a database table with a given name.
436
     *
437
     * In $options you can specify on_delete|on_delete = cascade|no_action ..,
438 29
     * on_update, constraint = constraint name.
439 28
     *
440 28
     * @param string $name The constaint name
441 22
     * @param string|array $columns Columns
442 22
     * @param string|\Phinx\Db\Table $referencedTable   Referenced Table
443 28
     * @param string|array $referencedColumns Referenced Columns
444 28
     * @param array $options Options
445 28
     * @return \Phinx\Db\Table
446 1
     */
447 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...
448
    {
449 29
        $action = AddForeignKey::build(
450 29
            $this->table,
451
            $columns,
0 ignored issues
show
Bug introduced by
It seems like $columns defined by parameter $columns on line 447 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...
452
            $referencedTable,
0 ignored issues
show
Bug introduced by
It seems like $referencedTable defined by parameter $referencedTable on line 447 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...
453
            $referencedColumns,
454
            $options,
455
            $name
456
        );
457
        $this->actions->addAction($action);
458
459 1
        return $this;
460
    }
461 1
462 1
    /**
463
     * Removes the given foreign key from the table.
464
     *
465
     * @param string|array $columns    Column(s)
466
     * @param null|string  $constraint Constraint names
467
     * @return \Phinx\Db\Table
468
     */
469
    public function dropForeignKey($columns, $constraint = null)
470
    {
471 1
        $action = DropForeignKey::build($this->table, $columns, $constraint);
0 ignored issues
show
Bug introduced by
It seems like $columns defined by parameter $columns on line 469 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...
472
        $this->actions->addAction($action);
473 1
474 1
        return $this;
475
    }
476
477
    /**
478
     * Checks to see if a foreign key exists.
479
     *
480
     * @param  string|array $columns    Column(s)
481
     * @param  null|string  $constraint Constraint names
482
     * @return bool
483
     */
484 12
    public function hasForeignKey($columns, $constraint = null)
485
    {
486 12
        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...
487
    }
488
489
    /**
490
     * Add timestamp columns created_at and updated_at to the table.
491
     *
492
     * @param string|null $createdAt    Alternate name for the created_at column
493
     * @param string|null $updatedAt    Alternate name for the updated_at column
494
     * @param bool        $withTimezone Whether to set the timezone option on the added columns
495
     *
496
     * @return \Phinx\Db\Table
497
     */
498
    public function addTimestamps($createdAt = 'created_at', $updatedAt = 'updated_at', $withTimezone = false)
499
    {
500
        $createdAt = is_null($createdAt) ? 'created_at' : $createdAt;
501 8
        $updatedAt = is_null($updatedAt) ? 'updated_at' : $updatedAt;
502
503 8
        $this->addColumn($createdAt, 'timestamp', [
504 4
                   'default' => 'CURRENT_TIMESTAMP',
505 4
                   'update' => '',
506 8
                   'timezone' => $withTimezone,
507 8
             ])
508
             ->addColumn($updatedAt, 'timestamp', [
509
                 'null' => true,
510 8
                 'default' => null,
511
                 'timezone' => $withTimezone,
512 8
             ]);
513 8
514 8
        return $this;
515 8
    }
516
517 8
    /**
518
     * Alias that always sets $withTimezone to true
519
     * @see addTimestamps
520
     *
521
     * @param string|null $createdAt Alternate name for the created_at column
522
     * @param string|null $updatedAt Alternate name for the updated_at column
523
     *
524
     * @return \Phinx\Db\Table
525
     */
526
    public function addTimestampsWithTimezone($createdAt = null, $updatedAt = null)
527 1
    {
528
        $this->addTimestamps($createdAt, $updatedAt, true);
529 1
530 1
        return $this;
531 1
    }
532 1
533
    /**
534
     * Insert data into the table.
535 1
     *
536
     * @param array $data array of data in the form:
537
     *              array(
538 1
     *                  array("col1" => "value1", "col2" => "anotherValue1"),
539
     *                  array("col2" => "value2", "col2" => "anotherValue2"),
540
     *              )
541
     *              or array("col1" => "value1", "col2" => "anotherValue1")
542
     *
543
     * @return \Phinx\Db\Table
544
     */
545
    public function insert($data)
546
    {
547
        // handle array of array situations
548 1
        if (isset($data[0]) && is_array($data[0])) {
549
            foreach ($data as $row) {
550 1
                $this->data[] = $row;
551
            }
552
553
            return $this;
554
        }
555
        $this->data[] = $data;
556
557
        return $this;
558
    }
559
560
    /**
561 15
     * Creates a table from the object instance.
562
     *
563 15
     * @return void
564 15
     */
565 15
    public function create()
566 15
    {
567
        $this->executeActions(false);
568 15
        $this->saveData();
569 15
        $this->reset(); // reset pending changes
570 15
    }
571
572 15
    /**
573
     * Updates a table from the object instance.
574 15
     *
575
     * @throws \RuntimeException
576
     * @return void
577
     */
578
    public function update()
579
    {
580
        $this->executeActions(true);
581
        $this->saveData();
582
        $this->reset(); // reset pending changes
583
    }
584
585
    /**
586
     * Commit the pending data waiting for insertion.
587
     *
588
     * @return void
589 17
     */
590
    public function saveData()
591
    {
592 17
        $rows = $this->getData();
593 11
        if (empty($rows)) {
594 11
            return;
595 11
        }
596 11
597
        $bulk = true;
598 8
        $row = current($rows);
599 8
        $c = array_keys($row);
600
        foreach ($this->getData() as $row) {
601
            $k = array_keys($row);
602
            if ($k != $c) {
603
                $bulk = false;
604
                break;
605
            }
606
        }
607 196
608
        if ($bulk) {
609 196
            $this->getAdapter()->bulkinsert($this->table, $this->getData());
610 196
        } else {
611 196
            foreach ($this->getData() as $row) {
612 196
                $this->getAdapter()->insert($this->table, $row);
613
            }
614
        }
615
616
        $this->resetData();
617
    }
618
619
    /**
620 46
     * Immediately truncates the table. This operation cannot be undone
621
     *
622 46
     * @return void
623
     */
624
    public function truncate()
625
    {
626
        $this->getAdapter()->truncateTable($this->getName());
627 46
    }
628 38
629 46
    /**
630
     * Commits the table changes.
631 46
     *
632 6
     * If the table doesn't exist it is created otherwise it is updated.
633 46
     *
634
     * @return void
635 46
     */
636 3
    public function save()
637 46
    {
638
        if ($this->exists()) {
639 46
            $this->update(); // update the table
640 46
        } else {
641 46
            $this->create(); // create the table
642
        }
643
    }
644
645
    /**
646
     * Executes all the pending actions for this table
647
     *
648 196
     * @param bool $exists Whether or not the table existed prior to executing this method
649
     * @return void
650 196
     */
651 196
    protected function executeActions($exists)
652 192
    {
653
        // Renaming a table is tricky, specially when running a reversible migration
654
        // down. We will just assume the table already exists if the user commands a
655 12
        // table rename.
656 12
        $renamed = collection($this->actions->getActions())
657 12
            ->filter(function ($action) {
658 12
                return $action instanceof RenameTable;
659 12
            })
660 12
            ->first();
661 1
662 1
        if ($renamed) {
663
            $exists = true;
664 12
        }
665
666 12
        // If the table does not exist, the last command in the chain needs to be
667 11
        // a CreateTable action.
668 11
        if (!$exists) {
669 1
            $this->actions->addAction(new CreateTable($this->table));
670 1
        }
671 1
672
        $plan = new Plan($this->actions);
673 12
        $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...
674
675
        // If a ChangeTable action was executed,
676
        // also change the options on this table instance to the new ones for consistency.
677
        foreach ($this->actions->getActions() as $action) {
678
            if ($action instanceof ChangeTable) {
679
                $this->table->setOptions($action->getNewOptions());
680 2
            }
681
        }
682 2
    }
683
}
684