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. 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 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) |
||
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) |
||
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) |
||
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) |
||
208 | |||
209 | // View functions |
||
210 | |||
211 | /** |
||
212 | * Updates a row in a table. |
||
213 | * |
||
214 | * @param string $table The table in which to update |
||
215 | * @param array $vars An array mapping new values for the row |
||
216 | * @param array $nulls An array mapping column => something if it is to be null |
||
217 | * @param array $format An array of the data type (VALUE or EXPRESSION) |
||
218 | * @param array $types An array of field types |
||
219 | * @param array $keyarr An array mapping column => value to update |
||
220 | * |
||
221 | * @return bool|int 0 success |
||
222 | */ |
||
223 | public function editRow($table, $vars, $nulls, $format, $types, $keyarr) |
||
290 | |||
291 | /** |
||
292 | * Delete a row from a table. |
||
293 | * |
||
294 | * @param string $table The table from which to delete |
||
295 | * @param array $key An array mapping column => value to delete |
||
296 | * @param string $schema the schema of the table |
||
297 | * |
||
298 | * @return bool|int 0 success |
||
299 | */ |
||
300 | public function deleteRow($table, $key, $schema = '') |
||
329 | |||
330 | abstract public function fieldClean(&$str); |
||
331 | |||
332 | abstract public function beginTransaction(); |
||
333 | |||
334 | abstract public function rollbackTransaction(); |
||
335 | |||
336 | abstract public function endTransaction(); |
||
337 | |||
338 | abstract public function execute($sql); |
||
339 | |||
340 | abstract public function setComment($obj_type, $obj_name, $table, $comment, $basetype = null); |
||
341 | |||
342 | abstract public function selectSet($sql); |
||
343 | |||
344 | abstract public function clean(&$str); |
||
345 | |||
346 | abstract public function phpBool($parameter); |
||
347 | |||
348 | abstract public function hasCreateTableLikeWithConstraints(); |
||
349 | |||
350 | abstract public function hasCreateTableLikeWithIndexes(); |
||
351 | |||
352 | abstract public function hasTablespaces(); |
||
353 | |||
354 | abstract public function delete($table, $conditions, $schema = ''); |
||
355 | |||
356 | abstract public function fieldArrayClean(&$arr); |
||
357 | |||
358 | abstract public function hasCreateFieldWithConstraints(); |
||
359 | |||
360 | abstract public function getAttributeNames($table, $atts); |
||
361 | |||
362 | abstract public function hasObjectID($table); |
||
363 | } |
||
364 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: