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:
| 1 | <?php |
||
| 21 | class DbQuery { |
||
| 22 | |||
| 23 | const SELECT = 'SELECT'; |
||
| 24 | const INSERT = 'INSERT'; |
||
| 25 | const UPDATE = 'UPDATE'; |
||
| 26 | const DELETE = 'DELETE'; |
||
| 27 | const REPLACE = 'REPLACE'; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var db_mysql |
||
| 31 | */ |
||
| 32 | protected $db; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Which command would be performed |
||
| 36 | * |
||
| 37 | * @var string $command |
||
| 38 | */ |
||
| 39 | protected $command; |
||
| 40 | |||
| 41 | protected $table = ''; |
||
| 42 | protected $fields = array(); |
||
| 43 | protected $where = array(); |
||
| 44 | protected $whereDanger = array(); |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Variable for increment query build |
||
| 48 | * |
||
| 49 | * @var string[] $build |
||
| 50 | */ |
||
| 51 | protected $build = array(); |
||
| 52 | |||
| 53 | protected $isOneRow = false; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @param null|db_mysql $db |
||
| 57 | * |
||
| 58 | * @return static |
||
| 59 | */ |
||
| 60 | public static function build($db = null) { |
||
| 63 | |||
| 64 | /** |
||
| 65 | * DbQuery constructor. |
||
| 66 | * |
||
| 67 | * @param null|\db_mysql $db |
||
| 68 | */ |
||
| 69 | // TODO - $db should be supplied externally |
||
| 70 | public function __construct($db = null) { |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Wrapper for db_escape() |
||
| 76 | * |
||
| 77 | * @param $string |
||
| 78 | * |
||
| 79 | * @return string |
||
| 80 | */ |
||
| 81 | protected function escape($string) { |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Wrapper for db_escape() |
||
| 87 | * |
||
| 88 | * @param mixed $value |
||
| 89 | * |
||
| 90 | * @return string |
||
| 91 | */ |
||
| 92 | protected function stringValue($value) { |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Quote mysql DB identifier |
||
| 98 | * |
||
| 99 | * @param mixed $fieldName |
||
| 100 | * |
||
| 101 | * @return string |
||
| 102 | */ |
||
| 103 | protected function quote($fieldName) { |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Quote table name with {{ }} |
||
| 109 | * |
||
| 110 | * @param mixed $tableName |
||
| 111 | * |
||
| 112 | * @return string |
||
| 113 | */ |
||
| 114 | protected function quoteTable($tableName) { |
||
| 117 | |||
| 118 | public function table($table) { |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @param bool $oneRow - DB_RECORDS_ALL || DB_RECORD_ONE |
||
| 126 | * |
||
| 127 | * @return $this |
||
| 128 | */ |
||
| 129 | public function oneRow($oneRow = DB_RECORDS_ALL) { |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Merges WHERE array as array_merge() |
||
| 137 | * |
||
| 138 | * @param array $whereArray |
||
| 139 | */ |
||
| 140 | public function whereArray($whereArray = array()) { |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Merges WHERE array as array_merge() |
||
| 148 | * |
||
| 149 | * @param array $whereArrayDanger |
||
| 150 | * @deprecated |
||
| 151 | */ |
||
| 152 | public function whereArrayDanger($whereArrayDanger = array()) { |
||
| 157 | |||
| 158 | View Code Duplication | protected function castAsDbValue($value) { |
|
| 187 | |||
| 188 | /** |
||
| 189 | * Make list of DANGER where clauses |
||
| 190 | * |
||
| 191 | * This function is DANGER! It takes numeric indexes which translate to direct SQL string which can lead to SQL injection! |
||
| 192 | * |
||
| 193 | * @param array $where - array WHERE clauses which will not pass through SAFE filter |
||
| 194 | * |
||
| 195 | * @return array |
||
| 196 | */ |
||
| 197 | protected function dangerWhere($where) { |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Make field list safe. NOT DANGER |
||
| 216 | * |
||
| 217 | * This function is NOT DANGER |
||
| 218 | * Make SQL-safe assignment/equal compare string from (field => value) pair |
||
| 219 | * |
||
| 220 | * @param array $fieldValues - array of pair $fieldName => $fieldValue |
||
| 221 | * |
||
| 222 | * @return array |
||
| 223 | */ |
||
| 224 | protected function fieldEqValue($fieldValues) { |
||
| 240 | |||
| 241 | protected function buildCommand() { |
||
| 248 | |||
| 249 | protected function buildWhere() { |
||
| 259 | |||
| 260 | protected function buildLimit() { |
||
| 265 | |||
| 266 | |||
| 267 | public function delete() { |
||
| 277 | |||
| 278 | } |
||
| 279 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.