Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve psalm types for scalar values in select #818

Merged
merged 2 commits into from
Apr 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/Query/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,12 @@
* ```
*
* Query internally uses the {@see \Yiisoft\Db\QueryBuilder\AbstractQueryBuilder} class to generate the SQL statement.
*
* @psalm-import-type SelectValue from QueryPartsInterface
*/
class Query implements QueryInterface
{
/** @psalm-var SelectValue $select */
protected array $select = [];
protected string|null $selectOption = null;
protected bool|null $distinct = null;
Expand Down Expand Up @@ -855,6 +858,9 @@ private function normalizeOrderBy(array|string|ExpressionInterface $columns): ar

/**
* Normalizes the `SELECT` columns passed to {@see select()} or {@see addSelect()}.
*
* @psalm-param SelectValue|scalar|ExpressionInterface $columns
* @psalm-return SelectValue
*/
private function normalizeSelect(array|bool|float|int|string|ExpressionInterface $columns): array
{
Expand All @@ -866,7 +872,6 @@ private function normalizeSelect(array|bool|float|int|string|ExpressionInterface

$select = [];

/** @psalm-var array<array-key, bool|float|int|string|ExpressionInterface> $columns */
foreach ($columns as $columnAlias => $columnDefinition) {
if (is_string($columnAlias)) {
// Already in the normalized format, good for them.
Expand Down
2 changes: 2 additions & 0 deletions src/Query/QueryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
* Sorting is supported via {@see orderBy()} and items can be limited to match some conditions using {@see where()}.
*
* @psalm-import-type ParamsType from ConnectionInterface
* @psalm-import-type SelectValue from QueryPartsInterface
*/
interface QueryInterface extends ExpressionInterface, QueryPartsInterface, QueryFunctionsInterface
{
Expand Down Expand Up @@ -207,6 +208,7 @@ public function getParams(): array;

/**
* @return array The "select" value.
* @psalm-return SelectValue
*/
public function getSelect(): array;

Expand Down
5 changes: 5 additions & 0 deletions src/Query/QueryPartsInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*
* {@see Query} uses these methods to build and manipulate SQL statements.
*
* @psalm-type SelectValue = array<array-key, ExpressionInterface|scalar>
* @psalm-import-type ParamsType from ConnectionInterface
*/
interface QueryPartsInterface
Expand Down Expand Up @@ -69,6 +70,8 @@ public function addOrderBy(array|string|ExpressionInterface $columns): static;
* @see select() for more details about the format of this parameter.
*
* @since 2.0.0 `$columns` can be a scalar value or an array of scalar values.
*
* @psalm-param SelectValue|scalar|ExpressionInterface $columns
*/
public function addSelect(array|bool|float|int|string|ExpressionInterface $columns): static;

Expand Down Expand Up @@ -532,6 +535,8 @@ public function rightJoin(array|string $table, array|string $on = '', array $par
*
* @since 2.0.0 `$columns` can be a scalar value or an array of scalar values.
* For example, `$query->select(1)` will be converted to `SELECT 1`.
*
* @psalm-param SelectValue|scalar|ExpressionInterface $columns
*/
public function select(array|bool|float|int|string|ExpressionInterface $columns, string $option = null): static;

Expand Down
1 change: 0 additions & 1 deletion src/QueryBuilder/AbstractDQLQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,6 @@ public function buildSelect(
return $select . ' *';
}

/** @psalm-var array<array-key, ExpressionInterface|bool|float|int|string> $columns */
foreach ($columns as $i => $column) {
if ($column instanceof ExpressionInterface) {
if (is_int($i)) {
Expand Down
3 changes: 3 additions & 0 deletions src/QueryBuilder/DQLQueryBuilderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Yiisoft\Db\Exception\NotSupportedException;
use Yiisoft\Db\Expression\ExpressionBuilderInterface;
use Yiisoft\Db\Expression\ExpressionInterface;
use Yiisoft\Db\Query\QueryPartsInterface;
use Yiisoft\Db\QueryBuilder\Condition\Interface\ConditionInterface;
use Yiisoft\Db\Query\QueryInterface;

Expand All @@ -20,6 +21,7 @@
* @link https://en.wikipedia.org/wiki/Data_query_language
*
* @psalm-import-type ParamsType from ConnectionInterface
* @psalm-import-type SelectValue from QueryPartsInterface
*/
interface DQLQueryBuilderInterface
{
Expand Down Expand Up @@ -224,6 +226,7 @@ public function buildOrderByAndLimit(
*
* @return string The `SELECT` clause built from {@see \Yiisoft\Db\Query\Query::select()}.
*
* @psalm-param SelectValue $columns
* @psalm-param ParamsType $params
*/
public function buildSelect(
Expand Down
Loading