Skip to content

Conversation

@GautierDele
Copy link
Member

@GautierDele GautierDele commented May 28, 2025

Summary by CodeRabbit

  • Bug Fixes
    • Improved error handling when accessing the control property, preventing potential issues if the property is undefined.
  • Refactor
    • Updated logic for resolving and instantiating control classes to use static property and method checks for greater robustness and consistency.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented May 28, 2025

Walkthrough

This change updates several internal mechanisms for handling "control" classes and properties. It standardizes namespace usage in generated code, switches from method to property checks for control resolution, adds property existence checks before instantiating controls, and changes how control instances are retrieved within query scopes.

Changes

File(s) Change Summary
src/Console/ControlMakeCommand.php Prepends a backslash to perimeter class names for absolute namespaces and adds a blank line in generated code.
src/Controls/Control.php Changes control name resolution to check for a static property instead of a method on the model class.
src/Controls/HasControl.php Adds a property existence check before instantiating the static control property in newControl().
src/Controls/HasControlScope.php Switches from calling newControl() on model instances to calling the static control() method on the class.

Sequence Diagram(s)

sequenceDiagram
    participant Builder
    participant Model
    participant Control

    Builder->>Model: getModel()
    Model-->>Builder: Model class
    Builder->>Model: ::control()
    Model->>Control: instantiate Control
    Control->>Builder: queried(builder, user)
Loading

Possibly related PRs

Poem

In the warren of code, a control is born,
With namespaces tidy and checks newly sworn.
No more surprises—properties in sight,
Static or not, we’ll get it right!
Hopping through scopes, our logic refined—
A rabbit’s delight in the code we designed.
🐇✨

✨ Finishing Touches
  • 📝 Generate Docstrings

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.
    • @coderabbitai modularize this function.
  • 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.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

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

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

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 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.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

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: 0

🧹 Nitpick comments (1)
src/Console/ControlMakeCommand.php (1)

132-132: Consider removing trailing whitespace

The added blank line after the query closure may be unintentional formatting. Consider removing trailing whitespace unless it serves a specific purpose in the generated code structure.

                    }),
-                    
+                    
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between bdd9c1f and d300d3c.

📒 Files selected for processing (4)
  • src/Console/ControlMakeCommand.php (2 hunks)
  • src/Controls/Control.php (1 hunks)
  • src/Controls/HasControl.php (1 hunks)
  • src/Controls/HasControlScope.php (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (2)
src/Controls/HasControlScope.php (1)
src/Controls/HasControl.php (1)
  • control (22-27)
src/Controls/HasControl.php (1)
src/Controls/Control.php (1)
  • new (221-224)
🔇 Additional comments (4)
src/Controls/HasControl.php (1)

36-36: Excellent defensive programming improvement!

The addition of property_exists() check prevents fatal errors when the control property is not defined on a model class. This aligns well with the broader refactoring from method-based to property-based control resolution.

src/Console/ControlMakeCommand.php (1)

119-119: Good practice: Absolute namespace reference

Adding the leading backslash ensures the perimeter class name is treated as an absolute namespace path, preventing potential conflicts with relative namespace resolution.

src/Controls/Control.php (1)

240-242:

✅ Verification successful

Verify consistency of property-based control resolution

The change from method_exists to property_exists aligns with the broader refactoring to standardize control discovery. However, ensure all control usages throughout the codebase are consistent with this property-based approach.

Let me verify that all models using controls have the static control property defined correctly:


🏁 Script executed:

#!/bin/bash
# Description: Find all model classes that might be using controls and verify they have the control property defined properly

# Search for models that use HasControl trait
echo "Models using HasControl trait:"
rg -A 5 -B 5 "use.*HasControl" --type php

echo -e "\n=== Static control property definitions ==="
# Search for static control property definitions
rg "static.*\\\$control" --type php

echo -e "\n=== Control property access patterns ==="
# Search for control property access patterns
rg "::control\(\)" --type php

Length of output: 2113


Property-based control resolution verified

  • The only model using the HasControl trait (tests/Support/Models/Model.php) defines protected static $control.
  • No method_exists(..., 'control') calls remain in the codebase.
  • All control() invocations in Control.php, HasControl.php, and HasControlScope.php honor the property_exists(..., 'control') check.
src/Controls/HasControlScope.php (1)

57-57:

❓ Verification inconclusive

Consistent with the control discovery refactoring

The change from instance method newControl() to static method control() aligns with the standardization across the codebase. This leverages the static control() method defined in the HasControl trait (lines 22-27 in src/Controls/HasControl.php).

Let me verify that all models using this scope properly implement the required static control method:


🏁 Script executed:

#!/bin/bash
# Description: Verify that models using HasControlScope have the necessary control() method available

echo "Searching for HasControlScope usage:"
rg -A 10 -B 5 "HasControlScope" --type php

echo -e "\n=== Models with HasControl trait (should have control() method) ==="
rg -A 3 "use.*HasControl" --type php

echo -e "\n=== Potential models without HasControl trait but using controlled() queries ==="
rg -B 5 -A 5 "->controlled\(\)" --type php

Length of output: 3270


Ensure all Eloquent models using this scope implement control()

The switch from the instance method newControl() to the static control() call is correct and aligns with the HasControl trait’s refactoring. However, I wasn’t able to find any application models (outside of the test suite) importing the HasControl trait—and thus exposing the static control() method—that would make use of this scope. Please verify that:

  • Every model meant to be governed by this scope imports Lomkit\Access\Controls\HasControl;
  • Each of those models either defines the static $control property or otherwise provides a public static function control()

Without that, calls to $builder->getModel()::control() will fail at runtime.

@GautierDele GautierDele merged commit 007f5c9 into master May 28, 2025
21 checks passed
@GautierDele GautierDele deleted the fix/auto-discovery branch May 28, 2025 16:16
@coderabbitai coderabbitai bot mentioned this pull request Jul 27, 2025
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