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

Conversation

xepozz
Copy link
Member

@xepozz xepozz commented Feb 16, 2025

Q A
Is bugfix?
New feature? ✔️
Breaks BC?
Screenshot 2025-02-16 at 12 40 52 PM

Using this two-way binding allows PHPStorm / PHPStan / Psalm recognize return type of calling all()/one()/batch()/each() methods

It's possible both:

  1. Add @extends ActiveQuery<ModelClass> phpdoc to the query class
  2. Add @method static ActiveQuery<static> find() phpdoc to the active record class in case the query class isn't necessary

Copy link

codecov bot commented Feb 16, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 64.85%. Comparing base (1c191ea) to head (2890fb5).
Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff            @@
##             master   #20325   +/-   ##
=========================================
  Coverage     64.85%   64.85%           
  Complexity    11435    11435           
=========================================
  Files           431      431           
  Lines         37193    37193           
=========================================
  Hits          24120    24120           
  Misses        13073    13073           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Comment on lines 129 to 131
* @return array|ActiveRecord[] the query results. If the query results in nothing, an empty array will be returned.
* @return T[] the query results. If the query results in nothing, an empty array will be returned.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That will likely break many IDEs. It will likely break API docs generation as well. Can we keep regular type for @return and add psalm/phpstan annotations only?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's possible

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

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we do it without adding methods? I think @method annotation for a class may work.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, @method phpdoc doesn't support multiple @return phpdocs

@xepozz xepozz changed the title feat: active record generic types feat: Add generic types for ActiveRecord and Container Feb 21, 2025
@samdark samdark changed the title feat: Add generic types for ActiveRecord and Container Add generic types for ActiveRecord and Container Feb 21, 2025
@samdark samdark merged commit 06bd683 into yiisoft:master Feb 21, 2025
87 checks passed
@samdark
Copy link
Member

samdark commented Feb 21, 2025

Thank you!

@xepozz xepozz deleted the types branch February 23, 2025 07:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants