Skip to content

Fix queryScalar() state mutation by using clone#1172

Open
darkspock wants to merge 1 commit intoyiisoft:masterfrom
darkspock:fix/query-scalar-state-mutation
Open

Fix queryScalar() state mutation by using clone#1172
darkspock wants to merge 1 commit intoyiisoft:masterfrom
darkspock:fix/query-scalar-state-mutation

Conversation

@darkspock
Copy link
Copy Markdown

Summary

Query::queryScalar() manually saves and restores select, orderBy, limit, and offset properties around createCommand(). This is fragile:

  • If createCommand() throws an exception, the original state is never restored
  • Not safe if properties hold references used elsewhere
  • Inconsistent with the else branch (line 856) which already creates a new Query instance

Before

$select = $this->select;
$order = $this->orderBy;
$limit = $this->limit;
$offset = $this->offset;

$this->select = [$selectExpression];
$this->orderBy = [];
$this->limit = null;
$this->offset = null;

$command = $this->createCommand();

$this->select = $select;
$this->orderBy = $order;
$this->limit = $limit;
$this->offset = $offset;

return $command->queryScalar();

After

$query = clone $this;
$query->select = [$selectExpression];
$query->orderBy = [];
$query->limit = null;
$query->offset = null;

return $query->createCommand()->queryScalar();

The clone approach is already the established pattern in this codebase (see withTypecasting()).

Risk

Low — the clone produces identical SQL output. The only behavioral change is that the original Query object is no longer temporarily mutated.

queryScalar() was manually saving and restoring select, orderBy, limit,
and offset properties around createCommand(). This is fragile: if
createCommand() throws, the original state is never restored. It is also
not safe if properties are referenced elsewhere.

Replace with clone $this, which is already the pattern used in the else
branch (line 856) and in withTypecasting().

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@codecov
Copy link
Copy Markdown

codecov bot commented Apr 2, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.62%. Comparing base (dffa745) to head (7e710c5).

Additional details and impacted files
@@             Coverage Diff              @@
##             master    #1172      +/-   ##
============================================
- Coverage     98.62%   98.62%   -0.01%     
  Complexity     1645     1645              
============================================
  Files           120      120              
  Lines          4292     4284       -8     
============================================
- Hits           4233     4225       -8     
  Misses           59       59              

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

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Copy Markdown
Member

@Tigrov Tigrov left a comment

Choose a reason for hiding this comment

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

Needs to add a line to changelog.

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.

4 participants