Completed
Pull Request — master (#18)
by Michal
03:39
created

MySqlHeaderManager::isNumeric()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 19
ccs 0
cts 12
cp 0
rs 8.8571
cc 6
eloc 12
nc 6
nop 1
crap 42
1
<?php
2
3
namespace Adminerng\Drivers\MySql;
4
5
use Adminerng\Core\Column;
6
use Adminerng\Core\ListingHeaders\HeaderManagerInterface;
7
8
class MySqlHeaderManager implements HeaderManagerInterface
9
{
10
    private $dataManager;
11
12
    public function __construct(MySqlDataManager $dataManager)
13
    {
14
        $this->dataManager = $dataManager;
15
    }
16
17
    public function databasesHeaders()
18
    {
19
        $columns = [];
20
        $columns[] = (new Column('database', 'mysql.headers.databases.database'))
21
            ->setSortable(true);
22
23
        $columns[] = (new Column('charset', 'mysql.headers.databases.charset'))
24
            ->setSortable(true);
25
26
        $columns[] = (new Column('collation', 'mysql.headers.databases.collation'))
27
            ->setSortable(true);
28
29
        $columns[] = (new Column('tables_count', 'mysql.headers.databases.tables'))
30
            ->setSortable(true)
31
            ->setNumeric(true);
32
33
        $columns[] = (new Column('size', 'mysql.headers.databases.size'))
34
            ->setSortable(true)
35
            ->setNumeric(true)
36
            ->setSize(true);
37
        return $columns;
38
    }
39
40
    public function tablesHeaders()
41
    {
42
        $tableFields = [
43
            'table' => [],
44
            'engine' => [],
45
            'collation' => [],
46
            'data_length' => ['is_numeric' => true, 'is_size' => true],
47
            'index_length' => ['is_numeric' => true, 'is_size' => true],
48
            'data_free' => ['is_numeric' => true, 'is_size' => true],
49
            'autoincrement' => ['is_numeric' => true],
50
            'rows' => ['is_numeric' => true],
51
        ];
52
        $tableColumns = [];
53
        foreach ($tableFields as $key => $settings) {
54
            $column = (new Column($key, 'mysql.headers.tables.' . $key))
55
                ->setSortable(true);
56
            if (isset($settings['is_numeric'])) {
57
                $column->setNumeric($settings['is_numeric']);
58
            }
59
            if (isset($settings['is_size'])) {
60
                $column->setSize($settings['is_size']);
61
            }
62
            $tableColumns[] = $column;
63
        }
64
65
        $viewFields = [
66
            'view' => [],
67
            'check_option' => [],
68
            'is_updatable' => [],
69
            'definer' => [],
70
            'security_type' => [],
71
            'character_set' => [],
72
            'collation' => [],
73
        ];
74
        $viewColumns = [];
75
        foreach ($viewFields as $key => $settings) {
76
            $column = (new Column($key, 'mysql.headers.views.' . $key))
77
                ->setSortable(true);
78
            if (isset($settings['is_numeric'])) {
79
                $column->setNumeric($settings['is_numeric']);
80
            }
81
            $viewColumns[] = $column;
82
        }
83
84
        return [
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array(\Adminerng\..._VIEW => $viewColumns); (array<*,array>) is incompatible with the return type declared by the interface Adminerng\Core\ListingHe...nterface::tablesHeaders of type Adminerng\Core\Column[].

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
85
            MySqlDriver::TYPE_TABLE => $tableColumns,
86
            MySqlDriver::TYPE_VIEW => $viewColumns,
87
        ];
88
    }
89
90
    public function itemsHeaders($type, $table)
91
    {
92
        $columns = [];
93
        foreach ($this->dataManager->getColumns($type, $table) as $column => $definition) {
94
            $col = (new Column($column, $column))
95
                ->setSortable(true)
96
                ->setFilterable(true)
97
                ->setNumeric($this->isNumeric($definition))
98
                ->setDecimals($this->getDecimals($definition))
99
                ->setInfo($definition['Comment']);
100
            if ($definition['key_info'] && $definition['key_info']['REFERENCED_TABLE_NAME']) {
101
                $col->setExternal(
102
                    $definition['key_info']['REFERENCED_TABLE_SCHEMA'],
103
                    $definition['key_info']['REFERENCED_TABLE_NAME'],
104
                    function ($value) {
105
                        return md5($value);
106
                    }
107
                );
108
            }
109
            $columns[] = $col;
110
        }
111
        return $columns;
112
    }
113
114
    private function isNumeric(array $definition)
115
    {
116
        if ($definition['key_info']) {
117
            return false;
118
        }
119
        if (strpos($definition['Type'], 'int') !== false) {
120
            return true;
121
        }
122
        if (strpos($definition['Type'], 'float') !== false) {
123
            return true;
124
        }
125
        if (strpos($definition['Type'], 'double') !== false) {
126
            return true;
127
        }
128
        if (strpos($definition['Type'], 'decimal') !== false) {
129
            return true;
130
        }
131
        return false;
132
    }
133
134
    private function getDecimals(array $column)
135
    {
136
        if (!$this->isNumeric($column)) {
137
            return 0;
138
        }
139
        if (strpos($column['Type'], ',') === false) {
140
            return 0;
141
        }
142
        $pattern = '/(.*?)\((.*?),(.*?)\)/';
143
        preg_match($pattern, $column['Type'], $match);
144
        return isset($match[3]) ? $match[3] : 0;
145
    }
146
}
147