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 |
||
| 19 | class SelectQuery extends Query |
||
| 20 | { |
||
| 21 | use SelectableTrait; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var SelectStatement |
||
| 25 | */ |
||
| 26 | protected $select; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var FromStatement |
||
| 30 | */ |
||
| 31 | protected $from; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var WhereStatement |
||
| 35 | */ |
||
| 36 | protected $where; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var WhereStatement |
||
| 40 | */ |
||
| 41 | protected $having; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var OrderStatement |
||
| 45 | */ |
||
| 46 | protected $orderBy; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var OrderStatement |
||
| 50 | */ |
||
| 51 | protected $groupBy; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var LimitStatement |
||
| 55 | */ |
||
| 56 | protected $limit; |
||
| 57 | |||
| 58 | public function initialize() |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Sets the fields to be selected for the query. |
||
| 71 | * |
||
| 72 | * @param array|string $fields fields |
||
| 73 | * |
||
| 74 | * @return self |
||
| 75 | */ |
||
| 76 | public function select($fields) |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Sets the table for the query. |
||
| 85 | * |
||
| 86 | * @param string $table table name |
||
| 87 | * |
||
| 88 | * @return self |
||
| 89 | */ |
||
| 90 | public function from($table) |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Adds a join to the query. |
||
| 99 | * |
||
| 100 | * @param string $table table name |
||
| 101 | * @param string $on ON condition |
||
| 102 | * @param string $using USING columns |
||
| 103 | * @param string $type optional join type if not JOIN |
||
| 104 | * |
||
| 105 | * @return self |
||
| 106 | */ |
||
| 107 | public function join($table, $on = null, $using = null, $type = 'JOIN') |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Sets the where conditions for the query. |
||
| 116 | * |
||
| 117 | * @param array|string $field |
||
| 118 | * @param string $condition condition value (optional) |
||
| 119 | * @param string $operator operator (optional) |
||
| 120 | * |
||
| 121 | * @return self |
||
| 122 | */ |
||
| 123 | View Code Duplication | public function where($field, $condition = false, $operator = '=') |
|
| 133 | |||
| 134 | /** |
||
| 135 | * Sets the limit for the query. |
||
| 136 | * |
||
| 137 | * @param int $limit |
||
| 138 | * @param int $offset |
||
| 139 | * |
||
| 140 | * @return self |
||
| 141 | */ |
||
| 142 | public function limit($limit, $offset = 0) |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Sets the group by fields for the query. |
||
| 151 | * |
||
| 152 | * @param string|array $fields |
||
| 153 | * @param string $direction |
||
| 154 | * |
||
| 155 | * @return self |
||
| 156 | */ |
||
| 157 | public function groupBy($fields, $direction = false) |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Sets the having conditions for the query. |
||
| 166 | * |
||
| 167 | * @param array|string $field |
||
| 168 | * @param string|bool $condition condition value (optional) |
||
| 169 | * @param string $operator operator (optional) |
||
| 170 | * |
||
| 171 | * @return self |
||
| 172 | */ |
||
| 173 | View Code Duplication | public function having($field, $condition = false, $operator = '=') |
|
| 183 | |||
| 184 | /** |
||
| 185 | * Sets the order for the query. |
||
| 186 | * |
||
| 187 | * @param string|array $fields |
||
| 188 | * @param string $direction |
||
| 189 | * |
||
| 190 | * @return self |
||
| 191 | */ |
||
| 192 | public function orderBy($fields, $direction = false) |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Gets the select statement for the query. |
||
| 201 | * |
||
| 202 | * @return SelectStatement |
||
| 203 | */ |
||
| 204 | public function getSelect() |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Gets the from statement for the query. |
||
| 211 | * |
||
| 212 | * @return FromStatement |
||
| 213 | */ |
||
| 214 | public function getFrom() |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Gets the where statement for the query. |
||
| 221 | * |
||
| 222 | * @return WhereStatement |
||
| 223 | */ |
||
| 224 | public function getWhere() |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Gets the limit statement for the query. |
||
| 231 | * |
||
| 232 | * @return LimitStatement |
||
| 233 | */ |
||
| 234 | public function getLimit() |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Gets the group by statement for the query. |
||
| 241 | * |
||
| 242 | * @return GroupByStatement |
||
| 243 | */ |
||
| 244 | public function getGroupBy() |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Gets the having statement for the query. |
||
| 251 | * |
||
| 252 | * @return HavingStatement |
||
| 253 | */ |
||
| 254 | public function getHaving() |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Gets the order by statement for the query. |
||
| 261 | * |
||
| 262 | * @return OrderByStatement |
||
| 263 | */ |
||
| 264 | public function getOrderBy() |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Generates the raw SQL string for the query. |
||
| 271 | * |
||
| 272 | * @return string |
||
| 273 | */ |
||
| 274 | public function build() |
||
| 291 | } |
||
| 292 |