Skip to content

[11.x] Fix: Ensure generated policies return boolean values#53630

Closed
Aluisio-Pires wants to merge 1 commit intolaravel:11.xfrom
Aluisio-Pires:fix/aluisio/policies-phpstan
Closed

[11.x] Fix: Ensure generated policies return boolean values#53630
Aluisio-Pires wants to merge 1 commit intolaravel:11.xfrom
Aluisio-Pires:fix/aluisio/policies-phpstan

Conversation

@Aluisio-Pires
Copy link
Copy Markdown
Contributor

Summary

This PR resolves an issue where the methods in policies generated via commands such as make:model ModelName --policy returned a bool type but lacked implementations, causing PhpStan to report type errors. The problem stems from the empty method bodies in the default policy stubs.

Changes Made

  • Updated the policy stub to include default true return values for all methods:
    • viewAny
    • view
    • create
    • update
    • delete
    • restore
    • forceDelete

This ensures the generated methods comply with their defined return types and eliminates PhpStan type-checking errors.

How to Test

  1. Generate a new model with a policy using php artisan make:model Example --policy.
  2. Inspect the generated policy methods for default true return values.
  3. Confirm that running static analysis (e.g., PhpStan) no longer raises type errors for the generated policy.

Impact

This change improves developer experience by making generated policies immediately usable without requiring manual fixes to satisfy static analysis.

Let me know if further details are needed!

@Jubeki
Copy link
Copy Markdown
Contributor

Jubeki commented Nov 22, 2024

This would be a security issue. If this were to be changed, it should be deny by default.

@taylorotwell
Copy link
Copy Markdown
Member

See above.

@Aluisio-Pires
Copy link
Copy Markdown
Contributor Author

This would be a security issue. If this were to be changed, it should be deny by default.

Really good point. I was avoiding the same FormRequests behavior were we are forced to change the authorize return, but I see the issue. I will reopen de PR changing to false.

@Aluisio-Pires
Copy link
Copy Markdown
Contributor Author

See above.

@taylorotwell I've created a new PR following the recommendations.

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