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 ADOdbBase 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 ADOdbBase, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class ADOdbBase |
||
|
1 ignored issue
–
show
|
|||
| 18 | { |
||
| 19 | use \PHPPgAdmin\Traits\HelperTrait; |
||
| 20 | use \PHPPgAdmin\Database\Traits\HasTrait; |
||
| 21 | |||
| 22 | public $lang; |
||
| 23 | public $conf; |
||
| 24 | protected $container; |
||
| 25 | protected $server_info; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Base constructor. |
||
| 29 | * |
||
| 30 | * @param \PHPPgAdmin\ADONewConnection $conn The connection object |
||
| 31 | * @param mixed $container |
||
| 32 | * @param mixed $server_info |
||
| 33 | */ |
||
| 34 | View Code Duplication | public function __construct(&$conn, $container, $server_info) |
|
| 45 | |||
| 46 | /** |
||
| 47 | * Sets the comment for an object in the database. |
||
| 48 | * |
||
| 49 | * @pre All parameters must already be cleaned |
||
| 50 | * |
||
| 51 | * @param string $obj_type One of 'TABLE' | 'COLUMN' | 'VIEW' | 'SCHEMA' | 'SEQUENCE' | 'TYPE' | 'FUNCTION' | 'AGGREGATE' |
||
| 52 | * @param string $obj_name the name of the object for which to attach a comment |
||
| 53 | * @param string $table Name of table that $obj_name belongs to. Ignored unless $obj_type is 'TABLE' or 'COLUMN'. |
||
| 54 | * @param string $comment the comment to add |
||
| 55 | * @param null|string $basetype |
||
| 56 | * |
||
| 57 | * @return int|\PHPPgAdmin\ADORecordSet recordset of results or error code |
||
| 58 | */ |
||
| 59 | public function setComment($obj_type, $obj_name, $table, $comment, $basetype = null) |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Turns on or off query debugging. |
||
| 123 | * |
||
| 124 | * @param bool $debug True to turn on debugging, false otherwise |
||
| 125 | */ |
||
| 126 | public function setDebug($debug) |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Cleans (escapes) an array of field names. |
||
| 133 | * |
||
| 134 | * @param array $arr The array to clean, by reference |
||
| 135 | * |
||
| 136 | * @return array The cleaned array |
||
| 137 | */ |
||
| 138 | public function fieldArrayClean(&$arr) |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Cleans (escapes) an array. |
||
| 153 | * |
||
| 154 | * @param array $arr The array to clean, by reference |
||
| 155 | * |
||
| 156 | * @return array The cleaned array |
||
| 157 | */ |
||
| 158 | public function arrayClean(&$arr) |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Executes a query on the underlying connection. |
||
| 172 | * |
||
| 173 | * @param string $sql The SQL query to execute |
||
| 174 | * |
||
| 175 | * @return \PHPPgAdmin\ADORecordSet A recordset |
||
| 176 | */ |
||
| 177 | public function execute($sql) |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Closes the connection the database class |
||
| 188 | * relies on. |
||
| 189 | */ |
||
| 190 | public function close() |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Retrieves a ResultSet from a query. |
||
| 197 | * |
||
| 198 | * @param string $sql The SQL statement to be executed |
||
| 199 | * |
||
| 200 | * @return int|\PHPPgAdmin\ADORecordSet A recordset or an error number |
||
| 201 | */ |
||
| 202 | public function selectSet($sql) |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Retrieves a single value from a query. |
||
| 216 | * |
||
| 217 | * @@ assumes that the query will return only one row - returns field value in the first row |
||
| 218 | * |
||
| 219 | * @param string $sql The SQL statement to be executed |
||
| 220 | * @param string $field The field name to be returned |
||
| 221 | * |
||
| 222 | * @return int|string single field value, error number on error or -1 if no rows where found |
||
| 223 | */ |
||
| 224 | public function selectField($sql, $field) |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Delete from the database. |
||
| 243 | * |
||
| 244 | * @param string $table The name of the table |
||
| 245 | * @param array $conditions (array) A map of field names to conditions |
||
| 246 | * @param string $schema (optional) The table's schema |
||
| 247 | * |
||
| 248 | * @return int 0 success |
||
| 249 | */ |
||
| 250 | public function delete($table, $conditions, $schema = '') |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Cleans (escapes) an object name (eg. table, field). |
||
| 292 | * |
||
| 293 | * @param null|string $str The string to clean, by reference |
||
| 294 | * |
||
| 295 | * @return null|string The cleaned string |
||
| 296 | */ |
||
| 297 | public function fieldClean(&$str) |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Cleans (escapes) a string. |
||
| 309 | * |
||
| 310 | * @param null|string $str The string to clean, by reference |
||
| 311 | * |
||
| 312 | * @return null|string The cleaned string |
||
| 313 | */ |
||
| 314 | public function clean(&$str) |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Escapes bytea data for display on the screen. |
||
| 327 | * |
||
| 328 | * @param string $data The bytea data |
||
| 329 | * |
||
| 330 | * @return string Data formatted for on-screen display |
||
| 331 | */ |
||
| 332 | public function escapeBytea($data) |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Insert a set of values into the database. |
||
| 339 | * |
||
| 340 | * @param string $table The table to insert into |
||
| 341 | * @param array $vars (array) A mapping of the field names to the values to be inserted |
||
| 342 | * |
||
| 343 | * @return int 0 success |
||
| 344 | */ |
||
| 345 | public function insert($table, $vars) |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Update a row in the database. |
||
| 389 | * |
||
| 390 | * @param string $table The table that is to be updated |
||
| 391 | * @param array $vars (array) A mapping of the field names to the values to be updated |
||
| 392 | * @param array $where (array) A mapping of field names to values for the where clause |
||
| 393 | * @param array $nulls (array, optional) An array of fields to be set null |
||
| 394 | * |
||
| 395 | * @return int 0 success |
||
| 396 | */ |
||
| 397 | public function update($table, $vars, $where, $nulls = []) |
||
| 459 | |||
| 460 | /** |
||
| 461 | * Begin a transaction. |
||
| 462 | * |
||
| 463 | * @return bool 0 success |
||
| 464 | */ |
||
| 465 | public function beginTransaction() |
||
| 469 | |||
| 470 | /** |
||
| 471 | * End a transaction. |
||
| 472 | * |
||
| 473 | * @return bool 0 success |
||
| 474 | */ |
||
| 475 | public function endTransaction() |
||
| 479 | |||
| 480 | /** |
||
| 481 | * Roll back a transaction. |
||
| 482 | * |
||
| 483 | * @return bool 0 success |
||
| 484 | */ |
||
| 485 | public function rollbackTransaction() |
||
| 489 | |||
| 490 | /** |
||
| 491 | * Get the backend platform. |
||
| 492 | * |
||
| 493 | * @return string The backend platform |
||
| 494 | */ |
||
| 495 | public function getPlatform() |
||
| 505 | |||
| 506 | // Type conversion routines |
||
| 507 | |||
| 508 | /** |
||
| 509 | * Change the value of a parameter to database representation depending on whether it evaluates to true or false. |
||
| 510 | * |
||
| 511 | * @param mixed $parameter the parameter |
||
| 512 | * |
||
| 513 | * @return string boolean database representation |
||
| 514 | */ |
||
| 515 | public function dbBool(&$parameter) |
||
| 525 | |||
| 526 | /** |
||
| 527 | * Change a parameter from database representation to a boolean, (others evaluate to false). |
||
| 528 | * |
||
| 529 | * @param string $parameter the parameter |
||
| 530 | * |
||
| 531 | * @return bool |
||
| 532 | */ |
||
| 533 | public function phpBool($parameter) |
||
| 537 | |||
| 538 | /** |
||
| 539 | * Change a db array into a PHP array. |
||
| 540 | * |
||
| 541 | * @param string $dbarr |
||
| 542 | * |
||
| 543 | * @return array A PHP array |
||
| 544 | * |
||
| 545 | * @internal param String $arr representing the DB array |
||
| 546 | */ |
||
| 547 | public function phpArray($dbarr) |
||
| 588 | } |
||
| 589 |
This check marks property names that have not been written in camelCase.
In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes
databaseConnectionString.