1 | <?php |
||
19 | class Query |
||
20 | { |
||
21 | const DEFAULT_LIMIT = 100; |
||
22 | const MAX_LIMIT = 1000; |
||
23 | |||
24 | /** |
||
25 | * @var Model|string |
||
26 | */ |
||
27 | private $model; |
||
28 | |||
29 | /** |
||
30 | * @var array |
||
31 | */ |
||
32 | private $joins; |
||
33 | |||
34 | /** |
||
35 | * @var array |
||
36 | */ |
||
37 | private $eagerLoaded; |
||
38 | |||
39 | /** |
||
40 | * @var array |
||
41 | */ |
||
42 | private $where; |
||
43 | |||
44 | /** |
||
45 | * @var int |
||
46 | */ |
||
47 | private $limit; |
||
48 | |||
49 | /** |
||
50 | * @var int |
||
51 | */ |
||
52 | private $start; |
||
53 | |||
54 | /** |
||
55 | * @var array |
||
56 | */ |
||
57 | private $sort; |
||
58 | |||
59 | /** |
||
60 | * @param Model|string $model |
||
61 | */ |
||
62 | public function __construct($model = '') |
||
63 | { |
||
64 | $this->model = $model; |
||
65 | $this->joins = []; |
||
66 | $this->eagerLoaded = []; |
||
67 | $this->where = []; |
||
68 | $this->start = 0; |
||
69 | $this->limit = self::DEFAULT_LIMIT; |
||
70 | $this->sort = []; |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * Gets the model class associated with this query. |
||
75 | * |
||
76 | * @return Model|string |
||
77 | */ |
||
78 | public function getModel() |
||
79 | { |
||
80 | return $this->model; |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * Sets the limit for this query. |
||
85 | * |
||
86 | * @return $this |
||
87 | */ |
||
88 | public function limit(int $limit) |
||
94 | |||
95 | /** |
||
96 | * Gets the limit for this query. |
||
97 | */ |
||
98 | public function getLimit(): int |
||
102 | |||
103 | /** |
||
104 | * Sets the start offset. |
||
105 | * |
||
106 | * @return $this |
||
107 | */ |
||
108 | public function start(int $start) |
||
114 | |||
115 | /** |
||
116 | * Gets the start offset. |
||
117 | */ |
||
118 | public function getStart(): int |
||
122 | |||
123 | /** |
||
124 | * Sets the sort pattern for the query. |
||
125 | * |
||
126 | * @param array|string $sort |
||
127 | * |
||
128 | * @return $this |
||
129 | */ |
||
130 | public function sort($sort) |
||
155 | |||
156 | /** |
||
157 | * Gets the sort parameters. |
||
158 | */ |
||
159 | public function getSort(): array |
||
163 | |||
164 | /** |
||
165 | * Sets the where parameters. |
||
166 | * Accepts the following forms: |
||
167 | * i) where(['name' => 'Bob']) |
||
168 | * ii) where('name', 'Bob') |
||
169 | * iii) where('balance', 100, '>') |
||
170 | * iv) where('balance > 100'). |
||
171 | * |
||
172 | * @param array|string $where |
||
173 | * @param mixed $value optional value |
||
174 | * @param string|null $condition optional condition |
||
175 | * |
||
176 | * @return $this |
||
177 | */ |
||
178 | public function where($where, $value = null, $condition = null) |
||
199 | |||
200 | /** |
||
201 | * Gets the where parameters. |
||
202 | */ |
||
203 | public function getWhere(): array |
||
207 | |||
208 | /** |
||
209 | * Adds a join to the query. Matches a property on this model |
||
210 | * to the ID of the model we are joining. |
||
211 | * |
||
212 | * @param string $model model being joined |
||
213 | * @param string $column name of local property |
||
214 | * |
||
215 | * @return $this |
||
216 | */ |
||
217 | public function join($model, string $column, string $foreignKey) |
||
223 | |||
224 | /** |
||
225 | * Gets the joins. |
||
226 | */ |
||
227 | public function getJoins(): array |
||
231 | |||
232 | /** |
||
233 | * Marks a relationship property on the model that should be eager loaded. |
||
234 | * |
||
235 | * @param string $k local property containing the relationship |
||
236 | * |
||
237 | * @return $this |
||
238 | */ |
||
239 | public function with(string $k) |
||
247 | |||
248 | /** |
||
249 | * Gets the relationship properties that are going to be eager-loaded. |
||
250 | */ |
||
251 | public function getWith(): array |
||
255 | |||
256 | /** |
||
257 | * Executes the query against the model's driver. |
||
258 | * |
||
259 | * @return array results |
||
260 | */ |
||
261 | public function execute(): array |
||
270 | |||
271 | /** |
||
272 | * Creates an iterator for a search. |
||
273 | * |
||
274 | * @return Iterator |
||
275 | */ |
||
276 | public function all() |
||
280 | |||
281 | /** |
||
282 | * Executes the query against the model's driver and returns the first result. |
||
283 | * |
||
284 | * @return array|Model|null when $limit = 1, returns a single model or null, otherwise returns an array |
||
285 | */ |
||
286 | public function first(int $limit = 1) |
||
296 | |||
297 | /** |
||
298 | * Gets the number of models matching the query. |
||
299 | */ |
||
300 | public function count(): int |
||
307 | |||
308 | /** |
||
309 | * Gets the sum of a property matching the query. |
||
310 | * |
||
311 | * @return number |
||
312 | */ |
||
313 | public function sum(string $property) |
||
320 | |||
321 | /** |
||
322 | * Gets the average of a property matching the query. |
||
323 | * |
||
324 | * @return number |
||
325 | */ |
||
326 | public function average(string $property) |
||
333 | |||
334 | /** |
||
335 | * Gets the max of a property matching the query. |
||
336 | * |
||
337 | * @return number |
||
338 | */ |
||
339 | public function max(string $property) |
||
346 | |||
347 | /** |
||
348 | * Gets the min of a property matching the query. |
||
349 | * |
||
350 | * @return number |
||
351 | */ |
||
352 | public function min(string $property) |
||
359 | |||
360 | /** |
||
361 | * Updates all of the models matched by this query. |
||
362 | * |
||
363 | * @todo should be optimized to be done in a single call to the data layer |
||
364 | * |
||
365 | * @param array $params key-value update parameters |
||
366 | * |
||
367 | * @return int # of models updated |
||
368 | */ |
||
369 | public function set(array $params): int |
||
379 | |||
380 | /** |
||
381 | * Deletes all of the models matched by this query. |
||
382 | * |
||
383 | * @todo should be optimized to be done in a single call to the data layer |
||
384 | * |
||
385 | * @return int # of models deleted |
||
386 | */ |
||
387 | public function delete(): int |
||
397 | } |
||
398 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.