Total Complexity | 51 |
Total Lines | 296 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Complex classes like Driver 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.
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 Driver, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | abstract class Driver implements DriverInterface |
||
24 | { |
||
25 | use ErrorTrait; |
||
26 | use ConfigTrait; |
||
|
|||
27 | use ConnectionTrait; |
||
28 | use ServerTrait; |
||
29 | use TableTrait; |
||
30 | use DatabaseTrait; |
||
31 | use QueryTrait; |
||
32 | use GrammarTrait; |
||
33 | |||
34 | /** |
||
35 | * @var Utils |
||
36 | */ |
||
37 | protected $utils; |
||
38 | |||
39 | /** |
||
40 | * The constructor |
||
41 | * |
||
42 | * @param Utils $utils |
||
43 | * @param array $options |
||
44 | */ |
||
45 | public function __construct(Utils $utils, array $options) |
||
46 | { |
||
47 | $this->utils = $utils; |
||
48 | $this->config = new ConfigEntity($utils->trans, $options); |
||
49 | $this->beforeConnection(); |
||
50 | // Create and set the main connection. |
||
51 | $this->mainConnection = $this->createConnection(); |
||
52 | } |
||
53 | |||
54 | /** |
||
55 | * @inheritDoc |
||
56 | * @throws AuthException |
||
57 | */ |
||
58 | public function open(string $database, string $schema = '') |
||
59 | { |
||
60 | if (!$this->connection->open($database, $schema)) { |
||
61 | throw new AuthException($this->error()); |
||
62 | } |
||
63 | $this->config->database = $database; |
||
64 | $this->config->schema = $schema; |
||
65 | |||
66 | $this->afterConnection(); |
||
67 | return $this->connection; |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * @inheritDoc |
||
72 | * @throws AuthException |
||
73 | */ |
||
74 | public function connect(string $database, string $schema = '') |
||
75 | { |
||
76 | $this->createConnection(); |
||
77 | return $this->open($database, $schema); |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * @inheritDoc |
||
82 | */ |
||
83 | public function minVersion(string $version, string $mariaDb = '') |
||
84 | { |
||
85 | $info = $this->connection->serverInfo(); |
||
86 | if ($mariaDb && preg_match('~([\d.]+)-MariaDB~', $info, $match)) { |
||
87 | $info = $match[1]; |
||
88 | $version = $mariaDb; |
||
89 | } |
||
90 | return (version_compare($info, $version) >= 0); |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * @inheritDoc |
||
95 | */ |
||
96 | public function support(string $feature) |
||
97 | { |
||
98 | return in_array($feature, $this->config->features); |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * @inheritDoc |
||
103 | */ |
||
104 | public function charset() |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * @inheritDoc |
||
112 | */ |
||
113 | public function setUtf8mb4(string $create) |
||
114 | { |
||
115 | static $set = false; |
||
116 | // possible false positive |
||
117 | if (!$set && preg_match('~\butf8mb4~i', $create)) { |
||
118 | $set = true; |
||
119 | return 'SET NAMES ' . $this->charset() . ";\n\n"; |
||
120 | } |
||
121 | return ''; |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * @inheritDoc |
||
126 | */ |
||
127 | public function execute(string $query) |
||
128 | { |
||
129 | $this->utils->history->save($query); |
||
130 | return $this->connection->query($query); |
||
131 | } |
||
132 | |||
133 | /** |
||
134 | * @inheritDoc |
||
135 | */ |
||
136 | public function queries() |
||
137 | { |
||
138 | return $this->utils->history->queries(); |
||
139 | } |
||
140 | |||
141 | /** |
||
142 | * @inheritDoc |
||
143 | */ |
||
144 | public function applyQueries(string $query, array $tables, $escape = null) |
||
145 | { |
||
146 | if (!$escape) { |
||
147 | $escape = function ($table) { |
||
148 | return $this->escapeTableName($table); |
||
149 | }; |
||
150 | } |
||
151 | foreach ($tables as $table) { |
||
152 | if (!$this->execute("$query " . $escape($table))) { |
||
153 | return false; |
||
154 | } |
||
155 | } |
||
156 | return true; |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * @inheritDoc |
||
161 | */ |
||
162 | public function values(string $query, int $column = 0) |
||
163 | { |
||
164 | $statement = $this->execute($query); |
||
165 | if (!is_object($statement)) { |
||
166 | return []; |
||
167 | } |
||
168 | $values = []; |
||
169 | while ($row = $statement->fetchRow()) { |
||
170 | $values[] = $row[$column]; |
||
171 | } |
||
172 | return $values; |
||
173 | } |
||
174 | |||
175 | /** |
||
176 | * @inheritDoc |
||
177 | */ |
||
178 | public function colValues(string $query, string $column) |
||
179 | { |
||
180 | $statement = $this->execute($query); |
||
181 | if (!is_object($statement)) { |
||
182 | return []; |
||
183 | } |
||
184 | $values = []; |
||
185 | while ($row = $statement->fetchAssoc()) { |
||
186 | $values[] = $row[$column]; |
||
187 | } |
||
188 | return $values; |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * @inheritDoc |
||
193 | */ |
||
194 | public function keyValues(string $query, int $keyColumn = 0, int $valueColumn = 1) |
||
195 | { |
||
196 | $statement = $this->execute($query); |
||
197 | if (!is_object($statement)) { |
||
198 | return []; |
||
199 | } |
||
200 | $values = []; |
||
201 | while ($row = $statement->fetchRow()) { |
||
202 | $values[$row[$keyColumn]] = $row[$valueColumn]; |
||
203 | } |
||
204 | return $values; |
||
205 | } |
||
206 | |||
207 | /** |
||
208 | * @inheritDoc |
||
209 | */ |
||
210 | public function rows(string $query) |
||
221 | } |
||
222 | |||
223 | /** |
||
224 | * Escape column key used in where() |
||
225 | * |
||
226 | * @param string |
||
227 | * |
||
228 | * @return string |
||
229 | */ |
||
230 | public function escapeKey(string $key): string |
||
231 | { |
||
232 | if (preg_match('(^([\w(]+)(' . str_replace('_', '.*', |
||
233 | preg_quote($this->escapeId('_'))) . ')([ \w)]+)$)', $key, $match)) { |
||
234 | //! columns looking like functions |
||
235 | return $match[1] . $this->escapeId($this->unescapeId($match[2])) . $match[3]; //! SQL injection |
||
236 | } |
||
237 | return $this->escapeId($key); |
||
238 | } |
||
239 | |||
240 | /** |
||
241 | * Escape or unescape string to use inside form [] |
||
242 | * |
||
243 | * @param string $idf |
||
244 | * @param bool $back |
||
245 | * |
||
246 | * @return string |
||
247 | */ |
||
248 | public function bracketEscape(string $idf, bool $back = false): string |
||
249 | { |
||
250 | // escape brackets inside name='x[]' |
||
251 | static $trans = [':' => ':1', ']' => ':2', '[' => ':3', '"' => ':4']; |
||
252 | return strtr($idf, ($back ? array_flip($trans) : $trans)); |
||
253 | } |
||
254 | |||
255 | /** |
||
256 | * Filter length value including enums |
||
257 | * |
||
258 | * @param string $length |
||
259 | * |
||
260 | * @return string |
||
261 | */ |
||
262 | public function processLength(string $length): string |
||
263 | { |
||
264 | if (!$length) { |
||
265 | return ''; |
||
266 | } |
||
267 | $enumLength = $this->enumLength(); |
||
268 | if (preg_match("~^\\s*\\(?\\s*$enumLength(?:\\s*,\\s*$enumLength)*+\\s*\\)?\\s*\$~", $length) && |
||
269 | preg_match_all("~$enumLength~", $length, $matches)) { |
||
270 | return '(' . implode(',', $matches[0]) . ')'; |
||
271 | } |
||
272 | return preg_replace('~^[0-9].*~', '(\0)', preg_replace('~[^-0-9,+()[\]]~', '', $length)); |
||
273 | } |
||
274 | |||
275 | /** |
||
276 | * Create SQL string from field type |
||
277 | * |
||
278 | * @param TableFieldEntity $field |
||
279 | * @param string $collate |
||
280 | * |
||
281 | * @return string |
||
282 | */ |
||
283 | private function processType(TableFieldEntity $field, string $collate = 'COLLATE'): string |
||
295 | } |
||
296 | |||
297 | /** |
||
298 | * Create SQL string from field |
||
299 | * |
||
300 | * @param TableFieldEntity $field Basic field information |
||
301 | * @param TableFieldEntity $typeField Information about field type |
||
302 | * |
||
303 | * @return array |
||
304 | */ |
||
305 | public function processField(TableFieldEntity $field, TableFieldEntity $typeField): array |
||
306 | { |
||
319 | } |
||
320 | } |
||
321 |