Complex classes like MySQLiByDanielGP 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 MySQLiByDanielGP, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
36 | trait MySQLiByDanielGP |
||
37 | { |
||
38 | |||
39 | use DomComponentsByDanielGP, |
||
40 | MySQLiMultipleExecution, |
||
41 | MySQLiByDanielGPqueries, |
||
42 | MySQLiByDanielGPtypes; |
||
43 | |||
44 | /** |
||
45 | * Intiates connection to MySQL |
||
46 | * |
||
47 | * @param array $mySQLconfig |
||
48 | * |
||
49 | * $mySQLconfig = [ |
||
50 | * 'host' => MYSQL_HOST, |
||
51 | * 'port' => MYSQL_PORT, |
||
52 | * 'username' => MYSQL_USERNAME, |
||
53 | * 'password' => MYSQL_PASSWORD, |
||
54 | * 'database' => MYSQL_DATABASE, |
||
55 | * ]; |
||
56 | */ |
||
57 | protected function connectToMySql($mySQLconfig) |
||
58 | { |
||
59 | if (is_null($this->mySQLconnection)) { |
||
60 | extract($mySQLconfig); |
||
61 | $this->mySQLconnection = new \mysqli($host, $username, $password, $database, $port); |
||
62 | if (is_null($this->mySQLconnection->connect_error)) { |
||
63 | return ''; |
||
64 | } |
||
65 | $this->mySQLconnection = null; |
||
66 | $erNo = $this->mySQLconnection->connect_errno; |
||
67 | $erMsg = $this->mySQLconnection->connect_error; |
||
68 | $msg = $this->lclMsgCmn('i18n_Feedback_ConnectionError'); |
||
69 | return sprintf($msg, $erNo, $erMsg, $host, $port, $username, $database); |
||
70 | } |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * Ensures table has special quoes and DOT as final char |
||
75 | * (if not empty, of course) |
||
76 | * |
||
77 | * @param string $referenceTable |
||
78 | * @return string |
||
79 | */ |
||
80 | private function correctTableWithQuotesAsFieldPrefix($referenceTable) |
||
81 | { |
||
82 | if ($referenceTable != '') { |
||
83 | return '`' . str_replace('`', '', $referenceTable) . '`.'; |
||
84 | } |
||
85 | return ''; |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * returns a list of MySQL databases |
||
90 | * |
||
91 | * @return array |
||
92 | */ |
||
93 | protected function getMySQLactiveDatabases() |
||
94 | { |
||
95 | return $this->getMySQLlistDatabases(true); |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * returns a list of active MySQL engines |
||
100 | * |
||
101 | * @return array |
||
102 | */ |
||
103 | protected function getMySQLactiveEngines() |
||
104 | { |
||
105 | return $this->getMySQLlistEngines(true); |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * returns the list of all MySQL generic informations |
||
110 | * |
||
111 | * @return array |
||
112 | */ |
||
113 | protected function getMySQLgenericInformations() |
||
114 | { |
||
115 | if (is_null($this->mySQLconnection)) { |
||
116 | return []; |
||
117 | } |
||
118 | return ['Info' => $this->mySQLconnection->server_info, 'Version' => $this->mySQLconnection->server_version]; |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * returns the list of all MySQL global variables |
||
123 | * |
||
124 | * @return array |
||
125 | */ |
||
126 | protected function getMySQLglobalVariables() |
||
127 | { |
||
128 | return $this->getMySQLlistMultiple('VariablesGlobal', 'array_key_value'); |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * returns a list of MySQL indexes (w. choice of to choose any combination of db/table/column) |
||
133 | * |
||
134 | * @return array |
||
135 | */ |
||
136 | protected function getMySQLlistColumns($filterArray = null) |
||
137 | { |
||
138 | return $this->getMySQLlistMultiple('Columns', 'full_array_key_numbered', $filterArray); |
||
139 | } |
||
140 | |||
141 | /** |
||
142 | * returns a list of MySQL databases (w. choice of exclude/include the system ones) |
||
143 | * |
||
144 | * @return array |
||
145 | */ |
||
146 | protected function getMySQLlistDatabases($excludeSystemDbs = true) |
||
147 | { |
||
148 | return $this->getMySQLlistMultiple('Databases', 'array_first_key_rest_values', $excludeSystemDbs); |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | * returns a list of MySQL engines (w. choice of return only the active ones) |
||
153 | * |
||
154 | * @return array |
||
155 | */ |
||
156 | protected function getMySQLlistEngines($onlyActiveOnes = true) |
||
157 | { |
||
158 | return $this->getMySQLlistMultiple('Engines', 'array_first_key_rest_values', $onlyActiveOnes); |
||
159 | } |
||
160 | |||
161 | /** |
||
162 | * returns a list of MySQL indexes (w. choice of to choose any combination of db/table/column) |
||
163 | * |
||
164 | * @return array |
||
165 | */ |
||
166 | protected function getMySQLlistIndexes($filterArray = null) |
||
167 | { |
||
168 | return $this->getMySQLlistMultiple('Indexes', 'full_array_key_numbered', $filterArray); |
||
169 | } |
||
170 | |||
171 | /** |
||
172 | * Return various informations (from predefined list) from the MySQL server |
||
173 | * |
||
174 | * @return int|array |
||
175 | */ |
||
176 | private function getMySQLlistMultiple($returnChoice, $returnType, $additionalFeatures = null) |
||
177 | { |
||
178 | if (is_null($this->mySQLconnection)) { |
||
179 | if ($returnType == 'value') { |
||
180 | return null; |
||
181 | } |
||
182 | return []; |
||
183 | } |
||
184 | return $this->getMySQLlistMultipleFinal($returnChoice, $returnType, $additionalFeatures); |
||
185 | } |
||
186 | |||
187 | /** |
||
188 | * Return various informations (from predefined list) from the MySQL server |
||
189 | * |
||
190 | * @return array |
||
191 | */ |
||
192 | private function getMySQLlistMultipleFinal($returnChoice, $returnType, $additionalFeatures = null) |
||
193 | { |
||
194 | $queryByChoice = [ |
||
195 | 'Columns' => $this->sQueryMySqlColumns($additionalFeatures), |
||
196 | 'Databases' => $this->sQueryMySqlActiveDatabases($additionalFeatures), |
||
197 | 'Engines' => $this->sQueryMySqlActiveEngines($additionalFeatures), |
||
198 | 'Indexes' => $this->sQueryMySqlIndexes($additionalFeatures), |
||
199 | 'ServerTime' => $this->sQueryMySqlServerTime(), |
||
200 | 'Statistics' => $this->sQueryMySqlStatistics($additionalFeatures), |
||
201 | 'Tables' => $this->sQueryMySqlTables($additionalFeatures), |
||
202 | 'VariablesGlobal' => $this->sQueryMySqlGlobalVariables(), |
||
203 | ]; |
||
204 | if (array_key_exists($returnChoice, $queryByChoice)) { |
||
205 | return $this->setMySQLquery2Server($queryByChoice[$returnChoice], $returnType)['result']; |
||
206 | } |
||
207 | return []; |
||
208 | } |
||
209 | |||
210 | /** |
||
211 | * Return the list of Tables from the MySQL server |
||
212 | * |
||
213 | * @return string |
||
214 | */ |
||
215 | protected function getMySQLStatistics($filterArray = null) |
||
216 | { |
||
217 | return $this->getMySQLlistMultiple('Statistics', 'full_array_key_numbered', $filterArray); |
||
218 | } |
||
219 | |||
220 | /** |
||
221 | * Return the list of Tables from the MySQL server |
||
222 | * |
||
223 | * @return string |
||
224 | */ |
||
225 | protected function getMySQLlistTables($filterArray = null) |
||
229 | |||
230 | /** |
||
231 | * Provides a detection if given Query does contain a Parameter |
||
232 | * that may require statement processing later on |
||
233 | * |
||
234 | * @param string $sQuery |
||
235 | * @param string $paramIdentifier |
||
236 | * @return boolean |
||
237 | */ |
||
238 | protected function getMySQLqueryWithParameterIdentifier($sQuery, $paramIdentifier) |
||
239 | { |
||
240 | $sReturn = true; |
||
241 | if (strpos($sQuery, $paramIdentifier) === false) { |
||
242 | $sReturn = false; |
||
246 | |||
247 | /** |
||
248 | * Return the time from the MySQL server |
||
249 | * |
||
250 | * @return string |
||
251 | */ |
||
252 | protected function getMySQLserverTime() |
||
256 | |||
257 | /** |
||
258 | * Reads data from table into REQUEST super global |
||
259 | * |
||
260 | * @param string $tableName |
||
261 | * @param array $filtersArray |
||
262 | */ |
||
263 | protected function getRowDataFromTable($tableName, $filtersArray) |
||
275 | |||
276 | /** |
||
277 | * Builds an filter string from pair of key and value, where value is array |
||
278 | * |
||
279 | * @param string $key |
||
280 | * @param array $value |
||
281 | * @param string $referenceTable |
||
282 | * @return string |
||
283 | */ |
||
284 | private function setArrayLineArrayToFilter($key, $value, $referenceTable) |
||
293 | |||
294 | /** |
||
295 | * Builds an filter string from pair of key and value, none array |
||
296 | * |
||
297 | * @param string $key |
||
298 | * @param int|float|string $value |
||
299 | * @return string |
||
300 | */ |
||
301 | private function setArrayLineToFilter($key, $value) |
||
309 | |||
310 | /** |
||
311 | * Transforms an array into usable filters |
||
312 | * |
||
313 | * @param array $entryArray |
||
314 | * @param string $referenceTable |
||
315 | * @return array |
||
316 | */ |
||
317 | private function setArrayToFilterValues($entryArray, $referenceTable = '') |
||
330 | |||
331 | /** |
||
332 | * Returns maximum length for a given MySQL field |
||
333 | * |
||
334 | * @param array $fieldDetails |
||
335 | * @param boolean $outputFormated |
||
336 | * @return array |
||
337 | */ |
||
338 | protected function setFieldNumbers($fieldDetails, $outputFormated = false) |
||
350 | |||
351 | /** |
||
352 | * Establishes numbers of fields |
||
353 | * |
||
354 | * @param array $fieldDetails |
||
355 | * @return array |
||
356 | */ |
||
357 | private function setFieldSpecific($fieldDetails) |
||
368 | |||
369 | private function setFieldSpecificElse($fieldDetails) |
||
377 | |||
378 | private function setFldLmts($colType, $loLmt, $upLmt, $szN, $szUS) |
||
386 | |||
387 | private function setFldLmtsExact($cTp) |
||
402 | |||
403 | /** |
||
404 | * Transmit Query to MySQL server and get results back |
||
405 | * |
||
406 | * @param string $sQuery |
||
407 | * @param string $sReturnType |
||
408 | * @param array $ftrs |
||
409 | * @return boolean|array|string |
||
410 | */ |
||
411 | protected function setMySQLquery2Server($sQuery, $sReturnType = null, $ftrs = null) |
||
427 | |||
428 | /** |
||
429 | * Turns a raw query result into various structures |
||
430 | * based on different predefined $parameters['returnType'] value |
||
431 | * |
||
432 | * @param array $parameters |
||
433 | * @return array as ['customError' => '...', 'result' => '...'] |
||
434 | */ |
||
435 | private function setMySQLquery2ServerByPattern($parameters) |
||
497 | |||
498 | private function setMySQLquery2ServerByPatternKey($parameters) |
||
517 | |||
518 | protected function setMySQLquery2ServerConnected($inArray) |
||
537 | |||
538 | private function setMySQLqueryValidateInputs($prm) |
||
556 | |||
557 | private function setMySQLqueryValidationMap() |
||
573 | } |
||
574 |