Skip to content

Fix passing generics to BatchQueryResult - #20856

Merged
mspirkov merged 6 commits into
yiisoft:masterfrom
mspirkov:fix-batch-query-result
May 7, 2026
Merged

Fix passing generics to BatchQueryResult#20856
mspirkov merged 6 commits into
yiisoft:masterfrom
mspirkov:fix-batch-query-result

Conversation

@mspirkov

@mspirkov mspirkov commented May 7, 2026

Copy link
Copy Markdown
Contributor
Q A
Is bugfix? ✔️
New feature?
Breaks BC?
Fixed issues
image

@coderabbitai

coderabbitai Bot commented May 7, 2026

Copy link
Copy Markdown

Review Change Stack
No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI (base), Organization UI (inherited)

Review profile: ASSERTIVE

Plan: Pro

Run ID: 613aebba-3f27-4bdc-8cdb-378bf82b0dd4

📥 Commits

Reviewing files that changed from the base of the PR and between 4c337b6 and 6d839b5.

📒 Files selected for processing (2)
  • framework/db/ActiveQuery.php
  • framework/db/BatchQueryResult.php
📜 Recent review details
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (20)
  • GitHub Check: MSSQL tests / PHP 8.0-mssql-2022-latest
  • GitHub Check: SQLite tests with coverage / PHP 8.5-windows-latest
  • GitHub Check: SQLite tests with coverage / PHP 8.5-ubuntu-22.04
  • GitHub Check: SQLite tests with coverage / PHP 7.4-ubuntu-22.04
  • GitHub Check: SQLite tests with coverage / PHP 7.4-windows-latest
  • GitHub Check: Oracle tests with coverage / PHP 7.4-oracle-slim-faststart
  • GitHub Check: phpstan-7x / PHP 7.4-ubuntu-latest
  • GitHub Check: MySQL tests with coverage / PHP 7.4-mysql-5.7
  • GitHub Check: phpstan / PHP 8.5-ubuntu-latest
  • GitHub Check: PostgreSQL tests / PHP 8.3-pgsql-latest
  • GitHub Check: PostgreSQL tests / PHP 8.1-pgsql-latest
  • GitHub Check: PostgreSQL tests / PHP 8.4-pgsql-latest
  • GitHub Check: PostgreSQL tests / PHP 8.2-pgsql-latest
  • GitHub Check: PostgreSQL tests / PHP 8.0-pgsql-latest
  • GitHub Check: PHP 8.3
  • GitHub Check: phpcs / PHP 7.4-ubuntu-latest
  • GitHub Check: PHP 8
  • GitHub Check: PHP 8.5
  • GitHub Check: PHP 7.4
  • GitHub Check: NPM 10 on ubuntu-22.04
🧰 Additional context used
🧠 Learnings (1)
📚 Learning: 2026-04-21T21:24:32.138Z
Learnt from: terabytesoftw
Repo: yiisoft/yii2 PR: 20829
File: framework/db/mssql/QueryBuilder.php:678-683
Timestamp: 2026-04-21T21:24:32.138Z
Learning: In yiisoft/yii2, follow the existing PHPStan configuration: `empty()` is explicitly prohibited. When reviewing PHP code, do not recommend replacing strict checks like `$var === null || $var === []` (or similar null/empty-array logic) with `empty($var)`, since that would conflict with the codebase’s `empty()` policy.

Applied to files:

  • framework/db/ActiveQuery.php
  • framework/db/BatchQueryResult.php
🔇 Additional comments (2)
framework/db/ActiveQuery.php (1)

77-77: each() generic key type now matches runtime behavior.

Using BatchQueryResult<array-key, T> here is accurate and aligns with keyed iteration via indexBy.

framework/db/BatchQueryResult.php (1)

32-34: Generic iterator typing is clean and consistent.

The TKey/TValue templates and the updated _key, key(), and current() phpdocs are aligned and correctly model BatchQueryResult as \Iterator<TKey, TValue>.

Also applies to: 87-88, 204-205, 215-216


📝 Walkthrough

Summary by CodeRabbit

  • Bug Fixes
    • Corrected generic type annotations for batch iteration results to better reflect key/value types.
  • Documentation
    • Added a changelog entry noting the fix.
  • Chores
    • Removed an obsolete static-analysis suppression from the baseline configuration.

Walkthrough

Docblock generics for BatchQueryResult were generalized: TKey and TValue templates added, iterator annotation changed to Iterator<TKey, TValue>, internal key and method docblocks (key(), current()) updated, ActiveQuery PHPDoc signatures adjusted, one phpstan baseline suppression removed, and changelog entry added.

Changes

BatchQueryResult Generic Type Annotations

Layer / File(s) Summary
Class Template and Iterator
framework/db/BatchQueryResult.php
Add @template TKey of array-key = array-key and @template TValue of mixed = mixed; change @implements \Iterator<int, mixed>@implements \Iterator<TKey, TValue>.
Internal Iterator Key
framework/db/BatchQueryResult.php
Change internal $_key doc type from `string
key() Method
framework/db/BatchQueryResult.php
`@return int
current() Method
framework/db/BatchQueryResult.php
@return mixed@return TValue.
ActiveQuery Docblocks
framework/db/ActiveQuery.php
Update batch() and each() @method annotations to use BatchQueryResult<array-key, T[]> and BatchQueryResult<array-key, T>.
PHPStan Baseline
phpstan-baseline.neon
Remove ignoreErrors entry suppressing “$newID in empty() always exists and is not falsy” for framework/web/DbSession.php; nearby BaseFileHelper suppression remains.
Release Notes
framework/CHANGELOG.md
Add bullet for Bug #20856 under 2.0.55 “under development”.
  • Possibly related PRs:

  • Suggested labels: type:bug

🐰 I hopped through docblocks, neat and spry,
Keys and values now type-correct and shy,
PHPStan's baseline trimmed a tiny thread,
Changelog notes the fix we spread,
BatchQueryResult blooms—templates up high.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title 'Fix passing generics to BatchQueryResult' directly and clearly describes the main change—updating generic type annotations in BatchQueryResult and related classes.
Description check ✅ Passed The description indicates this is a bugfix for issue #20856 with an image demonstrating PHPStan type analysis of BatchQueryResult generics, which is directly related to the changeset.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Tip

💬 Introducing Slack Agent: The best way for teams to turn conversations into code.

Slack Agent is built on CodeRabbit's deep understanding of your code, so your team can collaborate across the entire SDLC without losing context.

  • Generate code and open pull requests
  • Plan features and break down work
  • Investigate incidents and troubleshoot customer tickets together
  • Automate recurring tasks and respond to alerts with triggers
  • Summarize progress and report instantly

Built for teams:

  • Shared memory across your entire org—no repeating context
  • Per-thread sandboxes to safely plan and execute work
  • Governance built-in—scoped access, auditability, and budget controls

One agent for your entire SDLC. Right inside Slack.

👉 Get started


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@mspirkov
mspirkov marked this pull request as draft May 7, 2026 09:18
@codecov

codecov Bot commented May 7, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 80.15%. Comparing base (12b52ff) to head (6d839b5).
⚠️ Report is 1 commits behind head on master.
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@            Coverage Diff            @@
##             master   #20856   +/-   ##
=========================================
  Coverage     80.15%   80.15%           
  Complexity    11536    11536           
=========================================
  Files           374      374           
  Lines         30213    30213           
=========================================
  Hits          24218    24218           
  Misses         5995     5995           

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

🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@mspirkov
mspirkov marked this pull request as ready for review May 7, 2026 09:26
@coderabbitai coderabbitai Bot added the type:bug Bug label May 7, 2026

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 4

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@framework/db/ActiveQuery.php`:
- Around line 75-78: The batch() docblock broadened the key type from int to
array-key incorrectly; keep BatchQueryResult key type as int for batch() to
reflect that BatchQueryResult::next() assigns integer keys (see BatchQueryResult
class and its _key property and next() else-branch where _key is
auto-incremented). Revert or change the PHPDoc for batch() to
BatchQueryResult<int, T[]> (leave each() as BatchQueryResult<array-key, T>), so
callers know batch() yields integer keys only and types remain accurate. Ensure
the docblock mention of batch() and the template params are updated accordingly.
- Around line 75-78: The docblock for ActiveQuery::batch() wrongly widens the
key type to "array-key"; change the `@method` annotation back to use integer keys
(BatchQueryResult<int, T[]>) because BatchQueryResult::next() always sets $_key
to 0,1,2,...; leave the each() annotation as-is. Update the `@method` line
referencing batch($batchSize = 100, $db = null) in ActiveQuery.php to use
BatchQueryResult<int, T[]> so callers destructuring the key keep the correct int
type and the contract matches BatchQueryResult::next().

In `@framework/db/BatchQueryResult.php`:
- Around line 32-34: The docblock template for TValue in BatchQueryResult is
using a vacuous bound ("@template TValue of mixed = mixed"); update the docblock
to remove the redundant "of mixed" and declare it as "@template TValue = mixed"
(keeping the existing "@template TKey of array-key = array-key" and the
"@implements \Iterator<TKey, TValue>" line intact) so the annotation is concise
and semantically equivalent.
- Around line 32-33: The docblock uses a vacuous constraint for the TValue
template; update the class/file docblock in BatchQueryResult.php by replacing
the line "@template TValue of mixed = mixed" with the simplified "@template
TValue = mixed" (leave the existing "@template TKey of array-key = array-key"
line intact) so PHPDoc/PHPStan remains correct but clearer.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI (base), Organization UI (inherited)

Review profile: ASSERTIVE

Plan: Pro

Run ID: 5facaeeb-5718-4d95-840b-c578680af0ec

📥 Commits

Reviewing files that changed from the base of the PR and between 59249bb and 4c337b6.

📒 Files selected for processing (3)
  • framework/db/ActiveQuery.php
  • framework/db/BatchQueryResult.php
  • phpstan-baseline.neon
💤 Files with no reviewable changes (1)
  • phpstan-baseline.neon
📜 Review details
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (17)
  • GitHub Check: PHP 8
  • GitHub Check: MSSQL tests / PHP 8.1-mssql-2022-latest
  • GitHub Check: MSSQL tests / PHP 8.4-mssql-2022-latest
  • GitHub Check: MSSQL tests / PHP 8.0-mssql-2022-latest
  • GitHub Check: MSSQL tests / PHP 8.3-mssql-2022-latest
  • GitHub Check: SQLite tests / PHP 8.2-ubuntu-22.04
  • GitHub Check: MSSQL tests with coverage / PHP 7.4-mssql-2019-latest
  • GitHub Check: MSSQL tests with coverage / PHP 8.5-mssql-2022-latest
  • GitHub Check: MySQL tests / PHP 8.3-mysql-latest
  • GitHub Check: MySQL tests / PHP 8.1-mysql-latest
  • GitHub Check: PostgreSQL tests with coverage / PHP 7.4-pgsql-12
  • GitHub Check: SQLite tests / PHP 8.0-ubuntu-22.04
  • GitHub Check: SQLite tests / PHP 8.1-ubuntu-22.04
  • GitHub Check: PostgreSQL tests with coverage / PHP 7.4-pgsql-15
  • GitHub Check: SQLite tests with coverage / PHP 7.4-ubuntu-22.04
  • GitHub Check: Oracle tests with coverage / PHP 8.5-oracle-slim-faststart
  • GitHub Check: Oracle tests with coverage / PHP 7.4-oracle-slim-faststart
🧰 Additional context used
🧠 Learnings (1)
📚 Learning: 2026-04-21T21:24:32.138Z
Learnt from: terabytesoftw
Repo: yiisoft/yii2 PR: 20829
File: framework/db/mssql/QueryBuilder.php:678-683
Timestamp: 2026-04-21T21:24:32.138Z
Learning: In yiisoft/yii2, follow the existing PHPStan configuration: `empty()` is explicitly prohibited. When reviewing PHP code, do not recommend replacing strict checks like `$var === null || $var === []` (or similar null/empty-array logic) with `empty($var)`, since that would conflict with the codebase’s `empty()` policy.

Applied to files:

  • framework/db/BatchQueryResult.php
  • framework/db/ActiveQuery.php
🔇 Additional comments (3)
framework/db/BatchQueryResult.php (3)

204-220: LGTM — key() and current() return type annotations are correct.

The TKey|null return on key() accurately reflects both the null-after-reset case and the templated key type. current() returning TValue is consistent with $_value being typed mixed given TValue's upper bound is mixed.


87-89: $_key annotation is correct.

@var TKey|null accurately reflects both the null-initialised/reset state and the templated key type, consistent with the assignments in next() and reset().


204-220: key() and current() return annotations are correct.

@return TKey|null on key() is accurate: _key is null after reset() and typed as TKey during iteration. @return TValue on current() is consistent with TValue's upper bound of mixed matching $_value's mixed type.

Comment thread framework/db/ActiveQuery.php Outdated
Comment thread framework/db/BatchQueryResult.php
Comment thread framework/db/BatchQueryResult.php Outdated
@mspirkov
mspirkov merged commit f635d55 into yiisoft:master May 7, 2026
77 of 78 checks passed
@mspirkov
mspirkov deleted the fix-batch-query-result branch May 7, 2026 09:42
@coderabbitai coderabbitai Bot mentioned this pull request May 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants