Completed
Pull Request — master (#3)
by Rougin
02:43
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 54
    public function __construct(\PDO $pdo, $database)
38
    {
39 54
        $this->database = $database;
40 54
        $this->pdo = $pdo;
41 54
    }
42
43
    /**
44
     * Returns the result.
45
     *
46
     * @return array
47
     */
48 48
    public function getTable($table)
49
    {
50 48
        $this->columns = [];
51
52
        try {
53 48
            $information = $this->pdo->prepare('DESCRIBE ' . $table);
54
55 48
            $information->execute();
56 48
            $information->setFetchMode(\PDO::FETCH_OBJ);
57 48
        } catch (\PDOException $e) {
58
            return [];
59
        }
60
61 48
        while ($row = $information->fetch()) {
62 45
            preg_match('/(.*?)\((.*?)\)/', $row->Type, $match);
63
64 45
            $column = new Column;
65
66 45
            $this->setProperties($row, $column);
67 45
            $this->setKey($row, $column);
68
69 45
            $column->setDataType($row->Type);
70 45
            $column->setDefaultValue($row->Default);
71 45
            $column->setField($row->Field);
72
73 45
            if (isset($match[1])) {
74 45
                $column->setDataType($match[1]);
75 45
                $column->setLength($match[2]);
76 45
            }
77
78
            $query = 'SELECT COLUMN_NAME as "column",' .
79 45
                'REFERENCED_COLUMN_NAME as "referenced_column",' .
80 45
                'CONCAT(' .
81 45
                    'REFERENCED_TABLE_SCHEMA, ".",' .
82 45
                    'REFERENCED_TABLE_NAME' .
83 45
                ') as "referenced_table"' .
84 45
                'FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE ' .
85 45
                'WHERE CONSTRAINT_SCHEMA = "' . $this->database . '" ' .
86 45
                'AND TABLE_NAME = "' . $table . '";';
87
88 45
            $foreignTable = $this->pdo->prepare($query);
89
90 45
            $foreignTable->execute();
91 45
            $foreignTable->setFetchMode(\PDO::FETCH_OBJ);
92
93 45
            $this->setForeignColumns($foreignTable, $row, $column);
94
95 45
            array_push($this->columns, $column);
96 45
        }
97
98 48
        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 45
    protected function setKey($row, Column &$column)
128
    {
129 45
        switch ($row->Key) {
130 45
            case 'PRI':
131 45
                $column->setPrimary(true);
132
133 45
                break;
134
            
135 45
            case 'MUL':
136 45
                $column->setForeign(true);
137
138 45
                break;
139
140 45
            case 'UNI':
141 45
                $column->setUnique(true);
142
143 45
                break;
144 45
        }
145 45
    }
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 45
    protected function setForeignColumns($foreignTable, $row, Column &$column)
156
    {
157 45
        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 45
    }
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 45
    protected function setProperties($row, Column &$column)
175
    {
176 45
        $null = 'Null';
177
178 45
        if ($row->Extra == 'auto_increment') {
179 45
            $column->setAutoIncrement(true);
180 45
        }
181
182 45
        if ($row->$null == 'YES') {
183 45
            $column->setNull(true);
184 45
        }
185 45
    }
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