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 dbQuery 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 dbQuery, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class dbQuery extends \samsonframework\orm\Query |
||
|
|
|||
| 15 | { |
||
| 16 | /** |
||
| 17 | * Указатель на текущую группу условий с которой работает запрос |
||
| 18 | * |
||
| 19 | * @var Condition |
||
| 20 | */ |
||
| 21 | public $cConditionGroup; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Указатель на соединение с БД |
||
| 25 | * @var resource |
||
| 26 | */ |
||
| 27 | public $link; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Указатель на группу условий для текущего объекта с которой работает запрос |
||
| 31 | * |
||
| 32 | * @var Condition |
||
| 33 | */ |
||
| 34 | public $own_condition; |
||
| 35 | |||
| 36 | /** Limiting filter for base table */ |
||
| 37 | public $own_limit; |
||
| 38 | |||
| 39 | /** Grouping filter for base table */ |
||
| 40 | public $own_group; |
||
| 41 | |||
| 42 | /** Sorting filter for base table */ |
||
| 43 | public $own_order; |
||
| 44 | |||
| 45 | /** Virtual field for base table */ |
||
| 46 | public $own_virtual_fields = array(); |
||
| 47 | |||
| 48 | /** Virtual fields */ |
||
| 49 | public $virtual_fields = array(); |
||
| 50 | |||
| 51 | public $empty = false; |
||
| 52 | |||
| 53 | /** @var bool True to show requests */ |
||
| 54 | protected $debug = false; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Коллекция условных групп для запроса |
||
| 58 | * @var dbConditionGroup |
||
| 59 | */ |
||
| 60 | public $condition; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Параметры ограничения результатов запроса к БД |
||
| 64 | * @var array |
||
| 65 | */ |
||
| 66 | public $limit = array(); |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Параметры сортировки результатов запроса к БД |
||
| 70 | * @var array |
||
| 71 | */ |
||
| 72 | public $order = array(); |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Параметры группировки результатов запроса к БД |
||
| 76 | * @var array |
||
| 77 | */ |
||
| 78 | public $group = array(); |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Коллекция параметров для запроса к связанным объектам |
||
| 82 | * @var array |
||
| 83 | */ |
||
| 84 | public $join = array(); |
||
| 85 | |||
| 86 | |||
| 87 | |||
| 88 | /** Query handlers stack */ |
||
| 89 | protected $stack = array(); |
||
| 90 | |||
| 91 | /** Query parameters stack */ |
||
| 92 | protected $params = array(); |
||
| 93 | |||
| 94 | // /** |
||
| 95 | // * Universal handler to pass to CMSMaterial::get() * |
||
| 96 | // * @param samson\activerecord\dbQuery $db_query Original db query object for modifiyng |
||
| 97 | // * |
||
| 98 | // */ |
||
| 99 | // protected function __handler() |
||
| 100 | // { |
||
| 101 | // // Iterate handlers and run them |
||
| 102 | // foreach ( $this->stack as $i => $handler ) |
||
| 103 | // { |
||
| 104 | // // Create handler params array with first parameter pointing to this query object |
||
| 105 | // $params = array( &$this ); |
||
| 106 | |||
| 107 | // // Combine params with existing ones in one array |
||
| 108 | // $params = array_merge( $params, $this->params[ $i ] ); |
||
| 109 | |||
| 110 | // // Append this query object as first handler parameter |
||
| 111 | // //array_unshift( $this->params[ $i ] , & $this ); |
||
| 112 | |||
| 113 | // //trace($this->params[ $i ]); |
||
| 114 | // call_user_func_array( $handler, $params ); |
||
| 115 | // } |
||
| 116 | // } |
||
| 117 | |||
| 118 | // /** |
||
| 119 | // * Add query handler |
||
| 120 | // * @param callable $callable External handler |
||
| 121 | // * @return samson\activerecord\dbQuery |
||
| 122 | // */ |
||
| 123 | // public function handler( $callable ) |
||
| 124 | // { |
||
| 125 | // // If normal handler is passed |
||
| 126 | // if( is_callable( $callable ) ) |
||
| 127 | // { |
||
| 128 | // // Add handler |
||
| 129 | // $this->stack[] = $callable; |
||
| 130 | |||
| 131 | // // Get passed arguments |
||
| 132 | // $args = func_get_args(); |
||
| 133 | |||
| 134 | // // Remove first argument |
||
| 135 | // array_shift( $args ); |
||
| 136 | |||
| 137 | // // Add handler parameters stack |
||
| 138 | // $this->params[] = & $args; |
||
| 139 | // } |
||
| 140 | // else e('Cannot set CMS Query handler - function(##) does not exists', E_SAMSON_CMS_ERROR, $callable ); |
||
| 141 | |||
| 142 | // return $this; |
||
| 143 | // } |
||
| 144 | |||
| 145 | // /** @see idbQuery::fields() */ |
||
| 146 | // public function fields( $field_name, & $return_value = null ) |
||
| 147 | // { |
||
| 148 | // // Call handlers stack |
||
| 149 | // $this->_callHandlers(); |
||
| 150 | |||
| 151 | // // Iterate records and gather specified field |
||
| 152 | // $return_value = array(); |
||
| 153 | // foreach ( db()->find( $this->class_name, $this ) as $record ) $return_value[] = $record->$field_name; |
||
| 154 | |||
| 155 | // // Clear this query |
||
| 156 | // $this->flush(); |
||
| 157 | |||
| 158 | // // Method return value |
||
| 159 | // $return = null; |
||
| 160 | |||
| 161 | // // If return value is passed - return boolean about request results |
||
| 162 | // if( func_num_args() > 1 ) $return = (is_array( $return_value ) && sizeof( $return_value )); |
||
| 163 | // // Set request results as return value |
||
| 164 | // else $return = & $return_value; |
||
| 165 | |||
| 166 | // // Otherwise just return request results |
||
| 167 | // return $return; |
||
| 168 | // } |
||
| 169 | |||
| 170 | // /** @see idbQuery::get() */ |
||
| 171 | // public function & exec( & $return_value = null) |
||
| 172 | // { |
||
| 173 | // // Call handlers stack |
||
| 174 | // $this->_callHandlers(); |
||
| 175 | |||
| 176 | // // Perform DB request |
||
| 177 | // $return_value = db()->find( $this->class_name, $this ); |
||
| 178 | |||
| 179 | // // Clear this query |
||
| 180 | // $this->flush(); |
||
| 181 | |||
| 182 | // // Method return value |
||
| 183 | // $return = null; |
||
| 184 | |||
| 185 | // // If return value is passed - return boolean about request results |
||
| 186 | // if( func_num_args() ) $return = (is_array( $return_value ) && sizeof( $return_value )); |
||
| 187 | // // Set request results as return value |
||
| 188 | // else $return = & $return_value; |
||
| 189 | |||
| 190 | // // Otherwise just return request results |
||
| 191 | // return $return; |
||
| 192 | // } |
||
| 193 | |||
| 194 | // /** @see idbQuery::first() */ |
||
| 195 | // public function & first( & $return_value = null) |
||
| 196 | // { |
||
| 197 | // // Call handlers stack |
||
| 198 | // $this->_callHandlers(); |
||
| 199 | |||
| 200 | // // Выполним запрос к БД |
||
| 201 | // $return_value = db()->find( $this->class_name, $this ); |
||
| 202 | |||
| 203 | // // Получим первую запись из полученного массива, если она есть |
||
| 204 | // $return_value = isset( $return_value[0] ) ? $return_value[0] : null; |
||
| 205 | |||
| 206 | // // Очистим запрос |
||
| 207 | // $this->flush(); |
||
| 208 | |||
| 209 | // // Локальная переменная для возврата правильного результата |
||
| 210 | // $return = null; |
||
| 211 | |||
| 212 | // // Если хоть что-то передано в функцию - запишем в локальную переменную boolean значение |
||
| 213 | // // которое покажет результат выполнения запроса к БД |
||
| 214 | // if( func_num_args() ) $return = isset( $return_value ); |
||
| 215 | // // Сделаем копию полученных данных в локальную переменную |
||
| 216 | // else $return = & $return_value; |
||
| 217 | |||
| 218 | // // Вернем значение из локальной переменной |
||
| 219 | // return $return; |
||
| 220 | // } |
||
| 221 | |||
| 222 | /** */ |
||
| 223 | public function own_limit($st, $en = NULL) |
||
| 228 | |||
| 229 | /** */ |
||
| 230 | public function own_group_by($params) |
||
| 235 | |||
| 236 | /** */ |
||
| 237 | public function own_order_by($field, $direction = 'ASC') |
||
| 242 | |||
| 243 | /** @see idbQuery::flush() */ |
||
| 244 | public function flush() |
||
| 261 | |||
| 262 | /** @see idbQuery::random() */ |
||
| 263 | public function random(& $return_value = null) |
||
| 271 | |||
| 272 | |||
| 273 | /** |
||
| 274 | * @see idbQuery::or_() |
||
| 275 | * @deprecated |
||
| 276 | */ |
||
| 277 | public function or_($relation = 'OR') |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Set debug query mode |
||
| 294 | * @param bool $value Debug status, true - active |
||
| 295 | * |
||
| 296 | * @return $this Chaining |
||
| 297 | */ |
||
| 298 | public function debug($value = true) |
||
| 304 | |||
| 305 | public function isnull($attribute) |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Add condition by primary field |
||
| 324 | * |
||
| 325 | * @param string $value Primary field value |
||
| 326 | * @return \samson\activerecord\dbQuery Chaining |
||
| 327 | */ |
||
| 328 | public function id($value) |
||
| 336 | |||
| 337 | /** @see idbQuery::where() */ |
||
| 338 | public function where($condition) |
||
| 342 | |||
| 343 | /** @deprecated Use self::fields() */ |
||
| 344 | public function fieldsNew($fieldName, & $return = null) |
||
| 348 | |||
| 349 | /** @see idbQuery::join() */ |
||
| 350 | public function join($tableName, $className = null, $ignore = false) |
||
| 358 | |||
| 359 | /** @see idbQuery::group_by() */ |
||
| 360 | public function group_by($field) |
||
| 373 | |||
| 374 | /** @see idbQuery::limit() */ |
||
| 375 | public function limit($st, $en = NULL, $own = false) |
||
| 387 | |||
| 388 | /** @see idbQuery::order_by() */ |
||
| 389 | public function order_by($field, $direction = 'ASC') |
||
| 396 | |||
| 397 | /** @see idbQuery::add_field() */ |
||
| 398 | public function add_field($field, $alias = null, $own = true) |
||
| 417 | |||
| 418 | /** @see idbQuery::count() */ |
||
| 419 | public function count($field = '*') |
||
| 423 | |||
| 424 | /** @see idbQuery::innerCount() */ |
||
| 425 | public function innerCount($field = '*') |
||
| 429 | |||
| 430 | /** @see idbQuery::parse() */ |
||
| 431 | public function parse($queryText, array $args = null) |
||
| 490 | |||
| 491 | /** |
||
| 492 | * Function to reconfigure dbQuery to work with multiple Entities |
||
| 493 | * |
||
| 494 | * @param string $className Entity name |
||
| 495 | * @return self|string Chaining or current class name if nothing is passed |
||
| 496 | */ |
||
| 497 | public function className($className = null) |
||
| 514 | |||
| 515 | // Magic method after-clonning |
||
| 516 | public function __clone() |
||
| 526 | |||
| 527 | // Магический метод для выполнения не описанных динамических методов класса |
||
| 528 | public function __call($methodName, array $arguments) |
||
| 553 | |||
| 554 | /** |
||
| 555 | * Конструктор |
||
| 556 | * @param string|null $className Имя класса для которого создается запрос к БД |
||
| 557 | * @param mixed $link Указатель на экземпляр подключения к БД |
||
| 558 | */ |
||
| 559 | public function __construct($className = null, & $link = null) |
||
| 578 | } |
||
| 579 |
Classes in PHP are usually named in CamelCase.
In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. The whole name starts with a capital letter as well.
Thus the name database provider becomes
DatabaseProvider.