Completed
Pull Request — master (#3)
by Rougin
02:45
created

MySQLDriver::getTable()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 52
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 33
CRAP Score 4.0004

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 52
ccs 33
cts 34
cp 0.9706
rs 8.9408
cc 4
eloc 34
nc 6
nop 1
crap 4.0004

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Rougin\Describe\Driver;
4
5
use Rougin\Describe\Column;
6
7
/**
8
 * MySQL Driver
9
 *
10
 * A database driver extension for MySQL.
11
 *
12
 * @package  Describe
13
 * @category Driver
14
 * @author   Rougin Royce Gutib <[email protected]>
15
 */
16
class MySQLDriver implements DriverInterface
17
{
18
    /**
19
     * @var array
20
     */
21
    protected $columns = [];
22
23
    /**
24
     * @var string
25
     */
26
    protected $database;
27
28
    /**
29
     * @var \PDO
30
     */
31
    protected $pdo;
32
33
    /**
34
     * @param PDO    $pdo
35
     * @param string $database
36
     */
37 57
    public function __construct(\PDO $pdo, $database)
38
    {
39 57
        $this->database = $database;
40 57
        $this->pdo = $pdo;
41 57
    }
42
43
    /**
44
     * Returns the result.
45
     *
46
     * @return array
47
     */
48 51
    public function getTable($table)
49
    {
50 51
        $this->columns = [];
51
52
        try {
53 51
            $information = $this->pdo->prepare('DESCRIBE ' . $table);
54
55 51
            $information->execute();
56 51
            $information->setFetchMode(\PDO::FETCH_OBJ);
57 51
        } catch (\PDOException $e) {
58
            return [];
59
        }
60
61 51
        while ($row = $information->fetch()) {
62 48
            preg_match('/(.*?)\((.*?)\)/', $row->Type, $match);
63
64 48
            $column = new Column;
65
66 48
            $this->setProperties($row, $column);
67 48
            $this->setKey($row, $column);
68
69 48
            $column->setDataType($row->Type);
70 48
            $column->setDefaultValue($row->Default);
71 48
            $column->setField($row->Field);
72
73 48
            if (isset($match[1])) {
74 48
                $column->setDataType($match[1]);
75 48
                $column->setLength($match[2]);
76 48
            }
77
78
            $query = 'SELECT COLUMN_NAME as "column",' .
79 48
                'REFERENCED_COLUMN_NAME as "referenced_column",' .
80 48
                'CONCAT(' .
81 48
                    'REFERENCED_TABLE_SCHEMA, ".",' .
82 48
                    'REFERENCED_TABLE_NAME' .
83 48
                ') as "referenced_table"' .
84 48
                'FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE ' .
85 48
                'WHERE CONSTRAINT_SCHEMA = "' . $this->database . '" ' .
86 48
                'AND TABLE_NAME = "' . $table . '";';
87
88 48
            $foreignTable = $this->pdo->prepare($query);
89
90 48
            $foreignTable->execute();
91 48
            $foreignTable->setFetchMode(\PDO::FETCH_OBJ);
92
93 48
            $this->setForeignColumns($foreignTable, $row, $column);
94
95 48
            array_push($this->columns, $column);
96 48
        }
97
98 51
        return $this->columns;
99
    }
100
101
    /**
102
     * Shows the list of tables.
103
     *
104
     * @return array
105
     */
106 6
    public function showTables()
107
    {
108 6
        $tables = [];
109
110 6
        $information = $this->pdo->prepare('SHOW TABLES');
111 6
        $information->execute();
112
113 6
        while ($row = $information->fetch()) {
114 6
            array_push($tables, $row[0]);
115 6
        }
116
117 6
        return $tables;
118
    }
119
120
    /**
121
     * Sets the key of the specified column.
122
     *
123
     * @param  mixed                   $row
124
     * @param  \Rougin\Describe\Column &$column
125
     * @return void
126
     */
127 48
    protected function setKey($row, Column &$column)
128
    {
129 48
        switch ($row->Key) {
130 48
            case 'PRI':
131 48
                $column->setPrimary(true);
132
133 48
                break;
134
            
135 48
            case 'MUL':
136 48
                $column->setForeign(true);
137
138 48
                break;
139
140 48
            case 'UNI':
141 48
                $column->setUnique(true);
142
143 48
                break;
144 48
        }
145 48
    }
146
147
    /**
148
     * Sets the properties of the specified column.
149
     *
150
     * @param  \PDOStatement           $foreignTable
151
     * @param  mixed                   $row
152
     * @param  \Rougin\Describe\Column &$column
153
     * @return void
154
     */
155 48
    protected function setForeignColumns($foreignTable, $row, Column &$column)
156
    {
157 48
        while ($foreignRow = $foreignTable->fetch()) {
158 45
            if ($foreignRow->column == $row->Field) {
159 45
                $referencedTable = $this->stripTableSchema($foreignRow->referenced_table);
160
161 45
                $column->setReferencedField($foreignRow->referenced_column);
162 45
                $column->setReferencedTable($referencedTable);
163 45
            }
164 45
        }
165 48
    }
166
167
    /**
168
     * Sets the properties of the specified column.
169
     *
170
     * @param  mixed                   $row
171
     * @param  \Rougin\Describe\Column &$column
172
     * @return void
173
     */
174 48
    protected function setProperties($row, Column &$column)
175
    {
176 48
        $null = 'Null';
177
178 48
        if ($row->Extra == 'auto_increment') {
179 48
            $column->setAutoIncrement(true);
180 48
        }
181
182 48
        if ($row->$null == 'YES') {
183 48
            $column->setNull(true);
184 48
        }
185 48
    }
186
187
    /**
188
     * Strips the table schema from the table name.
189
     *
190
     * @param  string $table
191
     * @return string
192
     */
193 45
    protected function stripTableSchema($table)
194
    {
195 45
        return (strpos($table, '.') !== false) ? substr($table, strpos($table, '.') + 1) : $table;
196
    }
197
}
198