-
-
Notifications
You must be signed in to change notification settings - Fork 490
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
Forbid the addition of new magic class methods #2445
Comments
I think this ticket needs to be made more specific as there are more magic methods than mentioned above, including For the sake of discussion, let's presume the OP meant to limit the ticket to the magic In that case, I'm in favour of the principle of this, as nearly all magic Having said that, I do believe there should be (at least) one exception: While the magic methods implemented could still be flawed, at least there won't be any problems related to not dealing with inheritance correctly in This exception should, IMO, not be encoded in the sniff, but should be handled via a separate error code, which can be excluded from a ruleset when desired. It is also my opinion that this functionality should not be added to a pre-existing sniff, but should be encoded in a new sniff. I can also imagine a sister-sniff, which safeguards that magic methods when implemented, are always implemented in pairs for the following:
☝🏻 The above should probably also have an exception, i.e. when the only code within the magic method is a All in all, I think, this needs a proper, detailed discussion about the specs of any such sniffs. As things are, the ticket is too broadly formulated to be actionable for creating sniffs which will not be turned off by the majority. While the discussion on the sniff can remain here, I also think that if the final specifications are generically useful enough, i.e. not too WP specific, the sniff(s) should be created in PHPCSExtra. |
This made me rethink what I actually meant. I will be more specific next time. If a class has only the There is no reason to use Example:
The fact that the final class cannot be extended doesn't make introducing new magic methods more justified. For example. I see no reason to use
That sounds great. I like the idea of having a complementary sniff that would enforce pairs of magic methods for existing classes (with the exception of the case with |
While what you say is true, I don't agree that's always the best way. Using Introducing a "getter" method, will, depending on the size of the class and the number of properties this applies to, litter the class with dozens of extra methods, which will become superfluous once the properties becomes I.e. using the "getter" pattern creates a huge burden of technical debt, which can never be fixed. Even more so, if there would be some simple logic needed in the "getter"/ |
Note: in any "normal" codebase, that, of course, wouldn't be a consideration, but it is for WP with its awkward "no-BC breaks" policy. With that in mind, a little thinking ahead, knowing what PHP features will be in reach before long, is not a bad thing. |
I see no reason to introduce new "fake" read-only properties, as class methods are better suited for retrieving data. I was referring only to new code, not existing "fake" read-only properties. Of course, existing "fake" read-only properties likely don't need to be replaced with getter methods, and I absolutely agree that doing so would clutter the class. I have no bias against using the |
For new code, there is all the more reason not to clutter the codebase with nonsense getter methods. Either way, none of this is helping to make the ticket actionable, as it is still way too vague. |
Is your feature request related to a problem?
Adding new magic methods such as
__get
and__set
to WordPress Core should be discouraged unless necessary to fix existing dynamic class properties.There is no good reason to use magic methods unless someone is writing an ORM library (which is not likely the case with WordPress Core).
PHP classes can almost always function just fine without them.
There have also been relatively recent examples of adding
__get
magic methods to new classes in WordPress versions 6.1 and 6.5 to provide backward compatibility. However, I'm not sure if that qualifies as a good reason to use magic methods in entirely new code.Describe the solution you'd like
There could be a sniff that can take care of that (
PrefixAllGlobals
comes to mind).I'm fine with this issue being closed without long explanations, as this idea may have already been explored and rejected.
Additional context (optional)
The text was updated successfully, but these errors were encountered: