Complex classes like SQLBuilder 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 SQLBuilder, and based on these observations, apply Extract Interface, too.
1 | <?php declare(strict_types = 1); |
||
13 | class SQLBuilder |
||
14 | { |
||
15 | const NUMERIC_COLUMNS_TYPES = ['int', 'float', 'longint', 'smallint', 'tinyint']; |
||
16 | const DATE_COLUMNS_TYPES = ['date', 'datetime', 'timestamp']; |
||
17 | |||
18 | /** |
||
19 | * Build update statement. |
||
20 | * |
||
21 | * @param TableMetadata $tableMetadata Table metadata |
||
22 | * @param array $columnValues Collection of columnName => columnValue |
||
23 | * @param Condition|null $condition Update filtering condition |
||
24 | * |
||
25 | * @return string Update SQL statement |
||
26 | * @throws \InvalidArgumentException |
||
27 | */ |
||
28 | public function buildUpdateStatement(TableMetadata $tableMetadata, array $columnValues, Condition $condition = null) : string |
||
62 | |||
63 | /** |
||
64 | * Build full table column name. |
||
65 | * |
||
66 | * @param string $tableName Table name |
||
67 | * @param string $columnName Field name |
||
68 | * |
||
69 | * @return string Full table column name |
||
70 | */ |
||
71 | protected function buildFullColumnName(string $tableName, string $columnName) : string |
||
75 | |||
76 | /** |
||
77 | * Build argument value statement. |
||
78 | * |
||
79 | * @param string|array $value Argument column value |
||
80 | * @param string $columnType Argument column type |
||
81 | * @param string $relation Argument relation |
||
82 | * @param bool $nullable |
||
83 | * @param string $defaultValue |
||
84 | * |
||
85 | * @return string Argument relation with value statement |
||
86 | */ |
||
87 | protected function buildArgumentValue( |
||
99 | |||
100 | /** |
||
101 | * Build array argument value statement. |
||
102 | * |
||
103 | * @param string $columnType Table column type |
||
104 | * @param array $value Table column array value |
||
105 | * @param string $relation Table column relation to value |
||
106 | * |
||
107 | * @return string Array argument relation with value statement |
||
108 | */ |
||
109 | protected function buildArrayValue(string $columnType, array $value, string $relation = 'IN') : string |
||
117 | |||
118 | /** |
||
119 | * Define if table column type is numeric. |
||
120 | * |
||
121 | * @param string $columnType Table column type |
||
122 | * |
||
123 | * @return bool True if column type is numeric |
||
124 | */ |
||
125 | protected function isColumnNumeric(string $columnType) : bool |
||
129 | |||
130 | /** |
||
131 | * Build array with numeric values statement. |
||
132 | * |
||
133 | * @param array $value Array with numeric values |
||
134 | * @param string $relation Table column relation to value |
||
135 | * |
||
136 | * @return string Array with numeric values statement |
||
137 | */ |
||
138 | protected function buildNumericArrayValue(array $value, string $relation) : string |
||
142 | |||
143 | /** |
||
144 | * Build array string value statement. |
||
145 | * |
||
146 | * @param array $value Array with string values |
||
147 | * @param string $relation Table column relation to value |
||
148 | * |
||
149 | * @return string Array with string values statement |
||
150 | */ |
||
151 | protected function buildStringArrayValue(array $value, string $relation) : string |
||
155 | |||
156 | /** |
||
157 | * Build not array argument value statement. |
||
158 | * |
||
159 | * @param string $columnType Table column type |
||
160 | * @param mixed $value Table column value |
||
161 | * @param string $relation Table column relation to value |
||
162 | * |
||
163 | * @return string Not array argument relation with value statement |
||
164 | */ |
||
165 | protected function buildValue( |
||
192 | |||
193 | /** |
||
194 | * Build not array numeric value statement. |
||
195 | * |
||
196 | * @param mixed $value Numeric value |
||
197 | * |
||
198 | * @return string Not array numeric value statement |
||
199 | */ |
||
200 | protected function buildNumericValue($value) : string |
||
204 | |||
205 | /** |
||
206 | * Define if table column type is date. |
||
207 | * |
||
208 | * @param string $columnType Table column type |
||
209 | * |
||
210 | * @return bool True if column type is date |
||
211 | */ |
||
212 | protected function isColumnDate(string $columnType) : bool |
||
216 | |||
217 | /** |
||
218 | * Build not array date value statement. |
||
219 | * |
||
220 | * @param mixed $value Date value |
||
221 | * |
||
222 | * @return string Not array date value statement |
||
223 | */ |
||
224 | protected function buildDateValue($value) : string |
||
228 | |||
229 | /** |
||
230 | * Build not array string value statement. |
||
231 | * |
||
232 | * @param string $value String value |
||
233 | * |
||
234 | * @return string Not array string value statement |
||
235 | */ |
||
236 | protected function buildStringValue(string $value) : string |
||
240 | |||
241 | /** |
||
242 | * Build not array NULL value statement. |
||
243 | * |
||
244 | * @return string Not array string value statement |
||
245 | */ |
||
246 | protected function buildNullValue() : string |
||
250 | |||
251 | /** |
||
252 | * Build where statement. |
||
253 | * |
||
254 | * @param TableMetadata $metadata |
||
255 | * @param Condition $condition |
||
256 | * |
||
257 | * @return string Limitation statement |
||
258 | * @throws \InvalidArgumentException |
||
259 | * |
||
260 | */ |
||
261 | public function buildWhereStatement(TableMetadata $metadata, Condition $condition) : string |
||
282 | |||
283 | /** |
||
284 | * Build argument condition. |
||
285 | * |
||
286 | * @param Argument $argument Condition argument |
||
287 | * @param TableMetadata $metadata Table metadata |
||
288 | * |
||
289 | * @return string Argument condition statement |
||
290 | * @throws \InvalidArgumentException If argument column does not exist |
||
291 | */ |
||
292 | protected function buildArgumentCondition(Argument $argument, TableMetadata $metadata) |
||
313 | |||
314 | /** |
||
315 | * Build own condition statement. |
||
316 | * |
||
317 | * @param string $ownCondition Condition statement |
||
318 | * |
||
319 | * @return string Own condition statement |
||
320 | */ |
||
321 | protected function buildOwnCondition(string $ownCondition) : string |
||
325 | |||
326 | /** |
||
327 | * Build generic condition statement. |
||
328 | * |
||
329 | * @param string $columnName Table column name |
||
330 | * @param string $relation Table column value relation |
||
331 | * @param string $value Table column value |
||
332 | * |
||
333 | * @return string Generic condition statement |
||
334 | */ |
||
335 | protected function buildCondition(string $columnName, string $relation = '', string $value = '') : string |
||
339 | |||
340 | /** |
||
341 | * Build is null/not null condition statement. |
||
342 | * |
||
343 | * @param string $columnName Table column name |
||
344 | * @param string $nullRelation Table column null relation |
||
345 | * |
||
346 | * @return string Is null/not null condition statement |
||
347 | */ |
||
348 | protected function buildNullCondition(string $columnName, string $nullRelation) : string |
||
352 | |||
353 | /** |
||
354 | * Build insert statement. |
||
355 | * |
||
356 | * @param TableMetadata $tableMetadata Table metadata |
||
357 | * @param array $columnValues Collection of columnName => columnValue |
||
358 | * |
||
359 | * @return string Insert SQL statement |
||
360 | * @throws \InvalidArgumentException |
||
361 | */ |
||
362 | public function buildInsertStatement(TableMetadata $tableMetadata, array $columnValues) : string |
||
382 | |||
383 | /** |
||
384 | * Build selected fields SELECT statement part. |
||
385 | * |
||
386 | * @param array $tableColumns Tables and column names collection |
||
387 | * |
||
388 | * @return string SELECT statement |
||
389 | */ |
||
390 | public function buildSelectStatement(array $tableColumns) : string |
||
394 | |||
395 | /** |
||
396 | * Build full table column names collection. |
||
397 | * |
||
398 | * @param array $tableColumns Tables and column names collection |
||
399 | * |
||
400 | * @return array Collection of full column names for query |
||
401 | */ |
||
402 | protected function buildFullColumnNames(array $tableColumns) : array |
||
414 | |||
415 | /** |
||
416 | * Build FROM statement part. |
||
417 | * |
||
418 | * @param array $tableNames Tables and column names collection |
||
419 | * |
||
420 | * @return string FROM statement |
||
421 | */ |
||
422 | public function buildFromStatement(array $tableNames = []) : string |
||
426 | |||
427 | /** |
||
428 | * Build grouping statement. |
||
429 | * |
||
430 | * @param array $tableColumns Tables and column names collection |
||
431 | * |
||
432 | * @return string Grouping statement |
||
433 | */ |
||
434 | public function buildGroupStatement(array $tableColumns) : string |
||
438 | |||
439 | /** |
||
440 | * Build ordering statement. |
||
441 | * |
||
442 | * @param array $tableColumns Tables and column names collection |
||
443 | * @param array $orders Collection of columns sorting order |
||
444 | * |
||
445 | * @return string Ordering statement |
||
446 | * @throws \InvalidArgumentException |
||
447 | */ |
||
448 | public function buildOrderStatement(array $tableColumns, array $orders) : string |
||
458 | |||
459 | /** |
||
460 | * Build limitation statement. |
||
461 | * |
||
462 | * @param int $rows Rows amount for limitation |
||
463 | * @param int $offset Rows offset |
||
464 | * |
||
465 | * @return string Limitation statement |
||
466 | */ |
||
467 | public function buildLimitStatement(int $rows, int $offset = 0) : string |
||
471 | } |
||
472 |