Skip to content

Make query cache helpers private#917

Open
premtsd-code wants to merge 2 commits into
mainfrom
query-cache-helpers-private
Open

Make query cache helpers private#917
premtsd-code wants to merge 2 commits into
mainfrom
query-cache-helpers-private

Conversation

@premtsd-code

@premtsd-code premtsd-code commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

Summary by CodeRabbit

  • Bug Fixes
    • Tightened access to internal query-cache helpers to reduce unintended external use.
    • Kept query-cache behavior consistent across tests by switching to shared helper-based calculations.
  • Tests
    • Expanded test support for query-cache key, field, and purge handling.
    • Updated cache-related tests to use a common approach for derived values.

@coderabbitai

coderabbitai Bot commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

Three Database query-cache methods (purgeCachedQueries, getQueryCacheKey, getQueryCacheField) are changed from public to private. A new QueryCacheTestHelpers trait uses ReflectionMethod to access these methods, and unit tests are updated to call the trait's helpers instead of the previously public methods directly.

Changes

Query-cache method privatization and test adaptation

Layer / File(s) Summary
Privatize Database query-cache methods
src/Database/Database.php
purgeCachedQueries, getQueryCacheKey, and getQueryCacheField change from public to private, with @phpstan-ignore-next-line annotations added where needed.
Reflection-based test helper trait
tests/unit/QueryCacheTestHelpers.php
New trait wraps the now-private Database methods via ReflectionMethod, exposing purgeCachedQueries, getQueryCacheKey, and getQueryCacheField helpers for tests.
CacheKeyTest updates
tests/unit/CacheKeyTest.php
Imports the helper trait and replaces $db->getQueryCacheKey/getQueryCacheField(...) calls with $this-> helper calls across multiple test methods.
QueryCacheTest updates
tests/unit/QueryCacheTest.php
Imports the helper trait and replaces $database->getQueryCacheKey/getQueryCacheField/purgeCachedQueries(...) calls with $this-> helper calls, including cache-seeding via $cache->save(...).
WithCacheLeaseTest updates
tests/unit/WithCacheLeaseTest.php
Imports the helper trait and updates setUp() to compute the cache key via the helper instead of calling the Database instance method directly.

Estimated code review effort: 2 (Simple) | ~15 minutes

Possibly related PRs

  • utopia-php/database#828: Both PRs touch query-cache key computation and its tests, exercising the same key-derivation behaviors in CacheKeyTest.php.
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main change: query cache helper methods were made private.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
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.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch query-cache-helpers-private

Warning

Tools execution failed with the following error:

Failed to run tools: 13 INTERNAL: Received RST_STREAM with code 2 (Internal server error)


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.

@greptile-apps

greptile-apps Bot commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR narrows the public API surface of Database by making three internal query-cache helpers (purgeCachedQueries, getQueryCacheKey, getQueryCacheField) private, and introduces a shared QueryCacheTestHelpers trait that exposes them to the test suite via ReflectionMethod.

  • Database.php: three methods become private; @phpstan-ignore-next-line comments suppress dead-code warnings for the two that PHPStan cannot trace through reflection.
  • QueryCacheTestHelpers.php (new): a single trait with thin ReflectionMethod wrappers, eliminating the duplication that previously existed across three test files.
  • QueryCacheTest, CacheKeyTest, WithCacheLeaseTest: each adds use QueryCacheTestHelpers and replaces every direct call with the corresponding wrapper method.

Confidence Score: 5/5

Safe to merge — the change is a straightforward visibility reduction backed by a well-structured shared test trait.

All three methods remain fully exercised by the existing test suite through the new reflection wrappers. The project targets PHP ≥ 8.4, where ReflectionMethod::invoke() calls private methods without setAccessible(true), so the helpers work correctly. No logic is altered; only method visibility and test call-sites change.

No files require special attention.

Important Files Changed

Filename Overview
src/Database/Database.php Three cache helper methods changed from public to private; @phpstan-ignore-next-line suppresses dead-code warnings for the two that are only exercised via reflection in tests
tests/unit/QueryCacheTestHelpers.php New trait introducing ReflectionMethod-based wrappers for the three now-private methods; consolidates helpers that were previously duplicated across three test classes
tests/unit/QueryCacheTest.php Adds QueryCacheTestHelpers trait and replaces all direct public-method calls with the trait's reflection wrappers
tests/unit/CacheKeyTest.php Same as QueryCacheTest.php — uses the shared trait and replaces every direct call to the now-private methods
tests/unit/WithCacheLeaseTest.php Adds QueryCacheTestHelpers trait; replaces the single getQueryCacheKey call in setUp with the reflection wrapper

Reviews (2): Last reviewed commit: "Address query cache helper review" | Re-trigger Greptile

Comment thread src/Database/Database.php
@@ -9719,7 +9719,7 @@ public function getCacheKeys(string $collectionId, ?string $documentId = null, a
* @param string|null $namespace

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P1 Breaking public API change

purgeCachedQueries, getQueryCacheKey, and getQueryCacheField were all previously public. This is a semver-breaking change: any downstream code that calls these methods directly on a Database instance will receive a fatal Call to private method error at runtime after upgrading. As a distributed library (utopia-php/database), this warrants a major-version bump or at minimum a deprecation cycle before removal from the public surface.

Prompt To Fix With AI
This is a comment left during a code review.
Path: src/Database/Database.php
Line: 9719

Comment:
**Breaking public API change**

`purgeCachedQueries`, `getQueryCacheKey`, and `getQueryCacheField` were all previously `public`. This is a semver-breaking change: any downstream code that calls these methods directly on a `Database` instance will receive a fatal `Call to private method` error at runtime after upgrading. As a distributed library (`utopia-php/database`), this warrants a major-version bump or at minimum a deprecation cycle before removal from the public surface.

How can I resolve this? If you propose a fix, please make it concise.

Fix in Claude Code Fix in Codex

Comment thread tests/unit/QueryCacheTest.php Outdated

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 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 `@tests/unit/QueryCacheTestHelpers.php`:
- Around line 9-39: The reflection helpers in QueryCacheTestHelpers are invoking
private Database methods without making them accessible, which can fail on PHP
8.0. Update purgeCachedQueries, getQueryCacheKey, and getQueryCacheField to call
setAccessible(true) on each ReflectionMethod before invoke so the private
Database methods can be exercised reliably in tests.
🪄 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: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: e0e87f9b-4217-49bb-bfa4-d431be487741

📥 Commits

Reviewing files that changed from the base of the PR and between a090699 and 3c789ae.

📒 Files selected for processing (5)
  • src/Database/Database.php
  • tests/unit/CacheKeyTest.php
  • tests/unit/QueryCacheTest.php
  • tests/unit/QueryCacheTestHelpers.php
  • tests/unit/WithCacheLeaseTest.php

Comment thread tests/unit/QueryCacheTestHelpers.php
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.

1 participant