Total Complexity | 54 |
Total Lines | 348 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like RowTrait 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 RowTrait, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | trait RowTrait |
||
13 | { |
||
14 | /** |
||
15 | * Returns a recordset of all columns in a table. |
||
16 | * |
||
17 | * @param string $table The name of a table |
||
18 | * @param array $key The associative array holding the key to retrieve |
||
19 | * |
||
20 | * @return \PHPPgAdmin\ADORecordSet A recordset |
||
21 | */ |
||
22 | public function browseRow($table, $key) |
||
23 | { |
||
24 | $f_schema = $this->_schema; |
||
25 | $this->fieldClean($f_schema); |
||
26 | $this->fieldClean($table); |
||
27 | |||
28 | $sql = "SELECT * FROM \"{$f_schema}\".\"{$table}\""; |
||
29 | if (is_array($key) && sizeof($key) > 0) { |
||
30 | $sql .= ' WHERE true'; |
||
31 | foreach ($key as $k => $v) { |
||
32 | $this->fieldClean($k); |
||
33 | $this->clean($v); |
||
34 | $sql .= " AND \"{$k}\"='{$v}'"; |
||
35 | } |
||
36 | } |
||
37 | |||
38 | return $this->selectSet($sql); |
||
39 | } |
||
40 | |||
41 | /** |
||
42 | * Get the fields for uniquely identifying a row in a table. |
||
43 | * |
||
44 | * @param string $table The table for which to retrieve the identifier |
||
45 | * |
||
46 | * @return array|array<integer,string>|int An array mapping attribute number to attribute name, empty for no identifiers |
||
47 | */ |
||
48 | public function getRowIdentifier($table) |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * Adds a new row to a table. |
||
102 | * |
||
103 | * @param string $table The table in which to insert |
||
104 | * @param array $fields Array of given field in values |
||
105 | * @param array $values Array of new values for the row |
||
106 | * @param array $nulls An array mapping column => something if it is to be null |
||
107 | * @param array $format An array of the data type (VALUE or EXPRESSION) |
||
108 | * @param array $types An array of field types |
||
109 | * |
||
110 | * @return int 0 if operation was successful |
||
111 | */ |
||
112 | public function insertRow($table, $fields, $values, $nulls, $format, $types) |
||
146 | } |
||
147 | |||
148 | /** |
||
149 | * Formats a value or expression for sql purposes. |
||
150 | * |
||
151 | * @param string $type The type of the field |
||
152 | * @param mixed $format VALUE or EXPRESSION |
||
153 | * @param mixed $value The actual value entered in the field. Can be NULL |
||
154 | * |
||
155 | * @return mixed The suitably quoted and escaped value |
||
156 | */ |
||
157 | public function formatValue($type, $format, $value) |
||
205 | } |
||
206 | } |
||
207 | |||
208 | // View functions |
||
209 | |||
210 | /** |
||
211 | * Updates a row in a table. |
||
212 | * |
||
213 | * @param string $table The table in which to update |
||
214 | * @param array $vars An array mapping new values for the row |
||
215 | * @param array $nulls An array mapping column => something if it is to be null |
||
216 | * @param array $format An array of the data type (VALUE or EXPRESSION) |
||
217 | * @param array $types An array of field types |
||
218 | * @param array $keyarr An array mapping column => value to update |
||
219 | * |
||
220 | * @return bool|int 0 success |
||
221 | */ |
||
222 | public function editRow($table, $vars, $nulls, $format, $types, $keyarr) |
||
288 | } |
||
289 | |||
290 | /** |
||
291 | * Delete a row from a table. |
||
292 | * |
||
293 | * @param string $table The table from which to delete |
||
294 | * @param array $key An array mapping column => value to delete |
||
295 | * @param string $schema the schema of the table |
||
296 | * |
||
297 | * @return bool|int 0 success |
||
298 | */ |
||
299 | public function deleteRow($table, $key, $schema = '') |
||
327 | } |
||
328 | |||
329 | abstract public function fieldClean(&$str); |
||
330 | |||
331 | abstract public function beginTransaction(); |
||
332 | |||
333 | abstract public function rollbackTransaction(); |
||
334 | |||
335 | abstract public function endTransaction(); |
||
336 | |||
337 | abstract public function execute($sql); |
||
338 | |||
339 | abstract public function setComment($obj_type, $obj_name, $table, $comment, $basetype = null); |
||
340 | |||
341 | abstract public function selectSet($sql); |
||
342 | |||
343 | abstract public function clean(&$str); |
||
344 | |||
345 | abstract public function phpBool($parameter); |
||
346 | |||
347 | abstract public function hasCreateTableLikeWithConstraints(); |
||
348 | |||
349 | abstract public function hasCreateTableLikeWithIndexes(); |
||
350 | |||
351 | abstract public function hasTablespaces(); |
||
352 | |||
353 | abstract public function delete($table, $conditions, $schema = ''); |
||
354 | |||
355 | abstract public function fieldArrayClean(&$arr); |
||
356 | |||
357 | abstract public function hasCreateFieldWithConstraints(); |
||
358 | |||
359 | abstract public function getAttributeNames($table, $atts); |
||
360 | } |
||
361 |