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 | * Sets the where conditions for 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 | * Sets the limit for the query. |
||
| 145 | * |
||
| 146 | * @param int $limit |
||
| 147 | * @param int $offset |
||
| 148 | * |
||
| 149 | * @return self |
||
| 150 | */ |
||
| 151 | public function limit($limit, $offset = 0) |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Sets the group by fields for the query. |
||
| 160 | * |
||
| 161 | * @param string|array $fields |
||
| 162 | * @param string $direction |
||
| 163 | * |
||
| 164 | * @return self |
||
| 165 | */ |
||
| 166 | public function groupBy($fields, $direction = false) |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Sets the having conditions for the query. |
||
| 175 | * |
||
| 176 | * @param array|string $field |
||
| 177 | * @param string|bool $condition condition value (optional) |
||
| 178 | * @param string $operator operator (optional) |
||
| 179 | * |
||
| 180 | * @return self |
||
| 181 | */ |
||
| 182 | View Code Duplication | public function having($field, $condition = false, $operator = '=') |
|
| 192 | |||
| 193 | /** |
||
| 194 | * Sets the order for the query. |
||
| 195 | * |
||
| 196 | * @param string|array $fields |
||
| 197 | * @param string $direction |
||
| 198 | * |
||
| 199 | * @return self |
||
| 200 | */ |
||
| 201 | public function orderBy($fields, $direction = false) |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Unions another select query with this query. |
||
| 210 | * |
||
| 211 | * @param SelectQuery $query |
||
| 212 | * @param string $type optional union type |
||
| 213 | * |
||
| 214 | * @return self |
||
| 215 | */ |
||
| 216 | public function union(SelectQuery $query, $type = false) |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Gets the select statement for the query. |
||
| 225 | * |
||
| 226 | * @return SelectStatement |
||
| 227 | */ |
||
| 228 | public function getSelect() |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Gets the from statement for the query. |
||
| 235 | * |
||
| 236 | * @return FromStatement |
||
| 237 | */ |
||
| 238 | public function getFrom() |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Gets the where statement for the query. |
||
| 245 | * |
||
| 246 | * @return WhereStatement |
||
| 247 | */ |
||
| 248 | public function getWhere() |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Gets the limit statement for the query. |
||
| 255 | * |
||
| 256 | * @return LimitStatement |
||
| 257 | */ |
||
| 258 | public function getLimit() |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Gets the group by statement for the query. |
||
| 265 | * |
||
| 266 | * @return GroupByStatement |
||
| 267 | */ |
||
| 268 | public function getGroupBy() |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Gets the having statement for the query. |
||
| 275 | * |
||
| 276 | * @return HavingStatement |
||
| 277 | */ |
||
| 278 | public function getHaving() |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Gets the order by statement for the query. |
||
| 285 | * |
||
| 286 | * @return OrderByStatement |
||
| 287 | */ |
||
| 288 | public function getOrderBy() |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Gets the union statement for the query. |
||
| 295 | * |
||
| 296 | * @return UnionStatement |
||
| 297 | */ |
||
| 298 | public function getUnion() |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Generates the raw SQL string for the query. |
||
| 305 | * |
||
| 306 | * @return string |
||
| 307 | */ |
||
| 308 | public function build() |
||
| 328 | |||
| 329 | public function __clone() |
||
| 340 | } |
||
| 341 |