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 |
||
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 |
|
23 | |||
24 | /** |
||
25 | * (non-PHPdoc) |
||
26 | * |
||
27 | * @see \Nkey\Caribu\Type\IType::getDefaultPort() |
||
28 | */ |
||
29 | 6 | public function getDefaultPort(): int |
|
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) |
|
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 |
|
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 |
|
163 | |||
164 | /** |
||
165 | * (non-PHPdoc) |
||
166 | * |
||
167 | * @see \Nkey\Caribu\Type\AbstractType::mapType() |
||
168 | */ |
||
169 | 4 | protected function mapType(array $result): int |
|
195 | } |
||
196 |