Completed
Pull Request — master (#1402)
by José
07:52 queued 05:56
created

Table::getColumn()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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