From 003d4e85385b866457620a0c1e5ca4c7386c5611 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Tue, 21 Nov 2023 16:07:00 +0300 Subject: [PATCH] Improve type annotations --- CHANGELOG.md | 3 +++ src/Command/CommandInterface.php | 2 +- src/Helper/DbArrayHelper.php | 5 ++--- src/Query/BatchQueryResultInterface.php | 5 +++++ src/Query/QueryInterface.php | 2 +- 5 files changed, 12 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 44d5f549f..8239831ed 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,9 @@ - Bug #777: Fix `Query::count()` when it returns an incorrect value if the result is greater than `PHP_INT_MAX` (@Tigrov) +- Enh #779: Specify result type of `QueryInterface::all()`, `CommandInterface::queryAll()` and + `DbArrayHelper::populate()` methods to `array[]` (@vjik) +- Enh #779: Specify populate closure type in `BatchQueryResultInterface` (@vjik) ## 1.2.0 November 12, 2023 diff --git a/src/Command/CommandInterface.php b/src/Command/CommandInterface.php index 9c3244201..3caad60d2 100644 --- a/src/Command/CommandInterface.php +++ b/src/Command/CommandInterface.php @@ -600,7 +600,7 @@ public function query(): DataReaderInterface; * @throws Exception * @throws Throwable If execution failed. * - * @return array All rows of the query result. Each array element is an array representing a row of data. + * @return array[] All rows of the query result. Each array element is an array representing a row of data. * Empty array if the query results in nothing. */ public function queryAll(): array; diff --git a/src/Helper/DbArrayHelper.php b/src/Helper/DbArrayHelper.php index e65e1509e..a5059a73b 100644 --- a/src/Helper/DbArrayHelper.php +++ b/src/Helper/DbArrayHelper.php @@ -333,9 +333,9 @@ public static function multisort( * This method is internally used to convert the data fetched from a database into the format as required by this * query. * - * @param array $rows The raw query result from a database. + * @param array[] $rows The raw query result from a database. * - * @psalm-suppress MixedArrayOffset + * @return array[] */ public static function populate(array $rows, Closure|string|null $indexBy = null): array { @@ -345,7 +345,6 @@ public static function populate(array $rows, Closure|string|null $indexBy = null $result = []; - /** @psalm-var array[][] $row */ foreach ($rows as $row) { /** @psalm-suppress MixedArrayOffset */ $result[self::getValueByPath($row, $indexBy)] = $row; diff --git a/src/Query/BatchQueryResultInterface.php b/src/Query/BatchQueryResultInterface.php index f9f20c16d..585ad50d9 100644 --- a/src/Query/BatchQueryResultInterface.php +++ b/src/Query/BatchQueryResultInterface.php @@ -35,6 +35,8 @@ * ``` * * @extends Iterator + * + * @psalm-type PopulateClosure=Closure(array[],Closure|string|null): mixed */ interface BatchQueryResultInterface extends Iterator { @@ -106,5 +108,8 @@ public function getBatchSize(): int; */ public function batchSize(int $value): self; + /** + * @psalm-param PopulateClosure|null $populateMethod + */ public function setPopulatedMethod(Closure|null $populateMethod = null): self; } diff --git a/src/Query/QueryInterface.php b/src/Query/QueryInterface.php index a13b0d5fd..0bbe54957 100644 --- a/src/Query/QueryInterface.php +++ b/src/Query/QueryInterface.php @@ -45,7 +45,7 @@ public function addParams(array $params): static; * @throws InvalidConfigException * @throws Throwable * - * @return array The query results. If the query results in nothing, it returns an empty array. + * @return array[] The query results. If the query results in nothing, it returns an empty array. */ public function all(): array;