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

Add generic types for ActiveRecord and Container #20325

Merged
merged 6 commits into from
Feb 21, 2025
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
34 changes: 33 additions & 1 deletion framework/db/ActiveQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@
* @author Qiang Xue <[email protected]>
* @author Carsten Brandt <[email protected]>
* @since 2.0
*
* @template T of (ActiveRecord|array)
*/
class ActiveQuery extends Query implements ActiveQueryInterface
{
Expand Down Expand Up @@ -127,6 +129,8 @@ public function init()
* @param Connection|null $db the DB connection used to create the DB command.
* If null, the DB connection returned by [[modelClass]] will be used.
* @return array|ActiveRecord[] the query results. If the query results in nothing, an empty array will be returned.
* @psalm-return T[]
* @phpstan-return T[]
*/
public function all($db = null)
{
Expand Down Expand Up @@ -295,9 +299,11 @@ private function removeDuplicatedModels($models)
* Executes query and returns a single row of result.
* @param Connection|null $db the DB connection used to create the DB command.
* If `null`, the DB connection returned by [[modelClass]] will be used.
* @return ActiveRecord|array|null a single row of query result. Depending on the setting of [[asArray]],
* @return array|ActiveRecord|null a single row of query result. Depending on the setting of [[asArray]],
* the query result may be either an array or an ActiveRecord object. `null` will be returned
* if the query results in nothing.
* @psalm-return T|null
* @phpstan-return T|null
*/
public function one($db = null)
{
Expand All @@ -310,6 +316,32 @@ public function one($db = null)
return null;
}

/**
* {@inheritdoc}
*
* @return BatchQueryResult
* @psalm-return T[][]|BatchQueryResult
* @phpstan-return T[][]|BatchQueryResult
* @codeCoverageIgnore
*/
public function batch($batchSize = 100, $db = null)
{
return parent::batch($batchSize, $db);
}

/**
* {@inheritdoc}
*
* @return BatchQueryResult
* @psalm-return T[]|BatchQueryResult
* @phpstan-return T[]|BatchQueryResult
* @codeCoverageIgnore
*/
public function each($batchSize = 100, $db = null)
{
return parent::each($batchSize, $db);
}

/**
* Creates a DB command that can be used to execute this query.
* @param Connection|null $db the DB connection used to create the DB command.
Expand Down
7 changes: 7 additions & 0 deletions framework/di/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,13 @@ class Container extends Component
* @return object an instance of the requested class.
* @throws InvalidConfigException if the class cannot be recognized or correspond to an invalid definition
* @throws NotInstantiableException If resolved to an abstract class or an interface (since 2.0.9)
*
*
* @template T of class-string
* @psalm-param class-string<T>|array{class: class-string<T>} $class
* @phpstan-param class-string<T>|array{class: class-string<T>} $class
* @psalm-return T
* @phpstan-return T
*/
public function get($class, $params = [], $config = [])
{
Expand Down
1 change: 1 addition & 0 deletions tests/data/ar/CustomerQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

/**
* CustomerQuery.
* @extends ActiveQuery<CustomerWithAlias>
*/
class CustomerQuery extends ActiveQuery
{
Expand Down
4 changes: 2 additions & 2 deletions tests/data/ar/CustomerWithAlias.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ class CustomerWithAlias extends ActiveRecord
public $status2;

public $sumTotal;

public static function tableName()
{
return 'customer';
}

/**
* {@inheritdoc}
* @return CustomerQuery
Expand Down