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 |
||
| 24 | class SelectQuery extends AbstractQuery |
||
| 25 | { |
||
| 26 | use Executable, Fetchable, OrderBy, WhereConditions; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var SelectStatement |
||
| 30 | */ |
||
| 31 | protected $select; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var FromStatement |
||
| 35 | */ |
||
| 36 | protected $from; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var WhereStatement |
||
| 40 | */ |
||
| 41 | protected $having; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var OrderStatement |
||
| 45 | */ |
||
| 46 | protected $groupBy; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var LimitStatement |
||
| 50 | */ |
||
| 51 | protected $limit; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var UnionStatement |
||
| 55 | */ |
||
| 56 | protected $union; |
||
| 57 | |||
| 58 | public function __construct() |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Sets the fields to be selected for the query. |
||
| 72 | * |
||
| 73 | * @param array|string $fields fields |
||
| 74 | * |
||
| 75 | * @return self |
||
| 76 | */ |
||
| 77 | public function select($fields) |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Sets the table for the query. |
||
| 86 | * |
||
| 87 | * @param string $table table name |
||
| 88 | * |
||
| 89 | * @return self |
||
| 90 | */ |
||
| 91 | public function from($table) |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Adds a join to the query. |
||
| 100 | * |
||
| 101 | * @param string $table table name |
||
| 102 | * @param string $on ON condition |
||
| 103 | * @param string $using USING columns |
||
| 104 | * @param string $type optional join type if not JOIN |
||
| 105 | * |
||
| 106 | * @return self |
||
| 107 | */ |
||
| 108 | public function join($table, $on = null, $using = null, $type = 'JOIN') |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Sets the limit for the query. |
||
| 117 | * |
||
| 118 | * @param int $limit |
||
| 119 | * @param int $offset |
||
| 120 | * |
||
| 121 | * @return self |
||
| 122 | */ |
||
| 123 | public function limit($limit, $offset = 0) |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Sets the group by fields for the query. |
||
| 132 | * |
||
| 133 | * @param string|array $fields |
||
| 134 | * @param string $direction |
||
| 135 | * |
||
| 136 | * @return self |
||
| 137 | */ |
||
| 138 | public function groupBy($fields, $direction = false) |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Sets the having conditions for the query. |
||
| 147 | * |
||
| 148 | * @param array|string $field |
||
| 149 | * @param string|bool $condition condition value (optional) |
||
| 150 | * @param string $operator operator (optional) |
||
| 151 | * |
||
| 152 | * @return self |
||
| 153 | */ |
||
| 154 | View Code Duplication | public function having($field, $condition = false, $operator = '=') |
|
| 164 | |||
| 165 | /** |
||
| 166 | * Unions another select query with this query. |
||
| 167 | * |
||
| 168 | * @param SelectQuery $query |
||
| 169 | * @param string $type optional union type |
||
| 170 | * |
||
| 171 | * @return self |
||
| 172 | */ |
||
| 173 | public function union(SelectQuery $query, $type = '') |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Gets the select statement for the query. |
||
| 182 | * |
||
| 183 | * @return SelectStatement |
||
| 184 | */ |
||
| 185 | public function getSelect() |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Gets the from statement for the query. |
||
| 192 | * |
||
| 193 | * @return FromStatement |
||
| 194 | */ |
||
| 195 | public function getFrom() |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Gets the limit statement for the query. |
||
| 202 | * |
||
| 203 | * @return LimitStatement |
||
| 204 | */ |
||
| 205 | public function getLimit() |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Gets the group by statement for the query. |
||
| 212 | * |
||
| 213 | * @return GroupByStatement |
||
| 214 | */ |
||
| 215 | public function getGroupBy() |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Gets the having statement for the query. |
||
| 222 | * |
||
| 223 | * @return WhereStatement |
||
| 224 | */ |
||
| 225 | public function getHaving() |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Gets the union statement for the query. |
||
| 232 | * |
||
| 233 | * @return UnionStatement |
||
| 234 | */ |
||
| 235 | public function getUnion() |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Generates the raw SQL string for the query. |
||
| 242 | * |
||
| 243 | * @return string |
||
| 244 | */ |
||
| 245 | public function build() |
||
| 274 | |||
| 275 | public function __clone() |
||
| 286 | } |
||
| 287 |
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..