Completed
Branch develop (c2aa4c)
by Anton
05:17
created

TableSchema   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 90
Duplicated Lines 7.78 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
c 1
b 0
f 0
lcom 1
cbo 5
dl 7
loc 90
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A loadColumns() 0 12 2
B loadIndexes() 0 31 4
A loadReferences() 7 7 2
A columnSchema() 0 4 1
A indexSchema() 0 4 1
A referenceSchema() 0 4 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
 * Spiral Framework.
4
 *
5
 * @license   MIT
6
 * @author    Anton Titov (Wolfy-J)
7
 */
8
namespace Spiral\Database\Drivers\SQLServer\Schemas;
9
10
use Spiral\Database\Entities\Schemas\AbstractTable;
11
12
/**
13
 * SQLServer table schema.
14
 */
15
class TableSchema extends AbstractTable
16
{
17
    /**
18
     * {@inheritdoc}
19
     */
20
    protected function loadColumns()
21
    {
22
        $query = 'SELECT * FROM information_schema.columns INNER JOIN sys.columns AS sysColumns '
23
            . 'ON (object_name(object_id) = table_name AND sysColumns.name = COLUMN_NAME) '
24
            . 'WHERE table_name = ?';
25
26
        foreach ($this->driver->query($query, [$this->getName()]) as $column) {
27
            $this->registerColumn($this->columnSchema($column['COLUMN_NAME'], $column));
28
        }
29
30
        return $this;
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     *
36
     * @link http://stackoverflow.com/questions/765867/list-of-all-index-index-columns-in-sql-server-db
37
     */
38
    protected function loadIndexes()
39
    {
40
        $query = "SELECT indexes.name AS indexName, cl.name AS columnName, "
41
            . "is_primary_key AS isPrimary, is_unique AS isUnique\n"
42
            . "FROM sys.indexes AS indexes\n"
43
            . "INNER JOIN sys.index_columns as columns\n"
44
            . "  ON indexes.object_id = columns.object_id AND indexes.index_id = columns.index_id\n"
45
            . "INNER JOIN sys.columns AS cl\n"
46
            . "  ON columns.object_id = cl.object_id AND columns.column_id = cl.column_id\n"
47
            . "INNER JOIN sys.tables AS t\n"
48
            . "  ON indexes.object_id = t.object_id\n"
49
            . "WHERE t.name = ? ORDER BY indexes.name, indexes.index_id, columns.index_column_id";
50
51
        $indexes = [];
52
        $primaryKeys = [];
53
        foreach ($this->driver->query($query, [$this->getName()]) as $index) {
54
            if ($index['isPrimary']) {
55
                $primaryKeys[] = $index['columnName'];
56
                continue;
57
            }
58
59
            $indexes[$index['indexName']][] = $index;
60
        }
61
62
        $this->setPrimaryKeys($primaryKeys);
63
        foreach ($indexes as $index => $schema) {
64
            $this->registerIndex($this->indexSchema($index, $schema));
65
        }
66
67
        return $this;
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73 View Code Duplication
    protected function loadReferences()
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...
74
    {
75
        $references = $this->driver->query("sp_fkeys @fktable_name = ?", [$this->getName()]);
76
        foreach ($references as $reference) {
77
            $this->registerReference($this->referenceSchema($reference['FK_NAME'], $reference));
78
        }
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84
    protected function columnSchema($name, $schema = null)
85
    {
86
        return new ColumnSchema($this, $name, $schema);
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92
    protected function indexSchema($name, $schema = null)
93
    {
94
        return new IndexSchema($this, $name, $schema);
95
    }
96
97
    /**
98
     * {@inheritdoc}
99
     */
100
    protected function referenceSchema($name, $schema = null)
101
    {
102
        return new ReferenceSchema($this, $name, $schema);
103
    }
104
}