Completed
Push — master ( 152a5f...57e856 )
by José
11s
created

TimedOutputAdapter   B

Complexity

Total Complexity 39

Size/Duplication

Total Lines 340
Duplicated Lines 26.47 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 53.69%

Importance

Changes 0
Metric Value
wmc 39
lcom 1
cbo 7
dl 90
loc 340
rs 8.2857
c 0
b 0
f 0
ccs 80
cts 149
cp 0.5369

23 Methods

Rating   Name   Duplication   Size   Complexity  
A getAdapterType() 0 4 1
A startCommandTimer() 0 11 2
B writeCommand() 0 29 5
A insert() 7 7 1
A bulkinsert() 7 7 1
A createTable() 7 7 1
A renameTable() 0 11 2
A dropTable() 11 11 2
A truncateTable() 0 7 1
A addColumn() 18 18 2
A renameColumn() 0 11 2
A changeColumn() 11 11 2
A dropColumn() 0 11 2
A addIndex() 11 11 2
A dropIndex() 0 11 2
A dropIndexByName() 0 11 2
A addForeignKey() 11 11 2
A dropForeignKey() 0 11 2
A createDatabase() 0 7 1
A dropDatabase() 0 7 1
A createSchema() 0 7 1
A dropSchema() 0 7 1
A executeActions() 7 7 1

How to fix   Duplicated Code   

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:

1
<?php
2
/**
3
 * Phinx
4
 *
5
 * (The MIT license)
6
 * Copyright (c) 2017 Cake Software Foundation
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\Table\Column;
32
use Phinx\Db\Table\ForeignKey;
33
use Phinx\Db\Table\Index;
34
use Phinx\Db\Table\Table;
35
use Symfony\Component\Console\Output\OutputInterface;
36
37
/**
38
 * Wraps any adpter to record the time spend executing its commands
39
 */
40
class TimedOutputAdapter extends AdapterWrapper implements DirectActionInterface
41
{
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function getAdapterType()
47
    {
48
        return $this->getAdapter()->getAdapterType();
49
    }
50
51
    /**
52
     * Start timing a command.
53
     *
54
     * @return callable A function that is to be called when the command finishes
55
     */
56 5
    public function startCommandTimer()
57
    {
58 5
        $started = microtime(true);
59
60 5
        return function () use ($started) {
61 5
            $end = microtime(true);
62
            if (OutputInterface::VERBOSITY_VERBOSE <= $this->getOutput()->getVerbosity()) {
63
                $this->getOutput()->writeln('    -> ' . sprintf('%.4fs', $end - $started));
64 5
            }
65
        };
66
    }
67
68
    /**
69
     * Write a Phinx command to the output.
70
     *
71
     * @param string $command Command Name
72
     * @param array  $args    Command Args
73
     * @return void
74 5
     */
75
    public function writeCommand($command, $args = [])
76 5
    {
77 5
        if (OutputInterface::VERBOSITY_VERBOSE > $this->getOutput()->getVerbosity()) {
78
            return;
79
        }
80
81
        if (count($args)) {
82
            $outArr = [];
83
            foreach ($args as $arg) {
84
                if (is_array($arg)) {
85
                    $arg = array_map(
86
                        function ($value) {
87
                            return '\'' . $value . '\'';
88
                        },
89
                        $arg
90
                    );
91
                    $outArr[] = '[' . implode(', ', $arg) . ']';
92
                    continue;
93
                }
94
95
                $outArr[] = '\'' . $arg . '\'';
96
            }
97
            $this->getOutput()->writeln(' -- ' . $command . '(' . implode(', ', $outArr) . ')');
98
99
            return;
100
        }
101
102
        $this->getOutput()->writeln(' -- ' . $command);
103
    }
104
105
    /**
106
     * {@inheritdoc}
107
     *
108
     * @return void
109
     */
110 View Code Duplication
    public function insert(Table $table, $row)
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...
111
    {
112
        $end = $this->startCommandTimer();
113
        $this->writeCommand('insert', [$table->getName()]);
114
        parent::insert($table, $row);
115
        $end();
116
    }
117
118
    /**
119
     * {@inheritdoc}
120
     *
121 2
     * @return void
122
     */
123 2 View Code Duplication
    public function bulkinsert(Table $table, $rows)
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...
124 2
    {
125 2
        $end = $this->startCommandTimer();
126 2
        $this->writeCommand('bulkinsert', [$table->getName()]);
127 2
        parent::bulkinsert($table, $rows);
128
        $end();
129
    }
130
131
    /**
132 3
     * {@inheritdoc}
133
     */
134 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...
135 3
    {
136 3
        $end = $this->startCommandTimer();
137 3
        $this->writeCommand('createTable', [$table->getName()]);
138 3
        parent::createTable($table, $columns, $indexes);
139
        $end();
140
    }
141
142
    /**
143 3
     * {@inheritdoc}
144
     */
145 3
    public function renameTable($tableName, $newTableName)
146 3
    {
147 3
        $adapter = $this->getAdapter();
148 3
        if (!$adapter instanceof DirectActionInterface) {
149 3
            throw new \BadMethodCallException('The adapter needs to implement DirectActionInterface');
150
        }
151
        $end = $this->startCommandTimer();
152
        $this->writeCommand('renameTable', [$tableName, $newTableName]);
153
        $adapter->renameTable($tableName, $newTableName);
154 3
        $end();
155
    }
156 3
157 3
    /**
158 3
     * {@inheritdoc}
159 3
     */
160 3 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...
161
    {
162
        $adapter = $this->getAdapter();
163
        if (!$adapter instanceof DirectActionInterface) {
164
            throw new \BadMethodCallException('The adapter needs to implement DirectActionInterface');
165
        }
166
        $end = $this->startCommandTimer();
167
        $this->writeCommand('dropTable', [$tableName]);
168
        $adapter->dropTable($tableName);
169
        $end();
170
    }
171
172
    /**
173
     * {@inheritdoc}
174
     */
175
    public function truncateTable($tableName)
176 3
    {
177
        $end = $this->startCommandTimer();
178 3
        $this->writeCommand('truncateTable', [$tableName]);
179 3
        parent::truncateTable($tableName);
180 3
        $end();
181
    }
182 3
183 3
    /**
184 3
     * {@inheritdoc}
185 3
     */
186 3 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...
187 3
    {
188 3
        $adapter = $this->getAdapter();
189 3
        if (!$adapter instanceof DirectActionInterface) {
190
            throw new \BadMethodCallException('The adapter needs to implement DirectActionInterface');
191
        }
192
        $end = $this->startCommandTimer();
193
        $this->writeCommand(
194 3
            'addColumn',
195
            [
196 3
                $table->getName(),
197 3
                $column->getName(),
198 3
                $column->getType()
199 3
            ]
200 3
        );
201
        $adapter->addColumn($table, $column);
202
        $end();
203
    }
204
205
    /**
206
     * {@inheritdoc}
207
     */
208
    public function renameColumn($tableName, $columnName, $newColumnName)
209
    {
210
        $adapter = $this->getAdapter();
211
        if (!$adapter instanceof DirectActionInterface) {
212
            throw new \BadMethodCallException('The adapter needs to implement DirectActionInterface');
213
        }
214
        $end = $this->startCommandTimer();
215
        $this->writeCommand('renameColumn', [$tableName, $columnName, $newColumnName]);
216 3
        $adapter->renameColumn($tableName, $columnName, $newColumnName);
217
        $end();
218 3
    }
219 3
220 3
    /**
221 3
     * {@inheritdoc}
222 3
     */
223 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...
224
    {
225
        $adapter = $this->getAdapter();
226
        if (!$adapter instanceof DirectActionInterface) {
227
            throw new \BadMethodCallException('The adapter needs to implement DirectActionInterface');
228
        }
229
        $end = $this->startCommandTimer();
230
        $this->writeCommand('changeColumn', [$tableName, $columnName, $newColumn->getType()]);
231
        $adapter->changeColumn($tableName, $columnName, $newColumn);
232
        $end();
233
    }
234
235
    /**
236
     * {@inheritdoc}
237
     */
238
    public function dropColumn($tableName, $columnName)
239
    {
240
        $adapter = $this->getAdapter();
241
        if (!$adapter instanceof DirectActionInterface) {
242
            throw new \BadMethodCallException('The adapter needs to implement DirectActionInterface');
243
        }
244
        $end = $this->startCommandTimer();
245
        $this->writeCommand('dropColumn', [$tableName, $columnName]);
246
        $adapter->dropColumn($tableName, $columnName);
247
        $end();
248
    }
249
250
    /**
251
     * {@inheritdoc}
252
     */
253 View Code Duplication
    public function addIndex(Table $table, Index $index)
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...
254
    {
255
        $adapter = $this->getAdapter();
256
        if (!$adapter instanceof DirectActionInterface) {
257
            throw new \BadMethodCallException('The adapter needs to implement DirectActionInterface');
258
        }
259
        $end = $this->startCommandTimer();
260 3
        $this->writeCommand('addIndex', [$table->getName(), $index->getColumns()]);
261
        $adapter->addIndex($table, $index);
262 3
        $end();
263 3
    }
264 3
265 3
    /**
266 3
     * {@inheritdoc}
267
     */
268
    public function dropIndex($tableName, $columns)
269
    {
270
        $adapter = $this->getAdapter();
271 3
        if (!$adapter instanceof DirectActionInterface) {
272
            throw new \BadMethodCallException('The adapter needs to implement DirectActionInterface');
273 3
        }
274 3
        $end = $this->startCommandTimer();
275 3
        $this->writeCommand('dropIndex', [$tableName, $columns]);
276 3
        $adapter->dropIndex($tableName, $columns);
277 3
        $end();
278
    }
279
280
    /**
281
     * {@inheritdoc}
282 5
     */
283
    public function dropIndexByName($tableName, $indexName)
284 5
    {
285 5
        $adapter = $this->getAdapter();
286 5
        if (!$adapter instanceof DirectActionInterface) {
287 5
            throw new \BadMethodCallException('The adapter needs to implement DirectActionInterface');
288 5
        }
289
        $end = $this->startCommandTimer();
290
        $this->writeCommand('dropIndexByName', [$tableName, $indexName]);
291
        $adapter->dropIndexByName($tableName, $indexName);
292
        $end();
293 5
    }
294
295 5
    /**
296 5
     * {@inheritdoc}
297 5
     */
298 5 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...
299 5
    {
300
        $adapter = $this->getAdapter();
301
        if (!$adapter instanceof DirectActionInterface) {
302
            throw new \BadMethodCallException('The adapter needs to implement DirectActionInterface');
303
        }
304
        $end = $this->startCommandTimer();
305
        $this->writeCommand('addForeignKey', [$table->getName(), $foreignKey->getColumns()]);
306
        $adapter->addForeignKey($table, $foreignKey);
307
        $end();
308
    }
309
310
    /**
311
     * {@inheritdoc}
312
     */
313
    public function dropForeignKey($tableName, $columns, $constraint = null)
314
    {
315
        $adapter = $this->getAdapter();
316
        if (!$adapter instanceof DirectActionInterface) {
317
            throw new \BadMethodCallException('The adapter needs to implement DirectActionInterface');
318
        }
319
        $end = $this->startCommandTimer();
320
        $this->writeCommand('dropForeignKey', [$tableName, $columns]);
321
        $adapter->dropForeignKey($tableName, $columns, $constraint);
322
        $end();
323
    }
324
325
    /**
326
     * {@inheritdoc}
327
     */
328
    public function createDatabase($name, $options = [])
329
    {
330
        $end = $this->startCommandTimer();
331
        $this->writeCommand('createDatabase', [$name]);
332
        parent::createDatabase($name, $options);
333
        $end();
334
    }
335
336
    /**
337
     * {@inheritdoc}
338
     */
339
    public function dropDatabase($name)
340
    {
341
        $end = $this->startCommandTimer();
342
        $this->writeCommand('dropDatabase', [$name]);
343
        parent::dropDatabase($name);
344
        $end();
345
    }
346
347
    /**
348
     * {@inheritdoc}
349
     */
350
    public function createSchema($name = 'public')
351
    {
352
        $end = $this->startCommandTimer();
353
        $this->writeCommand('createSchema', [$name]);
354
        parent::createSchema($name);
355
        $end();
356
    }
357
358
    /**
359
     * {@inheritdoc}
360
     */
361
    public function dropSchema($name)
362
    {
363
        $end = $this->startCommandTimer();
364
        $this->writeCommand('dropSchema', [$name]);
365
        parent::dropSchema($name);
366
        $end();
367
    }
368
369
    /**
370
     * {@inheritdoc}
371
     */
372 View Code Duplication
    public function executeActions(Table $table, array $actions)
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...
373
    {
374
        $end = $this->startCommandTimer();
375
        $this->writeCommand(sprintf('Altering table %s', $table->getName()));
376
        parent::executeActions($table, $actions);
377
        $end();
378
    }
379
}
380