Completed
Pull Request — master (#1393)
by
unknown
01:51
created

TablePrefixAdapter   C

Complexity

Total Complexity 53

Size/Duplication

Total Lines 395
Duplicated Lines 25.06 %

Coupling/Cohesion

Components 1
Dependencies 15

Test Coverage

Coverage 81.19%

Importance

Changes 0
Metric Value
wmc 53
lcom 1
cbo 15
dl 99
loc 395
rs 6.96
c 0
b 0
f 0
ccs 82
cts 101
cp 0.8119

27 Methods

Rating   Name   Duplication   Size   Complexity  
A getAdapterType() 0 4 1
A hasTable() 0 6 1
A createTable() 8 8 1
A changePrimaryKey() 0 8 1
A changeComment() 8 8 1
A renameTable() 0 11 2
A dropTable() 9 9 2
A getColumns() 0 6 1
A hasColumn() 0 6 1
A addColumn() 10 10 2
A renameColumn() 9 9 2
A changeColumn() 9 9 2
A dropColumn() 9 9 2
A hasIndex() 0 6 1
A hasIndexByName() 0 6 1
A addIndex() 0 9 2
A dropIndex() 9 9 2
A dropIndexByName() 9 9 2
A hasPrimaryKey() 0 6 1
A hasForeignKey() 0 6 1
A addForeignKey() 10 10 2
A dropForeignKey() 9 9 2
A bulkinsert() 0 6 1
A getPrefix() 0 4 1
A getSuffix() 0 4 1
A getAdapterTableName() 0 4 1
C executeActions() 0 68 14

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like TablePrefixAdapter often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use TablePrefixAdapter, and based on these observations, apply Extract Interface, too.

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\ChangePrimaryKey;
36
use Phinx\Db\Action\ChangeComment;
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
48
/**
49
 * Table prefix/suffix adapter.
50
 *
51
 * Used for inserting a prefix or suffix into table names.
52
 *
53
 * @author Samuel Fisher <[email protected]>
54
 */
55
class TablePrefixAdapter extends AdapterWrapper implements DirectActionInterface
56 2
{
57
    /**
58 2
     * {@inheritdoc}
59 2
     */
60
    public function getAdapterType()
61
    {
62
        return 'TablePrefixAdapter';
63
    }
64
65 3
    /**
66
     * {@inheritdoc}
67 3
     */
68 3
    public function hasTable($tableName)
69 3
    {
70
        $adapterTableName = $this->getAdapterTableName($tableName);
71 3
72 1
        return parent::hasTable($adapterTableName);
73 1
    }
74 1
75 3
    /**
76
     * {@inheritdoc}
77 3
     */
78 3 View Code Duplication
    public function createTable(Table $table, array $columns = [], array $indexes = [])
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...
79
    {
80
        $adapterTable = new Table(
81
            $this->getAdapterTableName($table->getName()),
82
            $table->getOptions()
83 1
        );
84
        parent::createTable($adapterTable, $columns, $indexes);
85 1
    }
86 1
87 1
    /**
88 1
     * {@inheritdoc}
89
     */
90
    public function changePrimaryKey(Table $table, $newColumns)
91
    {
92
        $adapterTable = new Table(
93 1
            $this->getAdapterTableName($table->getName()),
94
            $table->getOptions()
95 1
        );
96 1
        parent::changePrimaryKey($adapterTable, $newColumns);
97 1
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102 View Code Duplication
    public function changeComment(Table $table, string $newComment = null)
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...
103
    {
104
        $adapterTable = new Table(
105
            $this->getAdapterTableName($table->getName()),
106
            $table->getOptions()
107
        );
108
        parent::changeComment($adapterTable, $newComment);
109
    }
110
111 1
    /**
112
     * {@inheritdoc}
113 1
     */
114 1
    public function renameTable($tableName, $newTableName)
115
    {
116
        $adapter = $this->getAdapter();
117
        if (!$adapter instanceof DirectActionInterface) {
118
            throw new \BadMethodCallException('The underlying adapter does not implement DirectActionInterface');
119
        }
120 1
121
        $adapterTableName = $this->getAdapterTableName($tableName);
122 1
        $adapterNewTableName = $this->getAdapterTableName($newTableName);
123 1
        $adapter->renameTable($adapterTableName, $adapterNewTableName);
124
    }
125
126
    /**
127
     * {@inheritdoc}
128
     */
129 1 View Code Duplication
    public function dropTable($tableName)
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...
130
    {
131 1
        $adapter = $this->getAdapter();
132 1
        if (!$adapter instanceof DirectActionInterface) {
133 1
            throw new \BadMethodCallException('The underlying adapter does not implement DirectActionInterface');
134 1
        }
135 1
        $adapterTableName = $this->getAdapterTableName($tableName);
136
        $adapter->dropTable($adapterTableName);
137
    }
138
139
    /**
140 1
     * {@inheritdoc}
141
     */
142 1
    public function truncateTable($tableName)
143 1
    {
144 1
        $adapterTableName = $this->getAdapterTableName($tableName);
145
        parent::truncateTable($adapterTableName);
146
    }
147
148
    /**
149 1
     * {@inheritdoc}
150
     */
151 1
    public function getColumns($tableName)
152 1
    {
153
        $adapterTableName = $this->getAdapterTableName($tableName);
154
155
        return parent::getColumns($adapterTableName);
156
    }
157
158 1
    /**
159
     * {@inheritdoc}
160 1
     */
161 1
    public function hasColumn($tableName, $columnName)
162 1
    {
163
        $adapterTableName = $this->getAdapterTableName($tableName);
164
165
        return parent::hasColumn($adapterTableName, $columnName);
166
    }
167 1
168
    /**
169 1
     * {@inheritdoc}
170 1
     */
171 View Code Duplication
    public function addColumn(Table $table, Column $column)
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...
172
    {
173
        $adapter = $this->getAdapter();
174
        if (!$adapter instanceof DirectActionInterface) {
175
            throw new \BadMethodCallException('The underlying adapter does not implement DirectActionInterface');
176
        }
177
        $adapterTableName = $this->getAdapterTableName($table->getName());
178
        $adapterTable = new Table($adapterTableName, $table->getOptions());
179
        $adapter->addColumn($adapterTable, $column);
180
    }
181
182
    /**
183
     * {@inheritdoc}
184
     */
185 View Code Duplication
    public function renameColumn($tableName, $columnName, $newColumnName)
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...
186
    {
187
        $adapter = $this->getAdapter();
188
        if (!$adapter instanceof DirectActionInterface) {
189
            throw new \BadMethodCallException('The underlying adapter does not implement DirectActionInterface');
190
        }
191
        $adapterTableName = $this->getAdapterTableName($tableName);
192
        $adapter->renameColumn($adapterTableName, $columnName, $newColumnName);
193
    }
194
195
    /**
196 1
     * {@inheritdoc}
197
     */
198 1 View Code Duplication
    public function changeColumn($tableName, $columnName, Column $newColumn)
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...
199 1
    {
200 1
        $adapter = $this->getAdapter();
201
        if (!$adapter instanceof DirectActionInterface) {
202
            throw new \BadMethodCallException('The underlying adapter does not implement DirectActionInterface');
203
        }
204
        $adapterTableName = $this->getAdapterTableName($tableName);
205 1
        $adapter->changeColumn($adapterTableName, $columnName, $newColumn);
206
    }
207 1
208 1
    /**
209 1
     * {@inheritdoc}
210
     */
211 View Code Duplication
    public function dropColumn($tableName, $columnName)
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...
212
    {
213
        $adapter = $this->getAdapter();
214 1
        if (!$adapter instanceof DirectActionInterface) {
215
            throw new \BadMethodCallException('The underlying adapter does not implement DirectActionInterface');
216 1
        }
217 1
        $adapterTableName = $this->getAdapterTableName($tableName);
218
        $adapter->dropColumn($adapterTableName, $columnName);
219
    }
220
221
    /**
222
     * {@inheritdoc}
223 1
     */
224
    public function hasIndex($tableName, $columns)
225 1
    {
226 1
        $adapterTableName = $this->getAdapterTableName($tableName);
227 1
228 1
        return parent::hasIndex($adapterTableName, $columns);
229 1
    }
230
231
    /**
232
     * {@inheritdoc}
233
     */
234 1
    public function hasIndexByName($tableName, $indexName)
235
    {
236 1
        $adapterTableName = $this->getAdapterTableName($tableName);
237 1
238 1
        return parent::hasIndexByName($adapterTableName, $indexName);
239
    }
240
241
    /**
242
     * {@inheritdoc}
243
     */
244
    public function addIndex(Table $table, Index $index)
245
    {
246
        $adapter = $this->getAdapter();
247
        if (!$adapter instanceof DirectActionInterface) {
248
            throw new \BadMethodCallException('The underlying adapter does not implement DirectActionInterface');
249
        }
250
        $adapterTable = new Table($table->getName(), $table->getOptions());
251
        $adapter->addIndex($adapterTable, $index);
252
    }
253
254 1
    /**
255
     * {@inheritdoc}
256 1
     */
257 1 View Code Duplication
    public function dropIndex($tableName, $columns)
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...
258 1
    {
259 1
        $adapter = $this->getAdapter();
260 1
        if (!$adapter instanceof DirectActionInterface) {
261
            throw new \BadMethodCallException('The underlying adapter does not implement DirectActionInterface');
262
        }
263
        $adapterTableName = $this->getAdapterTableName($tableName);
264
        $adapter->dropIndex($adapterTableName, $columns);
265
    }
266
267 19
    /**
268
     * {@inheritdoc}
269 19
     */
270 View Code Duplication
    public function dropIndexByName($tableName, $indexName)
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...
271
    {
272
        $adapter = $this->getAdapter();
273
        if (!$adapter instanceof DirectActionInterface) {
274
            throw new \BadMethodCallException('The underlying adapter does not implement DirectActionInterface');
275
        }
276
        $adapterTableName = $this->getAdapterTableName($tableName);
277 19
        $adapter->dropIndexByName($adapterTableName, $indexName);
278
    }
279 19
280
    /**
281
     * {@inheritdoc}
282
     */
283
    public function hasPrimaryKey($tableName, $columns, $constraint = null)
284
    {
285
        $adapterTableName = $this->getAdapterTableName($tableName);
286
287
        return parent::hasPrimaryKey($adapterTableName, $columns, $constraint);
288 19
    }
289
290 19
    /**
291
     * {@inheritdoc}
292
     */
293
    public function hasForeignKey($tableName, $columns, $constraint = null)
294
    {
295
        $adapterTableName = $this->getAdapterTableName($tableName);
296
297
        return parent::hasForeignKey($adapterTableName, $columns, $constraint);
298
    }
299
300
    /**
301
     * {@inheritdoc}
302
     */
303 View Code Duplication
    public function addForeignKey(Table $table, ForeignKey $foreignKey)
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...
304
    {
305
        $adapter = $this->getAdapter();
306
        if (!$adapter instanceof DirectActionInterface) {
307
            throw new \BadMethodCallException('The underlying adapter does not implement DirectActionInterface');
308
        }
309
        $adapterTableName = $this->getAdapterTableName($table->getName());
310
        $adapterTable = new Table($adapterTableName, $table->getOptions());
311
        $adapter->addForeignKey($adapterTable, $foreignKey);
312
    }
313
314
    /**
315
     * {@inheritdoc}
316
     */
317 View Code Duplication
    public function dropForeignKey($tableName, $columns, $constraint = null)
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...
318
    {
319
        $adapter = $this->getAdapter();
320
        if (!$adapter instanceof DirectActionInterface) {
321
            throw new \BadMethodCallException('The underlying adapter does not implement DirectActionInterface');
322
        }
323
        $adapterTableName = $this->getAdapterTableName($tableName);
324
        $adapter->dropForeignKey($adapterTableName, $columns, $constraint);
325
    }
326
327
    /**
328
     * {@inheritdoc}
329
     */
330
    public function insert(Table $table, $row)
331
    {
332
        $adapterTableName = $this->getAdapterTableName($table->getName());
333
        $adapterTable = new Table($adapterTableName, $table->getOptions());
334
        parent::insert($adapterTable, $row);
335
    }
336
337
    /**
338
     * {@inheritdoc}
339
     */
340
    public function bulkinsert(Table $table, $rows)
341
    {
342
        $adapterTableName = $this->getAdapterTableName($table->getName());
343
        $adapterTable = new Table($adapterTableName, $table->getOptions());
344
        parent::bulkinsert($adapterTable, $rows);
345
    }
346
347
    /**
348
     * Gets the table prefix.
349
     *
350
     * @return string
351
     */
352
    public function getPrefix()
353
    {
354
        return (string)$this->getOption('table_prefix');
355
    }
356
357
    /**
358
     * Gets the table suffix.
359
     *
360
     * @return string
361
     */
362
    public function getSuffix()
363
    {
364
        return (string)$this->getOption('table_suffix');
365
    }
366
367
    /**
368
     * Applies the prefix and suffix to the table name.
369
     *
370
     * @param string $tableName
371
     * @return string
372
     */
373
    public function getAdapterTableName($tableName)
374
    {
375
        return $this->getPrefix() . $tableName . $this->getSuffix();
376
    }
377
378
    /**
379
     * {@inheritdoc}
380
     */
381
    public function executeActions(Table $table, array $actions)
382
    {
383
        $adapterTableName = $this->getAdapterTableName($table->getName());
384
        $adapterTable = new Table($adapterTableName, $table->getOptions());
385
386
        foreach ($actions as $k => $action) {
387
            switch (true) {
388
                case ($action instanceof AddColumn):
389
                    $actions[$k] = new AddColumn($adapterTable, $action->getColumn());
390
                    break;
391
392
                case ($action instanceof AddIndex):
393
                    $actions[$k] = new AddIndex($adapterTable, $action->getIndex());
394
                    break;
395
396
                case ($action instanceof AddForeignKey):
397
                    $foreignKey = clone $action->getForeignKey();
398
                    $refTable = $foreignKey->getReferencedTable();
399
                    $refTableName = $this->getAdapterTableName($refTable->getName());
400
                    $foreignKey->setReferencedTable(new Table($refTableName, $refTable->getOptions()));
401
                    $actions[$k] = new AddForeignKey($adapterTable, $foreignKey);
402
                    break;
403
404
                case ($action instanceof ChangeColumn):
405
                    $actions[$k] = new ChangeColumn($adapterTable, $action->getColumnName(), $action->getColumn());
406
                    break;
407
408
                case ($action instanceof DropForeignKey):
409
                    $actions[$k] = new DropForeignKey($adapterTable, $action->getForeignKey());
410
                    break;
411
412
                case ($action instanceof DropIndex):
413
                    $actions[$k] = new DropIndex($adapterTable, $action->getIndex());
414
                    break;
415
416
                case ($action instanceof DropTable):
417
                    $actions[$k] = new DropTable($adapterTable);
418
                    break;
419
420
                case ($action instanceof RemoveColumn):
421
                    $actions[$k] = new RemoveColumn($adapterTable, $action->getColumn());
422
                    break;
423
424
                case ($action instanceof RenameColumn):
425
                    $actions[$k] = new RenameColumn($adapterTable, $action->getColumn(), $action->getNewName());
426
                    break;
427
428
                case ($action instanceof RenameTable):
429
                    $actions[$k] = new RenameTable($adapterTable, $action->getNewName());
430
                    break;
431
432
                case ($action instanceof ChangePrimaryKey):
433
                    $actions[$k] = new ChangePrimaryKey($adapterTable, $action->getNewColumns());
434
                    break;
435
436
                case ($action instanceof ChangeComment):
437
                    $actions[$k] = new ChangeComment($adapterTable, $action->getNewComment());
438
                    break;
439
440
                default:
441
                    throw new \InvalidArgumentException(
442
                        sprintf("Forgot to implement table prefixing for action: '%s'", get_class($action))
443
                    );
444
            }
445
        }
446
447
        parent::executeActions($adapterTable, $actions);
448
    }
449
}
450