Completed
Push — master ( 43fe8f...74d971 )
by Rougin
09:33
created

MySQLDriver::getTable()   C

Complexity

Conditions 11
Paths 194

Size

Total Lines 77
Code Lines 48

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 57.5506

Importance

Changes 4
Bugs 1 Features 2
Metric Value
c 4
b 1
f 2
dl 0
loc 77
ccs 15
cts 55
cp 0.2727
rs 5.0757
cc 11
eloc 48
nc 194
nop 1
crap 57.5506

How to fix   Long Method    Complexity   

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\Drivers;
4
5
use PDO;
6
use Rougin\Describe\Column;
7
8
/**
9
 * MySQL Driver
10
 *
11
 * A database driver extension for MySQL.
12
 *
13
 * @package  Describe
14
 * @category Drivers
15
 * @author   Rougin Royce Gutib <[email protected]>
16
 */
17
class MySQLDriver implements DriverInterface
18
{
19
    protected $database;
20
    protected $columns = [];
21
    protected $pdo;
22
23
    /**
24
     * @param PDO    $pdo
25
     * @param string $database
26
     */
27 51
    public function __construct(PDO $pdo, $database)
28
    {
29 51
        $this->database = $database;
30 51
        $this->pdo = $pdo;
31 51
    }
32
33
    /**
34
     * Returns the result.
35
     *
36
     * @return array
37
     */
38 45
    public function getTable($table)
39
    {
40 45
        $this->columns = [];
41
42 45
        $information = $this->pdo->prepare('DESCRIBE ' . $table);
43 45
        $information->execute();
44 45
        $information->setFetchMode(PDO::FETCH_OBJ);
45
46 45
        if ($stripped = strpos($table, '.')) {
47 3
            $table = substr($table, $stripped + 1);
48 3
        }
49
50 45
        while ($row = $information->fetch()) {
51 45
            preg_match('/(.*?)\((.*?)\)/', $row->Type, $match);
52
53 45
            $column = new Column;
54
55 45
            if ($row->Extra == 'auto_increment') {
56 45
                $column->setAutoIncrement(true);
57 45
            }
58
59 45
            if ($row->null == 'YES') {
60
                $column->setNull(true);
61
            }
62
63
            switch ($row->Key) {
64
                case 'PRI':
65
                    $column->setPrimary(true);
66
67
                    break;
68
                
69
                case 'MUL':
70
                    $column->setForeign(true);
71
72
                    break;
73
74
                case 'UNI':
75
                    $column->setUnique(true);
76
77
                    break;
78
            }
79
80
            $column->setDataType($row->Type);
81
            $column->setDefaultValue($row->Default);
82
            $column->setField($row->Field);
83
84
            if (isset($match[1])) {
85
                $column->setDataType($match[1]);
86
                $column->setLength($match[2]);
87
            }
88
89
            $query = 'SELECT COLUMN_NAME as "column",' .
90
                'REFERENCED_COLUMN_NAME as "referenced_column",' .
91
                'CONCAT(' .
92
                    'REFERENCED_TABLE_SCHEMA, ".",' .
93
                    'REFERENCED_TABLE_NAME' .
94
                ') as "referenced_table"' .
95
                'FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE ' .
96
                'WHERE CONSTRAINT_SCHEMA = "' . $this->database . '" ' .
97
                'AND TABLE_NAME = "' . $table . '";';
98
99
            $foreignTable = $this->pdo->prepare($query);
100
            $foreignTable->execute();
101
            $foreignTable->setFetchMode(PDO::FETCH_OBJ);
102
103
            while ($foreignRow = $foreignTable->fetch()) {
104
                if ($foreignRow->column == $row->Field) {
105
                    $column->setReferencedField($foreignRow->referenced_column);
106
                    $column->setReferencedTable($foreignRow->referenced_table);
107
                }
108
            }
109
110
            array_push($this->columns, $column);
111
        }
112
113
        return $this->columns;
114
    }
115
116
    /**
117
     * Shows the list of tables.
118
     *
119
     * @return array
120
     */
121 6
    public function showTables()
122
    {
123 6
        $tables = [];
124
125 6
        $information = $this->pdo->prepare('SHOW TABLES');
126 6
        $information->execute();
127
128 6
        while ($row = $information->fetch()) {
129 6
            array_push($tables, $row[0]);
130 6
        }
131
132 6
        return $tables;
133
    }
134
}
135