-
Notifications
You must be signed in to change notification settings - Fork 21
Fix two more false positives in EnforceReadonlyPublicPropertyRule
#334
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
|
Even tho I'm not satisfied with this rule anymore (considering removal upon v5), I think the intention there was to guard public props to be encapsulated. And both those issues are easily fixable on PHP 8.0+ in 99% cases (edited). You can move default value to ctor and you can add native typehint. The only cases where you cannot add typehint are:
I think I want to keep it as-is regarding those two. |
|
So, if you use PHP 7.4, I suggest disabling the rule. If 8+, you dont need this adjustment. |
|
Thanks for your feedback and taking the time to review. I can understand your reasoning, but I think it would still be very useful to keep this rule. It helped me track down properties that should should've been readonly (or their classes should've been readonly). I'm on PHP 8.4 by the way. The reason I "need" these fixes is due to false positives on properties that extend vendor code or are expected to be defined/used in a certain way. Let me share two concrete examples:
<?php
namespace App\Jobs;
class ProcessPodcast implements ShouldQueue
{
public int $tries = 5;
}Because of the default value this property cannot be readonly. Assigning the value in the constructor is possible, but kind of unconventional.
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Flight extends Model
{
public $timestamps = false;
}This property exists on the parent class and is untyped. Adding What are your thoughts on these examples? |
|
I think we can make this mergeable if the default value one would be configurable and would behave the same way as before by default: enforceReadonlyPublicProperty: structure([
enabled: bool()
excludePropertyWithDefaultValue: bool() # defaults to false
])Ensuring native typehint is present should be done via some other rule (or sniff), so we can probably keep that change. |
|
@janedbal I've made the changes you suggested. Could you review again? |
Thanks for merging my previous patch so quickly!
Two more small fixes to the same rule:
https://www.php.net/manual/en/language.oop5.properties.php#language.oop5.properties.readonly-properties