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::count() */ |
||
265 | public function count($field = '*') |
||
269 | |||
270 | /** @see idbQuery::innerCount() */ |
||
271 | public function innerCount($field = '*') |
||
275 | |||
276 | /** @see idbQuery::parse() */ |
||
277 | public function parse($queryText, array $args = null) |
||
336 | |||
337 | /** |
||
338 | * Function to reconfigure dbQuery to work with multiple Entities |
||
339 | * |
||
340 | * @param string $className Entity name |
||
341 | * @deprecated @see \samsonframework\orm\QueryInterface::entity(), full class name with namespace |
||
342 | * should be passed. |
||
343 | * @return self|string Chaining or current class name if nothing is passed |
||
344 | */ |
||
345 | public function className($className = null) |
||
355 | |||
356 | /** |
||
357 | * Add condition by primary field |
||
358 | * |
||
359 | * @param string $value Primary field value |
||
360 | * @return self Chaining |
||
361 | * @deprecated Use direct query with where('PRIMARY_FIELD',...) |
||
362 | */ |
||
363 | public function id($value) |
||
372 | |||
373 | /** |
||
374 | * Add condition to current query. |
||
375 | * This method supports receives three possible types for $fieldName, |
||
376 | * this is deprecated logic and this should be changed to use separate methods |
||
377 | * for each argument type. |
||
378 | * |
||
379 | * @param string|ConditionInterface|ArgumentInterface $fieldName Entity field name |
||
380 | * @param string $fieldValue Value |
||
381 | * @param string $relation Relation between field name and its value |
||
382 | * @deprecated @see self::where() |
||
383 | * @return self Chaining |
||
384 | */ |
||
385 | public function cond($fieldName, $fieldValue = null, $relation = '=') |
||
386 | { |
||
387 | // If empty array is passed |
||
388 | if (is_string($fieldName)) { |
||
389 | return $this->where($fieldName, $fieldValue, $relation); |
||
390 | } elseif (is_array($fieldValue) && !sizeof($fieldValue)) { |
||
391 | $this->empty = true; |
||
392 | return $this; |
||
393 | } elseif (is_a($fieldName, '\samsonframework\orm\ConditionInterface')) { |
||
394 | $this->whereCondition($fieldName); |
||
395 | } elseif (is_a($fieldName, '\samsonframework\orm\ArgumentInterface')) { |
||
396 | $this->getConditionGroup($fieldName->field)->addArgument($fieldName); |
||
397 | } |
||
398 | |||
399 | return $this; |
||
400 | } |
||
401 | |||
402 | /** |
||
403 | * Query constructor. |
||
404 | * @param string|null $entity Entity identifier |
||
405 | * @throws EntityNotFound |
||
406 | */ |
||
407 | public function __construct($entity = 'material') |
||
418 | |||
419 | /** |
||
420 | * Magic method for setting beautiful conditions. |
||
421 | * |
||
422 | * @deprecated Use ->where(..)-> |
||
423 | * @param $methodName |
||
424 | * @param array $arguments |
||
425 | * @return $this|array|bool|dbQuery |
||
426 | */ |
||
427 | public function __call($methodName, array $arguments) |
||
451 | } |
||
452 |
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
.