Skip to content

Commit 2cae328

Browse files
authored
feat(SelectQuery): refine Psalm integer types on query bounds (#257)
Express numeric bounds via Psalm types so static analysis flags invalid values at call sites: - runChunks() chunk size as int<1, max> (a non-positive value never advances the offset and spins in an endless loop); - getLimit() / getOffset() as int<0, max>|null; - count() as int<0, max> (a row count is never negative). Also drop the redundant `psalm-` prefix from the existing non-empty-string column/method hints: plain `@param` advanced types are now broadly supported (Psalm, PHPStan, PhpStorm). Return-type refinements only; LessSpecificReturnStatement / MoreSpecificReturnType are already suppressed project-wide, and setter params are left intact to preserve the PaginableInterface contract.
1 parent 38c3403 commit 2cae328

1 file changed

Lines changed: 17 additions & 7 deletions

File tree

‎src/Query/SelectQuery.php‎

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,9 @@ public function limit(?int $limit = null): self
255255
return $this;
256256
}
257257

258+
/**
259+
* @return int<0, max>|null
260+
*/
258261
public function getLimit(): ?int
259262
{
260263
return $this->limit;
@@ -271,6 +274,9 @@ public function offset(?int $offset = null): self
271274
return $this;
272275
}
273276

277+
/**
278+
* @return int<0, max>|null
279+
*/
274280
public function getOffset(): ?int
275281
{
276282
return $this->offset;
@@ -294,6 +300,8 @@ public function run(): StatementInterface
294300
*
295301
* You must return FALSE from walk function to stop chunking.
296302
*
303+
* @param int<1, max> $limit Chunk size.
304+
*
297305
* @throws \Throwable
298306
*/
299307
public function runChunks(int $limit, callable $callback): void
@@ -324,7 +332,9 @@ public function runChunks(int $limit, callable $callback): void
324332
/**
325333
* Count number of rows in query. Limit, offset, order by, group by values will be ignored.
326334
*
327-
* @psalm-param non-empty-string $column Column to count by (every column by default).
335+
* @param non-empty-string $column Column to count by (every column by default).
336+
*
337+
* @return int<0, max>
328338
*/
329339
public function count(string $column = '*', bool $distinct = false): int
330340
{
@@ -346,31 +356,31 @@ public function count(string $column = '*', bool $distinct = false): int
346356
}
347357

348358
/**
349-
* @psalm-param non-empty-string $column
359+
* @param non-empty-string $column
350360
*/
351361
public function avg(string $column): mixed
352362
{
353363
return $this->runAggregate('AVG', $column);
354364
}
355365

356366
/**
357-
* @psalm-param non-empty-string $column
367+
* @param non-empty-string $column
358368
*/
359369
public function max(string $column): mixed
360370
{
361371
return $this->runAggregate('MAX', $column);
362372
}
363373

364374
/**
365-
* @psalm-param non-empty-string $column
375+
* @param non-empty-string $column
366376
*/
367377
public function min(string $column): mixed
368378
{
369379
return $this->runAggregate('MIN', $column);
370380
}
371381

372382
/**
373-
* @psalm-param non-empty-string $column
383+
* @param non-empty-string $column
374384
*/
375385
public function sum(string $column): mixed
376386
{
@@ -436,8 +446,8 @@ private function addOrder(string|FragmentInterface $field, ?string $order): self
436446
}
437447

438448
/**
439-
* @psalm-param non-empty-string $method
440-
* @psalm-param non-empty-string $column
449+
* @param non-empty-string $method
450+
* @param non-empty-string $column
441451
*/
442452
private function runAggregate(string $method, string $column): mixed
443453
{

0 commit comments

Comments
 (0)