Skip to content

Commit 78f33cc

Browse files
authored
Improve psalm type for "FROM" property (#1077)
x
1 parent 72c5e19 commit 78f33cc

7 files changed

Lines changed: 22 additions & 7 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@
160160
- Enh #1065: Allow to use table name as a column prefix in update queries (@Tigrov)
161161
- Enh #1068: Quote string values as column names when join condition is an associative array (@Tigrov)
162162
- Chg #1070: Change "IndexBy" closure signature to `Closure(array|object):int|string` (@vjik)
163-
- Enh #1072: Improve psalm types (@vjik)
163+
- Enh #1072, #1077: Improve psalm types (@vjik)
164164
- New #1074: Add `ConnectionProvider` class (@Tigrov)
165165
- Chg #1075: Rename `Query::$join` property to `$joins` (@vjik)
166166
- New #1076: Allow to use expressions as table name or condition in "join" query methods (@vjik)

src/Command/CommandInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
* A command instance is usually created by calling {@see ConnectionInterface::createCommand}.
3232
*
3333
* @psalm-import-type ParamsType from ConnectionInterface
34+
* @psalm-import-type RawFrom from QueryInterface
3435
* @psalm-import-type BatchValues from DMLQueryBuilderInterface
3536
*/
3637
interface CommandInterface
@@ -835,6 +836,7 @@ public function truncateTable(string $table): static;
835836
* on how to specify FROM part.
836837
* @param array $params The parameters to bind to the command.
837838
*
839+
* @psalm-param RawFrom|null $from
838840
* @psalm-param ParamsType $params
839841
*
840842
* @throws Exception

src/Helper/DbArrayHelper.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use function array_multisort;
1818
use function count;
1919
use function get_object_vars;
20+
use function gettype;
2021
use function is_array;
2122
use function is_string;
2223
use function iterator_to_array;
@@ -298,12 +299,18 @@ public static function toArray(array|object $object): array
298299
* - a string with comma-separated expression values.
299300
*
300301
* @return array An array of normalized expressions.
302+
*
303+
* @psalm-template TArray as array
304+
* @psalm-template TExpression as ExpressionInterface
305+
* @psalm-param TArray|TExpression|string $raw
306+
* @psalm-return ($raw is string ? list<string> : ($raw is ExpressionInterface ? list{TExpression} : TArray))
307+
*
308+
* @psalm-suppress InvalidFalsableReturnType Psalm cannot correct parse method code.
301309
*/
302310
public static function normalizeExpressions(array|ExpressionInterface|string $raw): array
303311
{
304312
/**
305-
* @var array
306-
* @psalm-suppress PossiblyInvalidArgument
313+
* @psalm-suppress PossiblyInvalidArgument,FalsableReturnStatement
307314
*/
308315
return match (gettype($raw)) {
309316
GettypeResult::ARRAY => $raw,

src/Query/Query.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
use function is_array;
3030
use function is_int;
3131
use function is_numeric;
32+
use function is_scalar;
3233
use function is_string;
3334
use function key;
3435
use function preg_match;
@@ -75,13 +76,15 @@
7576
* @psalm-import-type IndexBy from QueryInterface
7677
* @psalm-import-type ResultCallback from QueryInterface
7778
* @psalm-import-type Join from QueryInterface
79+
* @psalm-import-type From from QueryInterface
7880
*/
7981
class Query implements QueryInterface
8082
{
8183
/** @psalm-var SelectValue $select */
8284
protected array $select = [];
8385
protected string|null $selectOption = null;
8486
protected bool $distinct = false;
87+
/** @psalm-var From */
8588
protected array $from = [];
8689
protected array $groupBy = [];
8790
protected array|ExpressionInterface|string|null $having = null;
@@ -523,7 +526,6 @@ public function getWithQueries(): array
523526
public function groupBy(array|string|ExpressionInterface $columns): static
524527
{
525528
$this->groupBy = DbArrayHelper::normalizeExpressions($columns);
526-
527529
return $this;
528530
}
529531

@@ -1000,9 +1002,6 @@ private function normalizeSelect(array|bool|float|int|string|ExpressionInterface
10001002
$columns = [$columns];
10011003
}
10021004

1003-
/**
1004-
* @var SelectValue
1005-
*/
10061005
$columns = DbArrayHelper::normalizeExpressions($columns);
10071006

10081007
$select = [];

src/Query/QueryInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
* @psalm-type JoinTable = array<string, ExpressionInterface|string>|ExpressionInterface|string
3434
* @psalm-type JoinOn = array<ExpressionInterface|string>|ExpressionInterface|string
3535
* @psalm-type Join = list{string, JoinTable, JoinOn}
36+
* @psalm-type From = array<string|ExpressionInterface>
37+
* @psalm-type RawFrom = array<string|ExpressionInterface>|ExpressionInterface|string
3638
* @psalm-import-type ParamsType from ConnectionInterface
3739
* @psalm-import-type SelectValue from QueryPartsInterface
3840
*/

src/Query/QueryPartsInterface.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
* @psalm-import-type JoinTable from QueryInterface
2323
* @psalm-import-type JoinOn from QueryInterface
2424
* @psalm-import-type Join from QueryInterface
25+
* @psalm-import-type RawFrom from QueryInterface
2526
*/
2627
interface QueryPartsInterface
2728
{
@@ -326,6 +327,8 @@ public function setFor(string|array|null $value): static;
326327
* $subQuery = "(SELECT * FROM `user` WHERE `active` = 1)";
327328
* $query = (new \Yiisoft\Db\Query\Query)->from(['activeusers' => $subQuery]);
328329
* ```
330+
*
331+
* @psalm-param RawFrom $tables
329332
*/
330333
public function from(array|ExpressionInterface|string $tables): static;
331334

src/QueryBuilder/DMLQueryBuilderInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
* @link https://en.wikipedia.org/wiki/Data_manipulation_language
2020
*
2121
* @psalm-import-type ParamsType from ConnectionInterface
22+
* @psalm-import-type RawFrom from QueryInterface
2223
* @psalm-type BatchValues = iterable<iterable<array-key, mixed>>
2324
*/
2425
interface DMLQueryBuilderInterface
@@ -190,6 +191,7 @@ public function resetSequence(string $table, int|string|null $value = null): str
190191
*
191192
* @return string The UPDATE SQL.
192193
*
194+
* @psalm-param RawFrom|null $from
193195
* @psalm-param ParamsType $params
194196
*
195197
* Note: The method will escape the table and column names.

0 commit comments

Comments
 (0)