MySQL   B
last analyzed

Complexity

Total Complexity 45

Size/Duplication

Total Lines 185
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 93.48%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 45
lcom 1
cbo 1
dl 0
loc 185
ccs 43
cts 46
cp 0.9348
rs 8.3673
c 2
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A getDsn() 0 4 1
A getDefaultPort() 0 4 1
A lock() 0 9 2
C isDecimalType() 0 4 7
A getPrimaryKeyColumn() 0 7 1
A unlock() 0 4 1
A getEscapeSign() 0 4 1
A getSequenceNameForColumn() 0 4 1
A getTypeQuery() 0 6 1
C isStringType() 0 4 8
B isBinaryType() 0 4 6
B isIntegerType() 0 4 6
A isDateTimeType() 0 4 3
B mapType() 0 26 6

How to fix   Complexity   

Complex Class

Complex classes like MySQL often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use MySQL, and based on these observations, apply Extract Interface, too.

1
<?php
2
namespace Nkey\Caribu\Type;
3
4
/**
5
 * Concrete mysql implementation of database type
6
 *
7
 * This class is part of Caribu package
8
 *
9
 * @author Maik Greubel <[email protected]>
10
 */
11
class MySQL extends AbstractType
12
{
13
14
    /**
15
     * (non-PHPdoc)
16
     *
17
     * @see \Nkey\Caribu\Type\IType::getDsn()
18
     */
19 6
    public function getDsn(): string
20
    {
21 6
        return "mysql:host={host};port={port};dbname={schema}";
22
    }
23
24
    /**
25
     * (non-PHPdoc)
26
     *
27
     * @see \Nkey\Caribu\Type\IType::getDefaultPort()
28
     */
29 6
    public function getDefaultPort(): int
30
    {
31 6
        return 3306;
32
    }
33
34
    /**
35
     * (non-PHPdoc)
36
     *
37
     * @see \Nkey\Caribu\Type\IType::getPrimaryKeyColumn()
38
     */
39 1
    public function getPrimaryKeyColumn(string $table, \Nkey\Caribu\Orm\Orm $orm): string
40
    {
41
        $query = "SELECT `COLUMN_NAME` as column_name FROM `information_schema`.`columns` " . //
42 1
"WHERE `TABLE_NAME` = '{table}' AND `TABLE_SCHEMA` = '{schema}' AND `COLUMN_KEY` = 'PRI'";
43
        
44 1
        return $this->getPrimaryColumnViaSql($orm, $table, $query);
45
    }
46
47
    /**
48
     * (non-PHPdoc)
49
     *
50
     * @see \Nkey\Caribu\Type\IType::lock()
51
     */
52 5
    public function lock(string $table, int $lockType, \Nkey\Caribu\Orm\Orm $orm)
53
    {
54 5
        $lock = "READ";
55 5
        if ($lockType == IType::LOCK_TYPE_WRITE) {
56 5
            $lock = "WRITE";
57
        }
58
        
59 5
        $this->changeLockViaSql($orm, $table, sprintf("LOCK TABLES `%s` %s", $table, $lock));
60 5
    }
61
62
    /**
63
     * (non-PHPdoc)
64
     *
65
     * @see \Nkey\Caribu\Type\IType::unlock()
66
     */
67 5
    public function unlock(string $table, \Nkey\Caribu\Orm\Orm $orm)
68
    {
69 5
        $this->changeLockViaSql($orm, $table, "UNLOCK TABLES");
70 5
    }
71
72
    /**
73
     * (non-PHPdoc)
74
     *
75
     * @see \Nkey\Caribu\Type\IType::getEscapeSign()
76
     */
77 6
    public function getEscapeSign(): string
78
    {
79 6
        return "`";
80
    }
81
82
    /**
83
     * (non-PHPdoc)
84
     *
85
     * @see \Nkey\Caribu\Type\IType::getSequenceNameForColumn()
86
     */
87 4
    public function getSequenceNameForColumn(string $table, string $columnName, \Nkey\Caribu\Orm\Orm $orm): string
88
    {
89 4
        return "";
90
    }
91
92
    /**
93
     * (non-PHPdoc)
94
     *
95
     * @see \Nkey\Caribu\Type\AbstractType::getTypeQuery()
96
     */
97 4
    protected function getTypeQuery(): string
98
    {
99 4
        $query = "SELECT `DATA_TYPE` FROM `INFORMATION_SCHEMA`.`COLUMNS` WHERE `TABLE_SCHEMA` = '{schema}' " . "AND `TABLE_NAME` = '{table}' AND `COLUMN_NAME` = '{column}'";
100
        
101 4
        return $query;
102
    }
103
104
    /**
105
     * Checks whether given type is a string type
106
     *
107
     * @param string $type            
108
     *
109
     * @return bool true in case of type is string type, false otherwise
110
     */
111 4
    private function isStringType(string $type): bool
112
    {
113 4
        return $type === 'CHAR' || $type === 'VARCHAR' || $type === 'TEXT' || $type === 'TINYTEXT' || $type === 'MEDIUMTEXT' || $type === 'LONGTEXT' || $type === 'ENUM' || $type === 'SET';
114
    }
115
116
    /**
117
     * Checks whether given type is a binary type
118
     *
119
     * @param string $type            
120
     *
121
     * @return bool true in case of type is binary type, false otherwise
122
     */
123 3
    private function isBinaryType(string $type): bool
124
    {
125 3
        return $type === 'BINARY' || $type === 'VABINARY' || $type === 'TINYBLOB' || $type === 'BLOB' || $type === 'MEDIUMBLOB' || $type === 'LONGBLOB';
126
    }
127
128
    /**
129
     * Checks whether given type is a integer type
130
     *
131
     * @param string $type            
132
     *
133
     * @return bool true in case of type is integer type, false otherwise
134
     */
135 3
    private function isIntegerType(string $type): bool
136
    {
137 3
        return $type === 'INTEGER' || $type === 'INT' || $type === 'SMALLINT' || $type === 'TINYINT' || $type === 'MEDIUMINT' || $type === 'BIGINT';
138
    }
139
140
    /**
141
     * Checks whether given type is a decimal type
142
     *
143
     * @param string $type            
144
     *
145
     * @return bool true in case of type is decimal type, false otherwise
146
     */
147 1
    private function isDecimalType(string $type): bool
148
    {
149 1
        return $type === 'DECIMAL' || $type === 'NUMERIC' || $type === 'FLOAT' || $type === 'REAL' || $type === 'FIXED' || $type === 'DEC' || $type === 'DOUBLE PRECISION';
150
    }
151
152
    /**
153
     * Checks whether given type is a datetime type
154
     *
155
     * @param string $type            
156
     *
157
     * @return bool true in case of type is datetime type, false otherwise
158
     */
159 1
    private function isDateTimeType(string $type): bool
160
    {
161 1
        return $type === 'DATE' || $type === 'DATETIME' || $type === 'TIMESTAMP';
162
    }
163
164
    /**
165
     * (non-PHPdoc)
166
     *
167
     * @see \Nkey\Caribu\Type\AbstractType::mapType()
168
     */
169 4
    protected function mapType(array $result): int
170
    {
171 4
        $type = strtoupper($result['DATA_TYPE']);
172
        
173 4
        if ($this->isStringType($type)) {
174 4
            return \Nkey\Caribu\Orm\OrmDataType::STRING;
175
        }
176
        
177 3
        if ($this->isBinaryType($type)) {
178
            return \Nkey\Caribu\Orm\OrmDataType::BLOB;
179
        }
180
        
181 3
        if ($this->isIntegerType($type)) {
182 3
            return \Nkey\Caribu\Orm\OrmDataType::INTEGER;
183
        }
184
        
185 1
        if ($this->isDecimalType($type)) {
186
            return \Nkey\Caribu\Orm\OrmDataType::DECIMAL;
187
        }
188
        
189 1
        if ($this->isDateTimeType($type)) {
190 1
            return \Nkey\Caribu\Orm\OrmDataType::DATETIME;
191
        }
192
        
193
        return \Nkey\Caribu\Orm\OrmDataType::STRING;
194
    }
195
}
196