From 4064b5e76e455f80321cd0c7d0c8704a6061d2ab Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Wed, 5 Aug 2026 11:35:46 +0200 Subject: [PATCH] Query: Fix some phpstan errors related to generics * Query::setModel() now implicitly changes the type of the query * Query::createSubQuery() returns a proper typed result * Query::derive() properly signals it does not know the result's type --- src/Query.php | 17 ++++++++++++----- src/ResultSet.php | 1 + 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/Query.php b/src/Query.php index b2af92c..d08b18f 100644 --- a/src/Query.php +++ b/src/Query.php @@ -158,7 +158,10 @@ public function getModel() /** * Set the model to query * - * @param TRow $model + * @template TNew of Model + * @phpstan-self-out static + * + * @param TNew $model * * @return $this */ @@ -577,7 +580,7 @@ public function createHydrator(): Hydrator * @param string $relation * @param TRow $source * - * @return static + * @return static<*> * * @throws InvalidArgumentException If the relation with the given name does not exist */ @@ -594,12 +597,14 @@ public function derive($relation, Model $source): static /** * Create a sub-query linked to rows of this query * - * @param Model $target The model to query + * @template TTarget of Model + * + * @param TTarget $target The model to query * @param string $targetPath The target's absolute relation path * @param ?TRow $from The source model * @param bool $link Whether the query should be linked to the parent query * - * @return static + * @return static */ public function createSubQuery(Model $target, string $targetPath, ?Model $from = null, bool $link = true): static { @@ -617,9 +622,11 @@ public function createSubQuery(Model $target, string $targetPath, ?Model $from = $subQuery->utilize($sourcePath); // TODO: Don't join if there's a matching foreign key if (! $link) { - return $subQuery->columns(array_map(function ($keyName) use ($sourcePath) { + $subQuery->columns(array_map(function ($keyName) use ($sourcePath) { return "$sourcePath.$keyName"; }, (array) $subQueryTarget->getKeyName())); + + return $subQuery; } // TODO: Should be done by the caller. Though, that's not possible until we've got a filter abstraction diff --git a/src/ResultSet.php b/src/ResultSet.php index 3ca7160..55d581f 100644 --- a/src/ResultSet.php +++ b/src/ResultSet.php @@ -46,6 +46,7 @@ public function __construct(Traversable $traversable, ?int $limit = null) * Create a new result set from the given query * * @template TQueryRow of Model + * * @param Query $query * * @return static