Skip to content

Commit 5c85223

Browse files
committed
fix(QueryBuilder): Make types even more strict
Signed-off-by: Carl Schwan <carlschwan@kde.org>
1 parent 29b81b5 commit 5c85223

5 files changed

Lines changed: 33 additions & 35 deletions

File tree

apps/dav/lib/DAV/Sharing/SharingMapper.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,6 @@ public function getPrincipalUrisByPrefix(string $resourceType, string $prefix):
164164
->andWhere($query->expr()->eq(
165165
'type',
166166
$query->createNamedParameter($resourceType, IQueryBuilder::PARAM_STR)),
167-
IQueryBuilder::PARAM_STR,
168167
)
169168
->executeQuery();
170169

lib/private/DB/QueryBuilder/QueryBuilder.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,9 @@ public function setFirstResult(int $firstResult): self {
290290

291291
#[Override]
292292
public function getFirstResult(): int {
293-
return $this->queryBuilder->getFirstResult();
293+
$firstResult = $this->queryBuilder->getFirstResult();
294+
assert($firstResult >= 0);
295+
return $firstResult;
294296
}
295297

296298
#[Override]
@@ -306,7 +308,9 @@ public function setMaxResults(?int $maxResults): self {
306308

307309
#[Override]
308310
public function getMaxResults(): ?int {
309-
return $this->queryBuilder->getMaxResults();
311+
$maxResult = $this->queryBuilder->getMaxResults();
312+
assert($maxResult === null || $maxResult > 0);
313+
return $maxResult;
310314
}
311315

312316
#[Override]

lib/private/Share20/DefaultShareProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -968,7 +968,7 @@ private function _getSharedWith(
968968
->setFirstResult(0);
969969

970970
if ($limit !== -1) {
971-
$qb->setMaxResults($limit - count($shares));
971+
$qb->setMaxResults(max($limit - count($shares), 1));
972972
}
973973

974974
// Filter by node if provided

lib/private/User/Manager.php

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
use OCP\User\Events\UserCreatedEvent;
3737
use OCP\UserInterface;
3838
use OCP\Util;
39+
use Override;
3940
use Psr\Log\LoggerInterface;
4041

4142
/**
@@ -728,16 +729,10 @@ public function validateUserId(string $uid, bool $checkDataDirectory = false): v
728729
}
729730
}
730731

731-
/**
732-
* Gets the list of user ids sorted by lastLogin, from most recent to least recent
733-
*
734-
* @param int|null $limit how many users to fetch (default: 25, max: 100)
735-
* @param int $offset from which offset to fetch
736-
* @param string $search search users based on search params
737-
* @return list<string> list of user IDs
738-
*/
732+
#[Override]
739733
public function getLastLoggedInUsers(?int $limit = null, int $offset = 0, string $search = ''): array {
740734
// We can't load all users who already logged in
735+
/** @var int<1, 100> */
741736
$limit = min(100, $limit ?: 25);
742737

743738
$connection = Server::get(IDBConnection::class);

lib/public/DB/QueryBuilder/IQueryBuilder.php

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -264,8 +264,8 @@ public function setParameter(string|int $key, mixed $value, string|null|int $typ
264264
* ));
265265
* </code>
266266
*
267-
* @param array $params The query parameters to set.
268-
* @param array $types The query parameters types to set.
267+
* @param array<string|int, mixed> $params The query parameters to set.
268+
* @param array<string|int, self::PARAM_*> $types The query parameters types to set.
269269
* @return $this This QueryBuilder instance.
270270
*
271271
* @since 8.2.0
@@ -293,7 +293,7 @@ public function getParameter(int|string $key): mixed;
293293
/**
294294
* Gets all defined query parameter types for the query being constructed indexed by parameter index or name.
295295
*
296-
* @return list<self::PARAM_*> The currently defined query parameter types indexed by parameter index or name.
296+
* @return array<string|int, self::PARAM_*> The currently defined query parameter types indexed by parameter index or name.
297297
* @since 8.2.0
298298
*/
299299
public function getParameterTypes(): array;
@@ -311,7 +311,7 @@ public function getParameterType(int|string $key): int|string;
311311
/**
312312
* Sets the position of the first result to retrieve (the "offset").
313313
*
314-
* @param int $firstResult The first result to return.
314+
* @param non-negative-int $firstResult The first result to return.
315315
*
316316
* @return $this This QueryBuilder instance.
317317
* @since 8.2.0
@@ -322,15 +322,15 @@ public function setFirstResult(int $firstResult): self;
322322
* Gets the position of the first result the query object was set to retrieve (the "offset").
323323
* Returns 0 if {@link setFirstResult} was not applied to this QueryBuilder.
324324
*
325-
* @return int The position of the first result.
325+
* @return non-negative-int The position of the first result.
326326
* @since 8.2.0
327327
*/
328328
public function getFirstResult(): int;
329329

330330
/**
331331
* Sets the maximum number of results to retrieve (the "limit").
332332
*
333-
* @param int|null $maxResults The maximum number of results to retrieve.
333+
* @param positive-int|null $maxResults The maximum number of results to retrieve.
334334
* @return $this This QueryBuilder instance.
335335
*
336336
* @since 8.2.0
@@ -341,7 +341,7 @@ public function setMaxResults(?int $maxResults): self;
341341
* Gets the maximum number of results the query object was set to retrieve (the "limit").
342342
* Returns NULL if {@link setMaxResults} was not applied to this query builder.
343343
*
344-
* @return int|null The maximum number of results.
344+
* @return positive-int|null The maximum number of results.
345345
* @since 8.2.0
346346
*/
347347
public function getMaxResults(): ?int;
@@ -357,14 +357,14 @@ public function getMaxResults(): ?int;
357357
* ->leftJoin('u', 'phonenumbers', 'p', 'u.id = p.user_id');
358358
* </code>
359359
*
360-
* @param mixed ...$selects The selection expressions.
360+
* @param string|list<string>|IQueryFunction|ILiteral ...$selects The selection expressions.
361361
* @return $this This QueryBuilder instance.
362362
*
363363
* @since 8.2.0
364364
*
365365
* @psalm-taint-sink sql $selects
366366
*/
367-
public function select(...$selects): self;
367+
public function select(mixed...$selects): self;
368368

369369
/**
370370
* Specifies an item that is to be returned with a different name in the query result.
@@ -416,14 +416,14 @@ public function selectDistinct(string|array $select): self;
416416
* ->leftJoin('u', 'phonenumbers', 'u.id = p.user_id');
417417
* </code>
418418
*
419-
* @param mixed ...$select The selection expression.
419+
* @param string|IParameter|IQueryFunction|ILiteral|list<string> ...$select The selection expression.
420420
* @return $this This QueryBuilder instance.
421421
*
422422
* @since 8.2.0
423423
*
424424
* @psalm-taint-sink sql $select
425425
*/
426-
public function addSelect(...$select): self;
426+
public function addSelect(mixed ...$select): self;
427427

428428
/**
429429
* Turns the query being built into a bulk delete query that ranges over
@@ -659,14 +659,14 @@ public function set(string $key, ILiteral|IParameter|IQueryFunction|string $valu
659659
* ->where($or);
660660
* </code>
661661
*
662-
* @param mixed $predicates The restriction predicates.
662+
* @param string|IParameter|IQueryFunction|ILiteral|ICompositeExpression $predicates The restriction predicates.
663663
* @return $this This QueryBuilder instance.
664664
*
665665
* @since 8.2.0
666666
*
667667
* @psalm-taint-sink sql $predicates
668668
*/
669-
public function where(...$predicates): self;
669+
public function where(mixed ...$predicates): self;
670670

671671
/**
672672
* Adds one or more restrictions to the query results, forming a logical
@@ -680,15 +680,15 @@ public function where(...$predicates): self;
680680
* ->andWhere('u.is_active = 1');
681681
* </code>
682682
*
683-
* @param mixed ...$where The query restrictions.
683+
* @param string|IParameter|IQueryFunction|ILiteral|ICompositeExpression ...$where The query restrictions.
684684
* @return $this This QueryBuilder instance.
685685
*
686686
* @see where()
687687
* @since 8.2.0
688688
*
689689
* @psalm-taint-sink sql $where
690690
*/
691-
public function andWhere(...$where): self;
691+
public function andWhere(mixed ...$where): self;
692692

693693
/**
694694
* Adds one or more restrictions to the query results, forming a logical
@@ -702,15 +702,15 @@ public function andWhere(...$where): self;
702702
* ->orWhere('u.id = 2');
703703
* </code>
704704
*
705-
* @param mixed ...$where The WHERE statement.
705+
* @param string|IParameter|IQueryFunction|ILiteral|ICompositeExpression ...$where The WHERE statement.
706706
* @return $this This QueryBuilder instance.
707707
*
708708
* @see where()
709709
* @since 8.2.0
710710
*
711711
* @psalm-taint-sink sql $where
712712
*/
713-
public function orWhere(...$where): self;
713+
public function orWhere(mixed ...$where): self;
714714

715715
/**
716716
* Specifies a grouping over the results of the query.
@@ -723,14 +723,14 @@ public function orWhere(...$where): self;
723723
* ->groupBy('u.id');
724724
* </code>
725725
*
726-
* @param mixed ...$groupBys The grouping expression.
726+
* @param string|IParameter|IQueryFunction|ILiteral|list<string> ...$groupBys The grouping expression.
727727
* @return $this This QueryBuilder instance.
728728
*
729729
* @since 8.2.0
730730
*
731731
* @psalm-taint-sink sql $groupBys
732732
*/
733-
public function groupBy(...$groupBys): self;
733+
public function groupBy(mixed ...$groupBys): self;
734734

735735
/**
736736
* Adds a grouping expression to the query.
@@ -743,14 +743,14 @@ public function groupBy(...$groupBys): self;
743743
* ->addGroupBy('u.createdAt')
744744
* </code>
745745
*
746-
* @param mixed ...$groupBy The grouping expression.
746+
* @param string|IParameter|IQueryFunction|ILiteral|list<string> ...$groupBy The grouping expression.
747747
* @return $this This QueryBuilder instance.
748748
*
749749
* @since 8.2.0
750750
*
751751
* @psalm-taint-sink sql $groupby
752752
*/
753-
public function addGroupBy(...$groupBy): self;
753+
public function addGroupBy(mixed ...$groupBy): self;
754754

755755
/**
756756
* Sets a value for a column in an insert query.
@@ -805,14 +805,14 @@ public function values(array $values): self;
805805
* Specifies a restriction over the groups of the query.
806806
* Replaces any previous having restrictions, if any.
807807
*
808-
* @param mixed ...$having The restriction over the groups.
808+
* @param string|IParameter|IQueryFunction|ILiteral ...$having The restriction over the groups.
809809
* @return $this This QueryBuilder instance.
810810
*
811811
* @since 8.2.0
812812
*
813813
* @psalm-taint-sink sql $having
814814
*/
815-
public function having(...$having): self;
815+
public function having(mixed ...$having): self;
816816

817817
/**
818818
* Adds a restriction over the groups of the query, forming a logical

0 commit comments

Comments
 (0)