Completed
Push — master ( 7c9586...c8ce39 )
by Oscar
01:44
created

Mysql   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 0
loc 48
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A loadTables() 0 4 1
A loadTableFields() 0 38 5
1
<?php
2
declare(strict_types = 1);
3
4
namespace SimpleCrud\Scheme;
5
6
use PDO;
7
8
final class Mysql implements SchemeInterface
9
{
10
    use Traits\CommonsTrait;
11
12
    protected function loadTables(): array
13
    {
14
        return $this->pdo->query('SHOW TABLES')->fetchAll(PDO::FETCH_COLUMN, 0);
15
    }
16
17
    protected function loadTableFields(string $table): array
18
    {
19
        $result = $this->pdo->query("DESCRIBE `{$table}`")->fetchAll(PDO::FETCH_ASSOC);
20
21
        return array_map(
22
            function ($field) {
23
                preg_match('#^(\w+)(\((.+)\))?( unsigned)?#', $field['Type'], $matches);
24
25
                $info = [
26
                    'name' => $field['Field'],
27
                    'type' => $matches[1],
28
                    'null' => ($field['Null'] === 'YES'),
29
                    'default' => $field['Default'],
30
                    'unsigned' => !empty($matches[4]),
31
                    'length' => null,
32
                    'values' => null,
33
                ];
34
35
                switch ($info['type']) {
36
                    case 'enum':
37
                    case 'set':
38
                        $info['values'] = explode(',', $matches[3]);
39
                        break;
40
                    default:
41
                        if (!isset($matches[3])) {
42
                            $info['length'] = null;
43
                        } elseif (strpos($matches[3], ',')) {
44
                            $info['length'] = floatval(str_replace(',', '.', $matches[3]));
45
                        } else {
46
                            $info['length'] = intval($matches[3]);
47
                        }
48
                }
49
50
                return $info;
51
            },
52
            $result
53
        );
54
    }
55
}
56