Completed
Pull Request — master (#1557)
by
unknown
01:42
created

AbstractDirectAction::renameColumn()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
nc 1
cc 1
nop 3
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\Adapter
28
 */
29
namespace Phinx\Db\Adapter;
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\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\Table\Column;
44
use Phinx\Db\Table\ForeignKey;
45
use Phinx\Db\Table\Index;
46
use Phinx\Db\Table\Table;
47
use Phinx\Db\Util\AlterInstructions;
48
49
/**
50
 * A generic implementation of DirectActionInterface
51
 *
52
 * @author Rob Morgan <[email protected]>
53
 */
54
trait AbstractDirectAction
55
{
56
    /**
57
     * Executes all the ALTER TABLE instructions passed for the given table
58
     *
59
     * @param string $tableName The table name to use in the ALTER statement
60
     * @param AlterInstructions $instructions The object containing the alter sequence
61
     * @return void
62
     */
63
    abstract protected function executeAlterSteps($tableName, AlterInstructions $instructions);
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function addColumn(Table $table, Column $column)
69
    {
70
        $instructions = $this->getAddColumnInstructions($table, $column);
71
        $this->executeAlterSteps($table->getName(), $instructions);
72
    }
73
74
    /**
75
     * Returns the instructions to add the specified column to a database table.
76
     *
77
     * @param \Phinx\Db\Table\Table $table Table
78
     * @param \Phinx\Db\Table\Column $column Column
79
     * @return AlterInstructions
80
     */
81
    abstract protected function getAddColumnInstructions(Table $table, Column $column);
82
83
    /**
84
     * {@inheritdoc}
85
     */
86
    public function renameColumn($tableName, $columnName, $newColumnName)
87
    {
88
        $instructions = $this->getRenameColumnInstructions($tableName, $columnName, $newColumnName);
89
        $this->executeAlterSteps($tableName, $instructions);
90
    }
91
92
    /**
93
     * Returns the instructions to rename the specified column.
94
     *
95
     * @param string $tableName Table Name
96
     * @param string $columnName Column Name
97
     * @param string $newColumnName New Column Name
98
     * @return AlterInstructions
99
     *
100
     */
101
    abstract protected function getRenameColumnInstructions($tableName, $columnName, $newColumnName);
102
103
    /**
104
     * {@inheritdoc}
105
     */
106
    public function changeColumn($tableName, $columnName, Column $newColumn)
107
    {
108
        $instructions = $this->getChangeColumnInstructions($tableName, $columnName, $newColumn);
109
        $this->executeAlterSteps($tableName, $instructions);
110
    }
111
112
    /**
113
     * Returns the instructions to change a table column type.
114
     *
115
     * @param string $tableName  Table Name
116
     * @param string $columnName Column Name
117
     * @param \Phinx\Db\Table\Column $newColumn  New Column
118
     * @return AlterInstructions
119
     */
120
    abstract protected function getChangeColumnInstructions($tableName, $columnName, Column $newColumn);
121
122
    /**
123
     * {@inheritdoc}
124
     */
125
    public function dropColumn($tableName, $columnName)
126
    {
127
        $instructions = $this->getDropColumnInstructions($tableName, $columnName);
128
        $this->executeAlterSteps($tableName, $instructions);
129
    }
130
131
    /**
132
     * Returns the instructions to drop the specified column.
133
     *
134
     * @param string $tableName Table Name
135
     * @param string $columnName Column Name
136
     * @return AlterInstructions
137
     */
138
    abstract protected function getDropColumnInstructions($tableName, $columnName);
139
140
    /**
141
     * {@inheritdoc}
142
     */
143
    public function addIndex(Table $table, Index $index)
144
    {
145
        $instructions = $this->getAddIndexInstructions($table, $index);
146
        $this->executeAlterSteps($table->getName(), $instructions);
147
    }
148
149
    /**
150
     * Returns the instructions to add the specified index to a database table.
151
     *
152
     * @param \Phinx\Db\Table\Table $table Table
153
     * @param \Phinx\Db\Table\Index $index Index
154
     * @return AlterInstructions
155
     */
156
    abstract protected function getAddIndexInstructions(Table $table, Index $index);
157
158
    /**
159
     * {@inheritdoc}
160
     */
161
    public function dropIndex($tableName, $columns)
162
    {
163
        $instructions = $this->getDropIndexByColumnsInstructions($tableName, $columns);
164
        $this->executeAlterSteps($tableName, $instructions);
165
    }
166
167
    /**
168
     * Returns the instructions to drop the specified index from a database table.
169
     *
170
     * @param string $tableName The name of of the table where the index is
171
     * @param mixed $columns Column(s)
172
     * @return AlterInstructions
173
     */
174
    abstract protected function getDropIndexByColumnsInstructions($tableName, $columns);
175
176
    /**
177
     * {@inheritdoc}
178
     */
179
    public function dropIndexByName($tableName, $indexName)
180
    {
181
        $instructions = $this->getDropIndexByNameInstructions($tableName, $indexName);
182
        $this->executeAlterSteps($tableName, $instructions);
183
    }
184
185
    /**
186
     * Returns the instructions to drop the index specified by name from a database table.
187
     *
188
     * @param string $tableName The table name whe the index is
189
     * @param string $indexName The name of the index
190
     * @return AlterInstructions
191
     */
192
    abstract protected function getDropIndexByNameInstructions($tableName, $indexName);
193
194
    /**
195
     * {@inheritdoc}
196
     */
197
    public function addForeignKey(Table $table, ForeignKey $foreignKey)
198
    {
199
        $instructions = $this->getAddForeignKeyInstructions($table, $foreignKey);
200
        $this->executeAlterSteps($table->getName(), $instructions);
201
    }
202
203
    /**
204
     * Returns the instructions to adds the specified foreign key to a database table.
205
     *
206
     * @param \Phinx\Db\Table\Table $table The table to add the constraint to
207
     * @param \Phinx\Db\Table\ForeignKey $foreignKey The foreign key to add
208
     * @return AlterInstructions
209
     */
210
    abstract protected function getAddForeignKeyInstructions(Table $table, ForeignKey $foreignKey);
211
212
    /**
213
     * {@inheritdoc}
214
     */
215
    public function dropForeignKey($tableName, $columns, $constraint = null)
216
    {
217
        if ($constraint) {
218
            $instructions = $this->getDropForeignKeyInstructions($tableName, $constraint);
219
        } else {
220
            $instructions = $this->getDropForeignKeyByColumnsInstructions($tableName, $columns);
221
        }
222
223
        $this->executeAlterSteps($tableName, $instructions);
224
    }
225
226
    /**
227
     * Returns the instructions to drop the specified foreign key from a database table.
228
     *
229
     * @param string   $tableName The table where the foreign key constraint is
230
     * @param string   $constraint Constraint name
231
     * @return AlterInstructions
232
     */
233
    abstract protected function getDropForeignKeyInstructions($tableName, $constraint);
234
235
    /**
236
     * Returns the instructions to drop the specified foreign key from a database table.
237
     *
238
     * @param string $tableName The table where the foreign key constraint is
239
     * @param array $columns The list of column names
240
     * @return AlterInstructions
241
     */
242
    abstract protected function getDropForeignKeyByColumnsInstructions($tableName, $columns);
243
244
    /**
245
     * {@inheritdoc}
246
     */
247
    public function dropTable($tableName)
248
    {
249
        $instructions = $this->getDropTableInstructions($tableName);
250
        $this->executeAlterSteps($tableName, $instructions);
251
    }
252
253
    /**
254
     * Returns the instructions to drop the specified database table.
255
     *
256
     * @param string $tableName Table Name
257
     * @return AlterInstructions
258
     */
259
    abstract protected function getDropTableInstructions($tableName);
260
261
    /**
262
     * {@inheritdoc}
263
     */
264
    public function renameTable($tableName, $newTableName)
265
    {
266
        $instructions = $this->getRenameTableInstructions($tableName, $newTableName);
267
        $this->executeAlterSteps($tableName, $instructions);
268
    }
269
270
    /**
271
     * Returns the instructions to rename the specified database table.
272
     *
273
     * @param string $tableName Table Name
274
     * @param string $newTableName New Name
275
     * @return AlterInstructions
276
     */
277
    abstract protected function getRenameTableInstructions($tableName, $newTableName);
278
279
    /**
280
     * {@inheritdoc}
281
     */
282
    public function changePrimaryKey(Table $table, $newColumns)
283
    {
284
        $instructions = $this->getChangePrimaryKeyInstructions($table, $newColumns);
285
        $this->executeAlterSteps($table->getName(), $instructions);
286
    }
287
288
    /**
289
     * Returns the instructions to change the primary key for the specified database table.
290
     *
291
     * @param Table $table Table
292
     * @param string|array|null $newColumns Column name(s) to belong to the primary key, or null to drop the key
293
     * @return AlterInstructions
294
     */
295
    abstract protected function getChangePrimaryKeyInstructions(Table $table, $newColumns);
296
297
    /**
298
     * {@inheritdoc}
299
     */
300
    public function changeComment(Table $table, $newComment)
301
    {
302
        $instructions = $this->getChangeCommentInstructions($table, $newComment);
303
        $this->executeAlterSteps($table->getName(), $instructions);
304
    }
305
306
    /**
307
     * Returns the instruction to change the comment for the specified database table.
308
     *
309
     * @param Table $table Table
310
     * @param string|null $newComment New comment string, or null to drop the comment
311
     * @return AlterInstructions
312
     */
313
    abstract protected function getChangeCommentInstructions(Table $table, $newComment);
314
315
    /**
316
     * {@inheritdoc}
317
     */
318
    public function executeActions(Table $table, array $actions)
319
    {
320
        $instructions = new AlterInstructions();
321
322
        foreach ($actions as $action) {
323
            switch (true) {
324
                case ($action instanceof AddColumn):
325
                    $instructions->merge($this->getAddColumnInstructions($table, $action->getColumn()));
326
                    break;
327
328
                case ($action instanceof AddIndex):
329
                    $instructions->merge($this->getAddIndexInstructions($table, $action->getIndex()));
330
                    break;
331
332
                case ($action instanceof AddForeignKey):
333
                    $instructions->merge($this->getAddForeignKeyInstructions($table, $action->getForeignKey()));
334
                    break;
335
336
                case ($action instanceof ChangeColumn):
337
                    $instructions->merge($this->getChangeColumnInstructions(
338
                        $table->getName(),
339
                        $action->getColumnName(),
340
                        $action->getColumn()
341
                    ));
342
                    break;
343
344
                case ($action instanceof DropForeignKey && !$action->getForeignKey()->getConstraint()):
345
                    $instructions->merge($this->getDropForeignKeyByColumnsInstructions(
346
                        $table->getName(),
347
                        $action->getForeignKey()->getColumns()
348
                    ));
349
                    break;
350
351
                case ($action instanceof DropForeignKey && $action->getForeignKey()->getConstraint()):
352
                    $instructions->merge($this->getDropForeignKeyInstructions(
353
                        $table->getName(),
354
                        $action->getForeignKey()->getConstraint()
0 ignored issues
show
Bug introduced by
It seems like $action->getForeignKey()->getConstraint() targeting Phinx\Db\Table\ForeignKey::getConstraint() can also be of type boolean; however, Phinx\Db\Adapter\Abstrac...oreignKeyInstructions() does only seem to accept string, maybe add an additional type check?

This check looks at variables that 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...
355
                    ));
356
                    break;
357
358
                case ($action instanceof DropIndex && $action->getIndex()->getName() !== null):
359
                    $instructions->merge($this->getDropIndexByNameInstructions(
360
                        $table->getName(),
361
                        $action->getIndex()->getName()
362
                    ));
363
                    break;
364
365
                case ($action instanceof DropIndex && $action->getIndex()->getName() == null):
366
                    $instructions->merge($this->getDropIndexByColumnsInstructions(
367
                        $table->getName(),
368
                        $action->getIndex()->getColumns()
369
                    ));
370
                    break;
371
372
                case ($action instanceof DropTable):
373
                    $instructions->merge($this->getDropTableInstructions(
374
                        $table->getName()
375
                    ));
376
                    break;
377
378
                case ($action instanceof RemoveColumn):
379
                    $instructions->merge($this->getDropColumnInstructions(
380
                        $table->getName(),
381
                        $action->getColumn()->getName()
382
                    ));
383
                    break;
384
385
                case ($action instanceof RenameColumn):
386
                    $instructions->merge($this->getRenameColumnInstructions(
387
                        $table->getName(),
388
                        $action->getColumn()->getName(),
389
                        $action->getNewName()
390
                    ));
391
                    break;
392
393
                case ($action instanceof RenameTable):
394
                    $instructions->merge($this->getRenameTableInstructions(
395
                        $table->getName(),
396
                        $action->getNewName()
397
                    ));
398
                    break;
399
400
                case ($action instanceof ChangePrimaryKey):
401
                    $instructions->merge($this->getChangePrimaryKeyInstructions(
402
                        $table,
403
                        $action->getNewColumns()
404
                    ));
405
                    break;
406
407
                case ($action instanceof ChangeComment):
408
                    $instructions->merge($this->getChangeCommentInstructions(
409
                        $table,
410
                        $action->getNewComment()
411
                    ));
412
                    break;
413
414
                default:
415
                    throw new \InvalidArgumentException(
416
                        sprintf("Don't know how to execute action: '%s'", get_class($action))
417
                    );
418
            }
419
        }
420
421
        $this->executeAlterSteps($table->getName(), $instructions);
422
    }
423
}
424