Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like DatabaseTrait 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 DatabaseTrait, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | trait DatabaseTrait |
||
13 | { |
||
14 | /** |
||
15 | * Returns the current default_with_oids setting. |
||
16 | * |
||
17 | * @return string default_with_oids setting |
||
18 | */ |
||
19 | public function getDefaultWithOid() |
||
25 | |||
26 | /** |
||
27 | * Determines whether or not a user is a super user. |
||
28 | * |
||
29 | * @param string $username The username of the user |
||
30 | * |
||
31 | * @return bool true if is a super user, false otherwise |
||
32 | */ |
||
33 | public function isSuperUser($username = '') |
||
53 | |||
54 | /** |
||
55 | * Analyze a database. |
||
56 | * |
||
57 | * @param string $table (optional) The table to analyze |
||
58 | * |
||
59 | * @return bool 0 if successful |
||
60 | */ |
||
61 | public function analyzeDB($table = '') |
||
75 | |||
76 | /** |
||
77 | * Return all information about a particular database. |
||
78 | * |
||
79 | * @param string $database The name of the database to retrieve |
||
80 | * |
||
81 | * @return \PHPPgAdmin\ADORecordSet The database info |
||
82 | */ |
||
83 | public function getDatabase($database) |
||
90 | |||
91 | /** |
||
92 | * Return all database available on the server. |
||
93 | * |
||
94 | * @param null|string $currentdatabase database name that should be on top of the resultset |
||
95 | * |
||
96 | * @return \PHPPgAdmin\ADORecordSet A list of databases, sorted alphabetically |
||
97 | */ |
||
98 | public function getDatabases($currentdatabase = null) |
||
158 | |||
159 | /** |
||
160 | * Return the database comment of a db from the shared description table. |
||
161 | * |
||
162 | * @param string $database the name of the database to get the comment for |
||
163 | * |
||
164 | * @return \PHPPgAdmin\ADORecordSet recordset of the db comment info |
||
165 | */ |
||
166 | public function getDatabaseComment($database) |
||
177 | |||
178 | /** |
||
179 | * Return the database owner of a db. |
||
180 | * |
||
181 | * @param string $database the name of the database to get the owner for |
||
182 | * |
||
183 | * @return \PHPPgAdmin\ADORecordSet recordset of the db owner info |
||
184 | */ |
||
185 | public function getDatabaseOwner($database) |
||
192 | |||
193 | // Help functions |
||
194 | |||
195 | // Database functions |
||
196 | |||
197 | /** |
||
198 | * Returns the current database encoding. |
||
199 | * |
||
200 | * @return string The encoding. eg. SQL_ASCII, UTF-8, etc. |
||
201 | */ |
||
202 | public function getDatabaseEncoding() |
||
206 | |||
207 | /** |
||
208 | * Creates a database. |
||
209 | * |
||
210 | * @param string $database The name of the database to create |
||
211 | * @param string $encoding Encoding of the database |
||
212 | * @param string $tablespace (optional) The tablespace name |
||
213 | * @param string $comment |
||
214 | * @param string $template |
||
215 | * @param string $lc_collate |
||
216 | * @param string $lc_ctype |
||
217 | * |
||
218 | * @return int 0 success |
||
219 | */ |
||
220 | public function createDatabase( |
||
268 | |||
269 | /** |
||
270 | * Drops a database. |
||
271 | * |
||
272 | * @param string $database The name of the database to drop |
||
273 | * |
||
274 | * @return int 0 if operation was successful |
||
275 | */ |
||
276 | public function dropDatabase($database) |
||
283 | |||
284 | /** |
||
285 | * Alters a database |
||
286 | * the multiple return vals are for postgres 8+ which support more functionality in alter database. |
||
287 | * |
||
288 | * @param string $dbName The name of the database |
||
289 | * @param string $newName new name for the database |
||
290 | * @param string $newOwner The new owner for the database |
||
291 | * @param string $comment |
||
292 | * |
||
293 | * @return bool|int 0 success |
||
294 | */ |
||
295 | public function alterDatabase($dbName, $newName, $newOwner = '', $comment = '') |
||
333 | |||
334 | /** |
||
335 | * Renames a database, note that this operation cannot be |
||
336 | * performed on a database that is currently being connected to. |
||
337 | * |
||
338 | * @param string $oldName name of database to rename |
||
339 | * @param string $newName new name of database |
||
340 | * |
||
341 | * @return int 0 on success |
||
342 | */ |
||
343 | public function alterDatabaseRename($oldName, $newName) |
||
356 | |||
357 | /** |
||
358 | * Changes ownership of a database |
||
359 | * This can only be done by a superuser or the owner of the database. |
||
360 | * |
||
361 | * @param string $dbName database to change ownership of |
||
362 | * @param string $newOwner user that will own the database |
||
363 | * |
||
364 | * @return int 0 on success |
||
365 | */ |
||
366 | public function alterDatabaseOwner($dbName, $newOwner) |
||
375 | |||
376 | /** |
||
377 | * Returns prepared transactions information. |
||
378 | * |
||
379 | * @param null|string $database (optional) Find only prepared transactions executed in a specific database |
||
380 | * |
||
381 | * @return \PHPPgAdmin\ADORecordSet A recordset |
||
382 | */ |
||
383 | View Code Duplication | public function getPreparedXacts($database = null) |
|
395 | |||
396 | /** |
||
397 | * Returns all available process information. |
||
398 | * |
||
399 | * @param null|string $database (optional) Find only connections to specified database |
||
400 | * |
||
401 | * @return \PHPPgAdmin\ADORecordSet A recordset |
||
402 | */ |
||
403 | View Code Duplication | public function getProcesses($database = null) |
|
421 | |||
422 | // interfaces Statistics collector functions |
||
423 | |||
424 | /** |
||
425 | * Returns table locks information in the current database. |
||
426 | * |
||
427 | * @return \PHPPgAdmin\ADORecordSet A recordset |
||
428 | */ |
||
429 | View Code Duplication | public function getLocks() |
|
455 | |||
456 | /** |
||
457 | * Sends a cancel or kill command to a process. |
||
458 | * |
||
459 | * @param int $pid The ID of the backend process |
||
460 | * @param string $signal 'CANCEL' or 'KILL' |
||
461 | * |
||
462 | * @return int 0 success |
||
463 | */ |
||
464 | public function sendSignal($pid, $signal) |
||
490 | |||
491 | /** |
||
492 | * Vacuums a database. |
||
493 | * |
||
494 | * @param string $table The table to vacuum |
||
495 | * @param bool $analyze If true, also does analyze |
||
496 | * @param bool $full If true, selects "full" vacuum |
||
497 | * @param bool $freeze If true, selects aggressive "freezing" of tuples |
||
498 | * |
||
499 | * @return bool 0 if successful |
||
500 | */ |
||
501 | public function vacuumDB($table = '', $analyze = false, $full = false, $freeze = false) |
||
525 | |||
526 | /** |
||
527 | * Returns all autovacuum global configuration. |
||
528 | * |
||
529 | * @return array associative array array( param => value, ...) |
||
530 | */ |
||
531 | public function getAutovacuum() |
||
557 | |||
558 | /** |
||
559 | * Returns all available variable information. |
||
560 | * |
||
561 | * @return \PHPPgAdmin\ADORecordSet A recordset |
||
562 | */ |
||
563 | public function getVariables() |
||
569 | |||
570 | abstract public function fieldClean(&$str); |
||
571 | |||
572 | abstract public function beginTransaction(); |
||
573 | |||
574 | abstract public function rollbackTransaction(); |
||
575 | |||
576 | abstract public function endTransaction(); |
||
577 | |||
578 | abstract public function execute($sql); |
||
579 | |||
580 | abstract public function setComment($obj_type, $obj_name, $table, $comment, $basetype = null); |
||
581 | |||
582 | abstract public function selectSet($sql); |
||
583 | |||
584 | abstract public function clean(&$str); |
||
585 | |||
586 | abstract public function phpBool($parameter); |
||
587 | |||
588 | abstract public function hasCreateTableLikeWithConstraints(); |
||
589 | |||
590 | abstract public function hasCreateTableLikeWithIndexes(); |
||
591 | |||
592 | abstract public function hasTablespaces(); |
||
593 | |||
594 | abstract public function delete($table, $conditions, $schema = ''); |
||
595 | |||
596 | abstract public function fieldArrayClean(&$arr); |
||
597 | |||
598 | abstract public function hasCreateFieldWithConstraints(); |
||
599 | |||
600 | abstract public function getAttributeNames($table, $atts); |
||
601 | |||
602 | abstract public function hasSharedComments(); |
||
603 | |||
604 | abstract public function selectField($sql, $field); |
||
605 | } |
||
606 |
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: