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 |
||
| 22 | class SelectQuery extends AbstractQuery |
||
| 23 | { |
||
| 24 | use Executable, Fetchable; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var SelectStatement |
||
| 28 | */ |
||
| 29 | protected $select; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var FromStatement |
||
| 33 | */ |
||
| 34 | protected $from; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var WhereStatement |
||
| 38 | */ |
||
| 39 | protected $where; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var WhereStatement |
||
| 43 | */ |
||
| 44 | protected $having; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var OrderStatement |
||
| 48 | */ |
||
| 49 | protected $orderBy; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var OrderStatement |
||
| 53 | */ |
||
| 54 | protected $groupBy; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var LimitStatement |
||
| 58 | */ |
||
| 59 | protected $limit; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var UnionStatement |
||
| 63 | */ |
||
| 64 | protected $union; |
||
| 65 | |||
| 66 | public function __construct() |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Sets the fields to be selected for the query. |
||
| 80 | * |
||
| 81 | * @param array|string $fields fields |
||
| 82 | * |
||
| 83 | * @return self |
||
| 84 | */ |
||
| 85 | public function select($fields) |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Sets the table for the query. |
||
| 94 | * |
||
| 95 | * @param string $table table name |
||
| 96 | * |
||
| 97 | * @return self |
||
| 98 | */ |
||
| 99 | public function from($table) |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Adds a join to the query. |
||
| 108 | * |
||
| 109 | * @param string $table table name |
||
| 110 | * @param string $on ON condition |
||
| 111 | * @param string $using USING columns |
||
| 112 | * @param string $type optional join type if not JOIN |
||
| 113 | * |
||
| 114 | * @return self |
||
| 115 | */ |
||
| 116 | public function join($table, $on = null, $using = null, $type = 'JOIN') |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Adds a where condition to the query. |
||
| 125 | * |
||
| 126 | * @param array|string $field |
||
| 127 | * @param string $condition condition value (optional) |
||
| 128 | * @param string $operator operator (optional) |
||
| 129 | * |
||
| 130 | * @return self |
||
| 131 | */ |
||
| 132 | View Code Duplication | public function where($field, $condition = false, $operator = '=') |
|
| 142 | |||
| 143 | /** |
||
| 144 | * Adds a where not condition to the query. |
||
| 145 | * |
||
| 146 | * @param string $field |
||
| 147 | * @param string $condition condition value (optional) |
||
| 148 | * |
||
| 149 | * @return self |
||
| 150 | */ |
||
| 151 | public function not($field, $condition = true) |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Adds a where between condition to the query. |
||
| 160 | * |
||
| 161 | * @param string $field |
||
| 162 | * @param mixed $a first between value |
||
| 163 | * @param mixed $b second between value |
||
| 164 | * |
||
| 165 | * @return self |
||
| 166 | */ |
||
| 167 | public function between($field, $a, $b) |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Adds a where not between condition to the query. |
||
| 176 | * |
||
| 177 | * @param string $field |
||
| 178 | * @param mixed $a first between value |
||
| 179 | * @param mixed $b second between value |
||
| 180 | * |
||
| 181 | * @return self |
||
| 182 | */ |
||
| 183 | public function notBetween($field, $a, $b) |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Sets the limit for the query. |
||
| 192 | * |
||
| 193 | * @param int $limit |
||
| 194 | * @param int $offset |
||
| 195 | * |
||
| 196 | * @return self |
||
| 197 | */ |
||
| 198 | public function limit($limit, $offset = 0) |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Sets the group by fields for the query. |
||
| 207 | * |
||
| 208 | * @param string|array $fields |
||
| 209 | * @param string $direction |
||
| 210 | * |
||
| 211 | * @return self |
||
| 212 | */ |
||
| 213 | public function groupBy($fields, $direction = false) |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Sets the having conditions for the query. |
||
| 222 | * |
||
| 223 | * @param array|string $field |
||
| 224 | * @param string|bool $condition condition value (optional) |
||
| 225 | * @param string $operator operator (optional) |
||
| 226 | * |
||
| 227 | * @return self |
||
| 228 | */ |
||
| 229 | View Code Duplication | public function having($field, $condition = false, $operator = '=') |
|
| 239 | |||
| 240 | /** |
||
| 241 | * Sets the order for the query. |
||
| 242 | * |
||
| 243 | * @param string|array $fields |
||
| 244 | * @param string $direction |
||
| 245 | * |
||
| 246 | * @return self |
||
| 247 | */ |
||
| 248 | public function orderBy($fields, $direction = false) |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Unions another select query with this query. |
||
| 257 | * |
||
| 258 | * @param SelectQuery $query |
||
| 259 | * @param string $type optional union type |
||
| 260 | * |
||
| 261 | * @return self |
||
| 262 | */ |
||
| 263 | public function union(SelectQuery $query, $type = false) |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Gets the select statement for the query. |
||
| 272 | * |
||
| 273 | * @return SelectStatement |
||
| 274 | */ |
||
| 275 | public function getSelect() |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Gets the from statement for the query. |
||
| 282 | * |
||
| 283 | * @return FromStatement |
||
| 284 | */ |
||
| 285 | public function getFrom() |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Gets the where statement for the query. |
||
| 292 | * |
||
| 293 | * @return WhereStatement |
||
| 294 | */ |
||
| 295 | public function getWhere() |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Gets the limit statement for the query. |
||
| 302 | * |
||
| 303 | * @return LimitStatement |
||
| 304 | */ |
||
| 305 | public function getLimit() |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Gets the group by statement for the query. |
||
| 312 | * |
||
| 313 | * @return GroupByStatement |
||
| 314 | */ |
||
| 315 | public function getGroupBy() |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Gets the having statement for the query. |
||
| 322 | * |
||
| 323 | * @return HavingStatement |
||
| 324 | */ |
||
| 325 | public function getHaving() |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Gets the order by statement for the query. |
||
| 332 | * |
||
| 333 | * @return OrderByStatement |
||
| 334 | */ |
||
| 335 | public function getOrderBy() |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Gets the union statement for the query. |
||
| 342 | * |
||
| 343 | * @return UnionStatement |
||
| 344 | */ |
||
| 345 | public function getUnion() |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Generates the raw SQL string for the query. |
||
| 352 | * |
||
| 353 | * @return string |
||
| 354 | */ |
||
| 355 | public function build() |
||
| 375 | |||
| 376 | public function __clone() |
||
| 387 | } |
||
| 388 |