Completed
Pull Request — master (#1393)
by José
03:31 queued 01:37
created

Table::setData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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