Skip to content

Commit 06bd683

Browse files
xepozzsamdark
andauthored
Add generic types for ActiveRecord and Container (#20325)
Co-authored-by: Alexander Makarov <[email protected]>
1 parent 1c191ea commit 06bd683

File tree

4 files changed

+43
-3
lines changed

4 files changed

+43
-3
lines changed

framework/db/ActiveQuery.php

+33-1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@
6969
* @author Qiang Xue <[email protected]>
7070
* @author Carsten Brandt <[email protected]>
7171
* @since 2.0
72+
*
73+
* @template T of (ActiveRecord|array)
7274
*/
7375
class ActiveQuery extends Query implements ActiveQueryInterface
7476
{
@@ -127,6 +129,8 @@ public function init()
127129
* @param Connection|null $db the DB connection used to create the DB command.
128130
* If null, the DB connection returned by [[modelClass]] will be used.
129131
* @return array|ActiveRecord[] the query results. If the query results in nothing, an empty array will be returned.
132+
* @psalm-return T[]
133+
* @phpstan-return T[]
130134
*/
131135
public function all($db = null)
132136
{
@@ -295,9 +299,11 @@ private function removeDuplicatedModels($models)
295299
* Executes query and returns a single row of result.
296300
* @param Connection|null $db the DB connection used to create the DB command.
297301
* If `null`, the DB connection returned by [[modelClass]] will be used.
298-
* @return ActiveRecord|array|null a single row of query result. Depending on the setting of [[asArray]],
302+
* @return array|ActiveRecord|null a single row of query result. Depending on the setting of [[asArray]],
299303
* the query result may be either an array or an ActiveRecord object. `null` will be returned
300304
* if the query results in nothing.
305+
* @psalm-return T|null
306+
* @phpstan-return T|null
301307
*/
302308
public function one($db = null)
303309
{
@@ -310,6 +316,32 @@ public function one($db = null)
310316
return null;
311317
}
312318

319+
/**
320+
* {@inheritdoc}
321+
*
322+
* @return BatchQueryResult
323+
* @psalm-return T[][]|BatchQueryResult
324+
* @phpstan-return T[][]|BatchQueryResult
325+
* @codeCoverageIgnore
326+
*/
327+
public function batch($batchSize = 100, $db = null)
328+
{
329+
return parent::batch($batchSize, $db);
330+
}
331+
332+
/**
333+
* {@inheritdoc}
334+
*
335+
* @return BatchQueryResult
336+
* @psalm-return T[]|BatchQueryResult
337+
* @phpstan-return T[]|BatchQueryResult
338+
* @codeCoverageIgnore
339+
*/
340+
public function each($batchSize = 100, $db = null)
341+
{
342+
return parent::each($batchSize, $db);
343+
}
344+
313345
/**
314346
* Creates a DB command that can be used to execute this query.
315347
* @param Connection|null $db the DB connection used to create the DB command.

framework/di/Container.php

+7
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,13 @@ class Container extends Component
157157
* @return object an instance of the requested class.
158158
* @throws InvalidConfigException if the class cannot be recognized or correspond to an invalid definition
159159
* @throws NotInstantiableException If resolved to an abstract class or an interface (since 2.0.9)
160+
*
161+
*
162+
* @template T of class-string
163+
* @psalm-param class-string<T>|array{class: class-string<T>} $class
164+
* @phpstan-param class-string<T>|array{class: class-string<T>} $class
165+
* @psalm-return T
166+
* @phpstan-return T
160167
*/
161168
public function get($class, $params = [], $config = [])
162169
{

tests/data/ar/CustomerQuery.php

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
/**
1313
* CustomerQuery.
14+
* @extends ActiveQuery<CustomerWithAlias>
1415
*/
1516
class CustomerQuery extends ActiveQuery
1617
{

tests/data/ar/CustomerWithAlias.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ class CustomerWithAlias extends ActiveRecord
2121
public $status2;
2222

2323
public $sumTotal;
24-
24+
2525
public static function tableName()
2626
{
2727
return 'customer';
2828
}
29-
29+
3030
/**
3131
* {@inheritdoc}
3232
* @return CustomerQuery

0 commit comments

Comments
 (0)