Completed
Push — master ( c4e3cd...c5cae6 )
by Anton
05:38
created

Commander   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 74
Duplicated Lines 39.19 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 29
loc 74
wmc 6
lcom 1
cbo 7
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A alterColumn() 17 17 1
A dropIndex() 0 6 1
A alterIndex() 12 12 1
A dropForeign() 0 6 1
A createStatement() 0 8 2

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
 * Spiral Framework.
4
 *
5
 * @license   MIT
6
 * @author    Anton Titov (Wolfy-J)
7
 */
8
9
namespace Spiral\Database\Drivers\MySQL\Schemas;
10
11
use Spiral\Database\Entities\Schemas\AbstractColumn;
12
use Spiral\Database\Entities\Schemas\AbstractCommander;
13
use Spiral\Database\Entities\Schemas\AbstractIndex;
14
use Spiral\Database\Entities\Schemas\AbstractReference;
15
use Spiral\Database\Entities\Schemas\AbstractTable;
16
use Spiral\Database\Exceptions\SchemaException;
17
18
/**
19
 * MySQL commander.
20
 */
21
class Commander extends AbstractCommander
22
{
23
    /**
24
     * {@inheritdoc}
25
     */
26 View Code Duplication
    public function alterColumn(
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...
27
        AbstractTable $table,
28
        AbstractColumn $initial,
29
        AbstractColumn $column
30
    ) {
31
        $query = "ALTER TABLE {table} CHANGE {column} {statement}";
32
33
        $query = \Spiral\interpolate($query, [
34
            'table'     => $table->getName(true),
35
            'column'    => $initial->getName(true),
36
            'statement' => $column->sqlStatement()
37
        ]);
38
39
        $this->run($query);
40
41
        return $this;
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function dropIndex(AbstractTable $table, AbstractIndex $index)
48
    {
49
        $this->run("DROP INDEX {$index->getName(true)} ON {$table->getName(true)}");
50
51
        return $this;
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57 View Code Duplication
    public function alterIndex(AbstractTable $table, AbstractIndex $initial, AbstractIndex $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...
58
    {
59
        $query = \Spiral\interpolate("ALTER TABLE {table} DROP INDEX {index}, ADD {statement}", [
60
            'table'     => $table->getName(true),
61
            'index'     => $initial->getName(true),
62
            'statement' => $index->sqlStatement(false)
63
        ]);
64
65
        $this->run($query);
66
67
        return $this;
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function dropForeign(AbstractTable $table, AbstractReference $foreign)
74
    {
75
        $this->run("ALTER TABLE {$table->getName(true)} DROP FOREIGN KEY {$foreign->getName(true)}");
76
77
        return $this;
78
    }
79
80
    /**
81
     * Get statement needed to create table.
82
     *
83
     * @param AbstractTable $table
84
     * @return string
85
     */
86
    protected function createStatement(AbstractTable $table)
87
    {
88
        if (!$table instanceof TableSchema) {
89
            throw new SchemaException("MySQL commander can process only MySQL tables.");
90
        }
91
92
        return parent::createStatement($table) . " ENGINE {$table->getEngine()}";
93
    }
94
}