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 |
||
| 21 | class SelectQuery extends Query |
||
| 22 | { |
||
| 23 | use Executable, Fetchable; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var SelectStatement |
||
| 27 | */ |
||
| 28 | protected $select; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var FromStatement |
||
| 32 | */ |
||
| 33 | protected $from; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var WhereStatement |
||
| 37 | */ |
||
| 38 | protected $where; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var WhereStatement |
||
| 42 | */ |
||
| 43 | protected $having; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var OrderStatement |
||
| 47 | */ |
||
| 48 | protected $orderBy; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var OrderStatement |
||
| 52 | */ |
||
| 53 | protected $groupBy; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var LimitStatement |
||
| 57 | */ |
||
| 58 | protected $limit; |
||
| 59 | |||
| 60 | public function __construct() |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Sets the fields to be selected for the query. |
||
| 73 | * |
||
| 74 | * @param array|string $fields fields |
||
| 75 | * |
||
| 76 | * @return self |
||
| 77 | */ |
||
| 78 | public function select($fields) |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Sets the table for the query. |
||
| 87 | * |
||
| 88 | * @param string $table table name |
||
| 89 | * |
||
| 90 | * @return self |
||
| 91 | */ |
||
| 92 | public function from($table) |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Adds a join to the query. |
||
| 101 | * |
||
| 102 | * @param string $table table name |
||
| 103 | * @param string $on ON condition |
||
| 104 | * @param string $using USING columns |
||
| 105 | * @param string $type optional join type if not JOIN |
||
| 106 | * |
||
| 107 | * @return self |
||
| 108 | */ |
||
| 109 | public function join($table, $on = null, $using = null, $type = 'JOIN') |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Sets the where conditions for the query. |
||
| 118 | * |
||
| 119 | * @param array|string $field |
||
| 120 | * @param string $condition condition value (optional) |
||
| 121 | * @param string $operator operator (optional) |
||
| 122 | * |
||
| 123 | * @return self |
||
| 124 | */ |
||
| 125 | View Code Duplication | public function where($field, $condition = false, $operator = '=') |
|
| 135 | |||
| 136 | /** |
||
| 137 | * Sets the limit for the query. |
||
| 138 | * |
||
| 139 | * @param int $limit |
||
| 140 | * @param int $offset |
||
| 141 | * |
||
| 142 | * @return self |
||
| 143 | */ |
||
| 144 | public function limit($limit, $offset = 0) |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Sets the group by fields for the query. |
||
| 153 | * |
||
| 154 | * @param string|array $fields |
||
| 155 | * @param string $direction |
||
| 156 | * |
||
| 157 | * @return self |
||
| 158 | */ |
||
| 159 | public function groupBy($fields, $direction = false) |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Sets the having conditions for the query. |
||
| 168 | * |
||
| 169 | * @param array|string $field |
||
| 170 | * @param string|bool $condition condition value (optional) |
||
| 171 | * @param string $operator operator (optional) |
||
| 172 | * |
||
| 173 | * @return self |
||
| 174 | */ |
||
| 175 | View Code Duplication | public function having($field, $condition = false, $operator = '=') |
|
| 185 | |||
| 186 | /** |
||
| 187 | * Sets the order for the query. |
||
| 188 | * |
||
| 189 | * @param string|array $fields |
||
| 190 | * @param string $direction |
||
| 191 | * |
||
| 192 | * @return self |
||
| 193 | */ |
||
| 194 | public function orderBy($fields, $direction = false) |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Gets the select statement for the query. |
||
| 203 | * |
||
| 204 | * @return SelectStatement |
||
| 205 | */ |
||
| 206 | public function getSelect() |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Gets the from statement for the query. |
||
| 213 | * |
||
| 214 | * @return FromStatement |
||
| 215 | */ |
||
| 216 | public function getFrom() |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Gets the where statement for the query. |
||
| 223 | * |
||
| 224 | * @return WhereStatement |
||
| 225 | */ |
||
| 226 | public function getWhere() |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Gets the limit statement for the query. |
||
| 233 | * |
||
| 234 | * @return LimitStatement |
||
| 235 | */ |
||
| 236 | public function getLimit() |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Gets the group by statement for the query. |
||
| 243 | * |
||
| 244 | * @return GroupByStatement |
||
| 245 | */ |
||
| 246 | public function getGroupBy() |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Gets the having statement for the query. |
||
| 253 | * |
||
| 254 | * @return HavingStatement |
||
| 255 | */ |
||
| 256 | public function getHaving() |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Gets the order by statement for the query. |
||
| 263 | * |
||
| 264 | * @return OrderByStatement |
||
| 265 | */ |
||
| 266 | public function getOrderBy() |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Generates the raw SQL string for the query. |
||
| 273 | * |
||
| 274 | * @return string |
||
| 275 | */ |
||
| 276 | public function build() |
||
| 293 | } |
||
| 294 |