Skip to content

Commit 5dcf4b8

Browse files
authored
Fix #20654: Add missing generics in yii\db namespace. Fix PHPDoc annotations in yii\db\ArrayExpression
1 parent 6238940 commit 5dcf4b8

16 files changed

+98
-34
lines changed

framework/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ Yii Framework 2 Change Log
7373
- Bug #20639: Add missing generics in `yii\web` namespace (mspirkov)
7474
- Bug #20645: Add missing generics in `yii\helpers` and `yii\test` namespaces. Fix PHPDoc annotations in `ArrayAccessTrait` (mspirkov)
7575
- Bug #20640: Fix `@param` annotation for `$block` in `yii\console\Markdown::renderParagraph()` (mspirkov)
76+
- Bug #20654: Add missing generics in `yii\db` namespace. Fix PHPDoc annotations in `yii\db\ArrayExpression` (mspirkov)
7677
- Bug #20651: Add missing generics in `yii\filters` namespace (mspirkov)
7778

7879

framework/db/ActiveQuery.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,10 @@ public function prepare($builder)
191191
$this->filterByModels($viaModels);
192192
} elseif (is_array($this->via)) {
193193
// via relation
194-
/** @var self $viaQuery */
194+
/**
195+
* @var self $viaQuery
196+
* @phpstan-var self<ActiveRecord|array<string, mixed>> $viaQuery
197+
*/
195198
list($viaName, $viaQuery, $viaCallableUsed) = $this->via;
196199
if ($viaQuery->multiple) {
197200
if ($viaCallableUsed) {
@@ -458,7 +461,10 @@ public function joinWith($with, $eagerLoading = true, $joinType = 'LEFT JOIN')
458461
list(, $relation, $alias) = $matches;
459462
$name = $relation;
460463
$callback = function ($query) use ($callback, $alias) {
461-
/** @var self $query */
464+
/**
465+
* @var self $query
466+
* @phpstan-var self<ActiveRecord|array<string, mixed>> $query
467+
*/
462468
$query->alias($alias);
463469
if ($callback !== null) {
464470
call_user_func($callback, $query);
@@ -648,6 +654,9 @@ protected function getTableNameAndAlias()
648654
* @param ActiveQuery $parent
649655
* @param ActiveQuery $child
650656
* @param string $joinType
657+
*
658+
* @phpstan-param ActiveQuery<ActiveRecord|array<string, mixed>> $parent
659+
* @phpstan-param ActiveQuery<ActiveRecord|array<string, mixed>> $child
651660
*/
652661
private function joinWithRelation($parent, $child, $joinType)
653662
{

framework/db/ActiveQueryTrait.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,10 @@ public function findWith($with, &$models)
149149
$primaryModel = $modelClass::instance();
150150
}
151151
$relations = $this->normalizeRelations($primaryModel, $with);
152-
/** @var ActiveQuery $relation */
152+
/**
153+
* @var ActiveQuery $relation
154+
* @phpstan-var ActiveQuery<ActiveRecord|array<string, mixed>> $relation
155+
*/
153156
foreach ($relations as $name => $relation) {
154157
if ($relation->asArray === null) {
155158
// inherit asArray from primary query

framework/db/ActiveRelationTrait.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,13 +233,19 @@ public function populateRelation($name, &$primaryModels)
233233

234234
if ($this->via instanceof self) {
235235
// via junction table
236-
/** @var self $viaQuery */
236+
/**
237+
* @var self $viaQuery
238+
* @phpstan-var self<ActiveRecord|array<string, mixed>> $viaQuery
239+
*/
237240
$viaQuery = $this->via;
238241
$viaModels = $viaQuery->findJunctionRows($primaryModels);
239242
$this->filterByModels($viaModels);
240243
} elseif (is_array($this->via)) {
241244
// via relation
242-
/** @var self|ActiveQueryTrait $viaQuery */
245+
/**
246+
* @var self|ActiveQueryTrait $viaQuery
247+
* @phpstan-var self<ActiveRecord|array<string, mixed>>|ActiveQueryTrait $viaQuery
248+
*/
243249
list($viaName, $viaQuery) = $this->via;
244250
if ($viaQuery->asArray === null) {
245251
// inherit asArray from primary query
@@ -401,6 +407,8 @@ private function populateInverseRelation(&$primaryModels, $models, $primaryName,
401407
* @param self|null $viaQuery
402408
* @param bool $checkMultiple
403409
* @return array
410+
*
411+
* @phpstan-param self<ActiveRecord|array<string, mixed>> $viaQuery
404412
*/
405413
private function buildBuckets($models, $link, $viaModels = null, $viaQuery = null, $checkMultiple = true)
406414
{

framework/db/ArrayExpression.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
* @author Dmytro Naumenko <[email protected]>
2626
* @since 2.0.14
2727
* @phpcs:disable Squiz.NamingConventions.ValidVariableName.PrivateNoUnderscore
28+
*
29+
* @implements \ArrayAccess<array-key, mixed>
30+
* @implements \IteratorAggregate<array-key, mixed>
2831
*/
2932
class ArrayExpression implements ExpressionInterface, \ArrayAccess, \Countable, \IteratorAggregate
3033
{
@@ -96,7 +99,7 @@ public function getDimension()
9699
* Whether a offset exists
97100
*
98101
* @link https://www.php.net/manual/en/arrayaccess.offsetexists.php
99-
* @param mixed $offset <p>
102+
* @param int|string $offset <p>
100103
* An offset to check for.
101104
* </p>
102105
* @return bool true on success or false on failure.
@@ -115,7 +118,7 @@ public function offsetExists($offset)
115118
* Offset to retrieve
116119
*
117120
* @link https://www.php.net/manual/en/arrayaccess.offsetget.php
118-
* @param mixed $offset <p>
121+
* @param int|string $offset <p>
119122
* The offset to retrieve.
120123
* </p>
121124
* @return mixed Can return all value types.
@@ -131,7 +134,7 @@ public function offsetGet($offset)
131134
* Offset to set
132135
*
133136
* @link https://www.php.net/manual/en/arrayaccess.offsetset.php
134-
* @param mixed $offset <p>
137+
* @param int|string $offset <p>
135138
* The offset to assign the value to.
136139
* </p>
137140
* @param mixed $value <p>
@@ -150,7 +153,7 @@ public function offsetSet($offset, $value)
150153
* Offset to unset
151154
*
152155
* @link https://www.php.net/manual/en/arrayaccess.offsetunset.php
153-
* @param mixed $offset <p>
156+
* @param int|string $offset <p>
154157
* The offset to unset.
155158
* </p>
156159
* @return void

framework/db/BaseActiveRecord.php

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -454,8 +454,13 @@ public function hasMany($class, $link)
454454
*/
455455
protected function createRelationQuery($class, $link, $multiple)
456456
{
457-
/** @var ActiveRecordInterface $class */
458-
/** @var ActiveQuery $query */
457+
/**
458+
* @var ActiveRecordInterface $class
459+
* @var ActiveQuery $query
460+
*
461+
* @phpstan-var ActiveQuery<ActiveRecord> $query
462+
*/
463+
459464
$query = $class::find();
460465
$query->primaryModel = $this;
461466
$query->link = $link;
@@ -1321,7 +1326,10 @@ public function link($name, $model, $extraColumns = [])
13211326
throw new InvalidCallException('Unable to link models: the models being linked cannot be newly created.');
13221327
}
13231328
if (is_array($relation->via)) {
1324-
/** @var ActiveQuery $viaRelation */
1329+
/**
1330+
* @var ActiveQuery $viaRelation
1331+
* @phpstan-var ActiveQuery<ActiveRecord|array<string, mixed>> $viaRelation
1332+
*/
13251333
list($viaName, $viaRelation) = $relation->via;
13261334
$viaClass = $viaRelation->modelClass;
13271335
// unset $viaName so that it can be reloaded to reflect the change
@@ -1414,7 +1422,10 @@ public function unlink($name, $model, $delete = false)
14141422

14151423
if ($relation->via !== null) {
14161424
if (is_array($relation->via)) {
1417-
/** @var ActiveQuery $viaRelation */
1425+
/**
1426+
* @var ActiveQuery $viaRelation
1427+
* @phpstan-var ActiveQuery<ActiveRecord> $viaRelation
1428+
*/
14181429
list($viaName, $viaRelation) = $relation->via;
14191430
$viaClass = $viaRelation->modelClass;
14201431
unset($this->_related[$viaName]);
@@ -1517,7 +1528,10 @@ public function unlinkAll($name, $delete = false)
15171528

15181529
if ($relation->via !== null) {
15191530
if (is_array($relation->via)) {
1520-
/** @var ActiveQuery $viaRelation */
1531+
/**
1532+
* @var ActiveQuery $viaRelation
1533+
* @phpstan-var ActiveQuery<ActiveRecord|array<string, mixed>> $viaRelation
1534+
*/
15211535
list($viaName, $viaRelation) = $relation->via;
15221536
$viaClass = $viaRelation->modelClass;
15231537
unset($this->_related[$viaName]);

framework/db/BatchQueryResult.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
*
2828
* @author Qiang Xue <[email protected]>
2929
* @since 2.0
30+
*
31+
* @implements \Iterator<int, mixed>
3032
*/
3133
class BatchQueryResult extends Component implements \Iterator
3234
{

framework/db/Connection.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,9 @@
131131
*
132132
* @author Qiang Xue <[email protected]>
133133
* @since 2.0
134+
*
135+
* @phpstan-property-read Schema<ColumnSchema> $schema
136+
* @psalm-property-read Schema<ColumnSchema> $schema
134137
*/
135138
class Connection extends Component
136139
{
@@ -436,6 +439,8 @@ class Connection extends Component
436439
private $_transaction;
437440
/**
438441
* @var Schema|null the database schema
442+
*
443+
* @phpstan-var Schema<ColumnSchema>|null
439444
*/
440445
private $_schema;
441446
/**
@@ -854,6 +859,9 @@ private function rollbackTransactionOnLevel($transaction, $level)
854859
* Returns the schema information for the database opened by this connection.
855860
* @return Schema the schema information for the database opened by this connection.
856861
* @throws NotSupportedException if there is no support for the current driver type
862+
*
863+
* @phpstan-return Schema<ColumnSchema>
864+
* @psalm-return Schema<ColumnSchema>
857865
*/
858866
public function getSchema()
859867
{

framework/db/DataReader.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@
4747
*
4848
* @author Qiang Xue <[email protected]>
4949
* @since 2.0
50+
*
51+
* @implements \Iterator<int, mixed>
5052
*/
5153
class DataReader extends \yii\base\BaseObject implements \Iterator, \Countable
5254
{

framework/db/QueryBuilder.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,12 +400,15 @@ protected function prepareInsertValues($table, $columns, $params = [])
400400
* Prepare select-subquery and field names for INSERT INTO ... SELECT SQL statement.
401401
*
402402
* @param Query $columns Object, which represents select query.
403-
* @param \yii\db\Schema $schema Schema object to quote column name.
403+
* @param Schema $schema Schema object to quote column name.
404404
* @param array $params the parameters to be bound to the generated SQL statement. These parameters will
405405
* be included in the result with the additional parameters generated during the query building process.
406406
* @return array array of column names, values and params.
407407
* @throws InvalidArgumentException if query's select does not contain named parameters only.
408408
* @since 2.0.11
409+
*
410+
* @phpstan-param Schema<ColumnSchema> $schema
411+
* @psalm-param Schema<ColumnSchema> $schema
409412
*/
410413
protected function prepareInsertSelectSubQuery($columns, $schema, $params = [])
411414
{

0 commit comments

Comments
 (0)