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 |
||
| 23 | class SelectQuery extends AbstractQuery |
||
| 24 | { |
||
| 25 | use Executable, Fetchable, WhereConditions; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var SelectStatement |
||
| 29 | */ |
||
| 30 | protected $select; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var FromStatement |
||
| 34 | */ |
||
| 35 | protected $from; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var WhereStatement |
||
| 39 | */ |
||
| 40 | protected $having; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var OrderStatement |
||
| 44 | */ |
||
| 45 | protected $orderBy; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var OrderStatement |
||
| 49 | */ |
||
| 50 | protected $groupBy; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var LimitStatement |
||
| 54 | */ |
||
| 55 | protected $limit; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var UnionStatement |
||
| 59 | */ |
||
| 60 | protected $union; |
||
| 61 | |||
| 62 | public function __construct() |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Sets the fields to be selected for the query. |
||
| 76 | * |
||
| 77 | * @param array|string $fields fields |
||
| 78 | * |
||
| 79 | * @return self |
||
| 80 | */ |
||
| 81 | public function select($fields) |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Sets the table for the query. |
||
| 90 | * |
||
| 91 | * @param string $table table name |
||
| 92 | * |
||
| 93 | * @return self |
||
| 94 | */ |
||
| 95 | public function from($table) |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Adds a join to the query. |
||
| 104 | * |
||
| 105 | * @param string $table table name |
||
| 106 | * @param string $on ON condition |
||
| 107 | * @param string $using USING columns |
||
| 108 | * @param string $type optional join type if not JOIN |
||
| 109 | * |
||
| 110 | * @return self |
||
| 111 | */ |
||
| 112 | public function join($table, $on = null, $using = null, $type = 'JOIN') |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Sets the limit for the query. |
||
| 121 | * |
||
| 122 | * @param int $limit |
||
| 123 | * @param int $offset |
||
| 124 | * |
||
| 125 | * @return self |
||
| 126 | */ |
||
| 127 | public function limit($limit, $offset = 0) |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Sets the group by fields for the query. |
||
| 136 | * |
||
| 137 | * @param string|array $fields |
||
| 138 | * @param string $direction |
||
| 139 | * |
||
| 140 | * @return self |
||
| 141 | */ |
||
| 142 | public function groupBy($fields, $direction = false) |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Sets the having conditions for the query. |
||
| 151 | * |
||
| 152 | * @param array|string $field |
||
| 153 | * @param string|bool $condition condition value (optional) |
||
| 154 | * @param string $operator operator (optional) |
||
| 155 | * |
||
| 156 | * @return self |
||
| 157 | */ |
||
| 158 | View Code Duplication | public function having($field, $condition = false, $operator = '=') |
|
| 168 | |||
| 169 | /** |
||
| 170 | * Sets the order for the query. |
||
| 171 | * |
||
| 172 | * @param string|array $fields |
||
| 173 | * @param string $direction |
||
| 174 | * |
||
| 175 | * @return self |
||
| 176 | */ |
||
| 177 | public function orderBy($fields, $direction = false) |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Unions another select query with this query. |
||
| 186 | * |
||
| 187 | * @param SelectQuery $query |
||
| 188 | * @param string $type optional union type |
||
| 189 | * |
||
| 190 | * @return self |
||
| 191 | */ |
||
| 192 | public function union(SelectQuery $query, $type = '') |
||
| 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 limit statement for the query. |
||
| 221 | * |
||
| 222 | * @return LimitStatement |
||
| 223 | */ |
||
| 224 | public function getLimit() |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Gets the group by statement for the query. |
||
| 231 | * |
||
| 232 | * @return GroupByStatement |
||
| 233 | */ |
||
| 234 | public function getGroupBy() |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Gets the having statement for the query. |
||
| 241 | * |
||
| 242 | * @return WhereStatement |
||
| 243 | */ |
||
| 244 | public function getHaving() |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Gets the order by statement for the query. |
||
| 251 | * |
||
| 252 | * @return OrderByStatement |
||
| 253 | */ |
||
| 254 | public function getOrderBy() |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Gets the union statement for the query. |
||
| 261 | * |
||
| 262 | * @return UnionStatement |
||
| 263 | */ |
||
| 264 | public function getUnion() |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Generates the raw SQL string for the query. |
||
| 271 | * |
||
| 272 | * @return string |
||
| 273 | */ |
||
| 274 | public function build() |
||
| 303 | |||
| 304 | public function __clone() |
||
| 315 | } |
||
| 316 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..