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 | public function own_limit($st, $en = NULL) |
||
| 93 | |||
| 94 | /** */ |
||
| 95 | public function own_group_by($params) |
||
| 100 | |||
| 101 | /** */ |
||
| 102 | public function own_order_by($field, $direction = 'ASC') |
||
| 107 | |||
| 108 | /** @see idbQuery::flush() */ |
||
| 109 | public function flush() |
||
| 126 | |||
| 127 | /** @see idbQuery::random() */ |
||
| 128 | public function random(& $return_value = null) |
||
| 136 | |||
| 137 | |||
| 138 | /** |
||
| 139 | * @see idbQuery::or_() |
||
| 140 | * @deprecated |
||
| 141 | */ |
||
| 142 | public function or_($relation = 'OR') |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Set debug query mode |
||
| 159 | * @param bool $value Debug status, true - active |
||
| 160 | * |
||
| 161 | * @return $this Chaining |
||
| 162 | */ |
||
| 163 | public function debug($value = true) |
||
| 169 | |||
| 170 | public function isnull($attribute) |
||
| 186 | |||
| 187 | |||
| 188 | |||
| 189 | /** @deprecated Use self::fields() */ |
||
| 190 | public function fieldsNew($fieldName, & $return = null) |
||
| 194 | |||
| 195 | /** @see idbQuery::join() */ |
||
| 196 | public function join($tableName, $className = null, $ignore = false) |
||
| 204 | |||
| 205 | /** @see idbQuery::group_by() */ |
||
| 206 | public function group_by($field) |
||
| 219 | |||
| 220 | /** @see idbQuery::limit() */ |
||
| 221 | public function limit($st, $en = NULL, $own = false) |
||
| 233 | |||
| 234 | /** @see idbQuery::order_by() */ |
||
| 235 | public function order_by($field, $direction = 'ASC') |
||
| 242 | |||
| 243 | /** @see idbQuery::add_field() */ |
||
| 244 | public function add_field($field, $alias = null, $own = true) |
||
| 263 | |||
| 264 | /** @see idbQuery::innerCount() */ |
||
| 265 | public function innerCount($field = '*') |
||
| 269 | |||
| 270 | /** @see idbQuery::parse() */ |
||
| 271 | public function parse($queryText, array $args = null) |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Function to reconfigure dbQuery to work with multiple Entities |
||
| 333 | * |
||
| 334 | * @param string $className Entity name |
||
| 335 | * @deprecated @see \samsonframework\orm\QueryInterface::entity(), full class name with namespace |
||
| 336 | * should be passed. |
||
| 337 | * @return self|string Chaining or current class name if nothing is passed |
||
| 338 | */ |
||
| 339 | public function className($className = null) |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Add condition by primary field |
||
| 352 | * |
||
| 353 | * @param string $value Primary field value |
||
| 354 | * @return self Chaining |
||
| 355 | * @deprecated Use direct query with where('PRIMARY_FIELD',...) |
||
| 356 | */ |
||
| 357 | public function id($value) |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Add condition to current query. |
||
| 369 | * This method supports receives three possible types for $fieldName, |
||
| 370 | * this is deprecated logic and this should be changed to use separate methods |
||
| 371 | * for each argument type. |
||
| 372 | * |
||
| 373 | * @param string|ConditionInterface|ArgumentInterface $fieldName Entity field name |
||
| 374 | * @param string $fieldValue Value |
||
| 375 | * @param string $relation Relation between field name and its value |
||
| 376 | * @deprecated @see self::where() |
||
| 377 | * @return self Chaining |
||
| 378 | */ |
||
| 379 | public function cond($fieldName, $fieldValue = null, $relation = '=') |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Query constructor. |
||
| 398 | * @param string|null $entity Entity identifier |
||
| 399 | * @throws EntityNotFound |
||
| 400 | */ |
||
| 401 | public function __construct($entity = 'material') |
||
| 413 | |||
| 414 | /** |
||
| 415 | * Magic method for setting beautiful conditions. |
||
| 416 | * |
||
| 417 | * @deprecated Use ->where(..)-> |
||
| 418 | * @param $methodName |
||
| 419 | * @param array $arguments |
||
| 420 | * @return $this|array|bool|dbQuery |
||
| 421 | */ |
||
| 422 | public function __call($methodName, array $arguments) |
||
| 446 | } |
||
| 447 |
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.