Skip to content

Conversation

@fogelito
Copy link
Contributor

@fogelito fogelito commented Aug 3, 2025

  • Fix updateDocuments when unsetting permissions
  • Remove encode permissions

Summary by CodeRabbit

  • Bug Fixes
    • Ensured that documents always include a permissions attribute, initialized as an empty array when permissions are empty.
    • Prevented unnecessary processing of the permissions attribute during decoding and casting.
  • Tests
    • Enhanced tests to verify that documents consistently include an empty permissions attribute.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Aug 3, 2025

Walkthrough

The changes update how the $permissions attribute is handled in document creation, encoding, decoding, and casting within the database logic. Explicit checks ensure $permissions is always an array during creation, while decoding and casting now skip processing this attribute. Tests are updated to verify these behaviors, and minor code simplifications are included.

Changes

Cohort / File(s) Change Summary
Database Permissions Handling
src/Database/Database.php
Ensures $permissions is always initialized as an array in document creation; skips decoding and casting for $permissions; removes array initialization from encoding; adds minor formatting.
Document Attribute Removal
src/Database/Document.php
Simplifies removeAttribute by directly unsetting the key without checking existence.
Test for Permissions Handling
tests/e2e/Adapter/Scopes/DocumentTests.php
Enhances test to assert that each document includes $permissions as an empty array.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant Database
    participant Adapter

    Client->>Database: createDocument(data, permissions)
    Database->>Database: If permissions empty, set as []
    Database->>Adapter: createDocument(data with permissions)
    Adapter-->>Database: Document created

    Client->>Database: decode(document)
    Database->>Database: For each attribute
    alt attribute == "$permissions"
        Database->>Database: Skip decoding/casting
    else
        Database->>Database: Decode/cast attribute
    end
    Database-->>Client: Decoded document
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~7 minutes

Possibly related PRs

  • Fix encoding decoding #639: Both PRs modify handling of the $permissions attribute in Database methods to ensure proper processing and prevent unwanted transformations.
  • Skip permissions update true #641: Both PRs change how permissions are managed during document updates, with explicit checks and logic for skipping permissions processing.
  • Skip permissions updates #636: Both PRs address skipping permissions transformations and ensuring correct initialization during document processing and updates.

Suggested reviewers

  • abnegate

Poem

A hop and a skip through the database warren,
Where permissions now always arrive without foreign.
No casting, no decoding, just arrays all the way—
The documents dance in a safe, tidy ballet.
With tests to confirm, and code shining bright,
This bunny approves—permissions handled right! 🐇✨

Note

⚡️ Unit Test Generation is now available in beta!

Learn more here, or try it out under "Finishing Touches" below.


📜 Recent review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 6ac96b2 and 4018440.

📒 Files selected for processing (2)
  • src/Database/Database.php (5 hunks)
  • tests/e2e/Adapter/Scopes/DocumentTests.php (1 hunks)
✅ Files skipped from review due to trivial changes (1)
  • tests/e2e/Adapter/Scopes/DocumentTests.php
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/Database/Database.php
✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix-encode-permissions

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
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai generate unit tests to generate unit tests for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (3)
src/Database/Database.php (2)

3612-3614: Prefer an offsetExists check over empty()

empty($document->getPermissions()) evaluates to true even when the caller deliberately passed an empty array.
A more precise guard is:

if (!$document->offsetExists('$permissions')) {
    $document->setAttribute('$permissions', []);
}

(Same duplication appears in both create paths – consider extracting into a small helper to keep things DRY.)

Also applies to: 3715-3717


6346-6349: Remove dead, commented-out code

The fallback that injected empty permissions is now obsolete—commenting it out just adds noise. Please delete these lines to keep the method lean.

tests/e2e/Adapter/Scopes/DocumentTests.php (1)

452-456: Use a strict comparison via the dedicated accessor

A stricter and slightly clearer assertion improves both intent and failure diagnostics:

 foreach ($results as $result) {
     $this->assertArrayHasKey('$permissions', $result);
-    $this->assertEquals([], $result->getAttribute('$permissions'));
+    // Rely on the public accessor and a strict check
+    $this->assertSame([], $result->getPermissions());
 }

assertSame avoids accidental truthiness matches, and getPermissions() is the canonical way to read the permissions field.

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 065b481 and a85cec9.

📒 Files selected for processing (4)
  • src/Database/Database.php (4 hunks)
  • src/Database/Document.php (1 hunks)
  • tests/e2e/Adapter/Scopes/DocumentTests.php (1 hunks)
  • tests/e2e/Adapter/Scopes/PermissionTests.php (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
src/Database/Database.php (2)
src/Database/Document.php (2)
  • getPermissions (87-90)
  • setAttribute (236-253)
src/Database/Query.php (1)
  • setAttribute (161-166)
🔇 Additional comments (2)
src/Database/Document.php (1)

279-285: Good simplification - removing unnecessary conditional check.

The removal of the conditional existence check before unset($this[$key]) is a positive change. In PHP, unset() is safe to call on non-existent array keys and doesn't produce errors or warnings, making the existence check redundant. This simplification improves performance by eliminating an unnecessary isset() call while maintaining the same functionality.

tests/e2e/Adapter/Scopes/PermissionTests.php (1)

17-132: Well-structured test for permission unsetting behavior.

The test comprehensively validates the permission handling changes:

  1. Creates documents with full permissions and verifies creation
  2. Updates without specifying permissions and confirms existing permissions are preserved
  3. Updates with empty permissions array and verifies permissions are cleared
  4. Uses proper assertions and follows testing best practices

if (\array_key_exists($key, (array)$this)) {
unset($this[$key]);
}
unset($this[$key]);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

No need to check + copy array since unset is silent

@abnegate abnegate merged commit 8fa21e9 into main Aug 5, 2025
15 checks passed
@abnegate abnegate deleted the fix-encode-permissions branch August 5, 2025 04:57
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.

3 participants