-
Notifications
You must be signed in to change notification settings - Fork 109
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
REST API logic in Optimization Detective can be encapsulated into a static class #1859
Comments
Hi @westonruter, If I had correctly understood, we need to update the file |
Yes, to |
Yes, thanks for the clarification, I would proceed with the changes and would update once done. |
Previous reference to this issue: #1098 (comment) |
I've merged #1863 so you can now branch off of |
Curious why we should use a static class instead of regular class that is instantiated. The Core endpoints are all written in the latter way. |
It doesn't have to be static but there's not really any value in it not being static. The classes for the storage lock and post type are also static. |
But there's also no value in being static :) Instances have a general advantage of being easier to test in case they have any state, because you can just create a new instance for every test. With static classes, you would need workarounds for this kind of thing. This may not be a huge concern depending on what the class does here, but worth considering. |
The value in being static is that you don't introduce a variable that isn't used. If you want to unhook something, you can use the class name. Otherwise, you have to hope there is an instance of the class in a global variable. |
The question to ask ourselves here is whether we want to allow unhooking what this class does. Obviously, technically everything can be unhooked via some kind of hack (similar how one could even access private PHP methods via hacking with In this particular case, I question why unhooking the REST API logic would be useful. Allowing to unhook everything just for the sake of it is not a great pattern, and unhooking the REST API logic from Optimization Detective would simply break Optimization Detective. |
Maybe you want to override the endpoint with something else for testing purposes or experimental purposes. Like Gutenberg unhooks things in core when it is introducing a new version of something. In any case, we should be consistent with what is currently in the codebase: performance/plugins/optimization-detective/hooks.php Lines 20 to 21 in d21b74d
If we want to use a non-static class then the other classes should be updated as well. In this case, would these lines become: ( new OD_URL_Metrics_Post_Type() )->add_hooks();
( new OD_Storage_Lock() )->add_hooks(); Or would we put these in new global variables? $od_url_metrics_post_type = new OD_URL_Metrics_Post_Type();
$od_url_metrics_post_type->add_hooks();
$od_storage_lock = new OD_Storage_Lock();
$od_storage_lock->add_hooks(); |
Fair point, I agree whatever we go with should be consistent throughout the plugin. My personal preference would be the Since even in Core though, introducing new globals is no longer encouraged, I think it depends:
At the end of the day, I don't feel very strongly about this, despite being in favor of the above. Since we're already in 1.0.0-bet though, we should also consider whether these APIs are currently marked private or not. If not, it would feel wrong to change the APIs now in this way as it would be a huge BC break and would ideally have been done in a 0.x version. WDYT? |
These APIs are all private, yes. Both That said, even though they are private, I am using them in the Admin UI plugin (which I don't think is a problem since it is not publicized). Also this is a somewhat experimental extension to core OD itself and not intended as a model for others to follow. These are the currently-private symbols I'm using in that plugin:
Some of these would make sense to be public. But others wouldn't necessarily have to be. For example, in two places in the frontend context I'm manually instantiating the ⚠ I just realized that Image Prioritizer is also referencing the private constants in performance/plugins/image-prioritizer/helper.php Lines 297 to 304 in d21b74d
|
👍
Is clearing out the variable relevant? If this is a theoretical concern at this point, I wouldn't worry about it. I wouldn't look at something like Alternatively, if we don't want to introduce a global function, we could introduce an
Probably fine to remove the private annotation on them. The constants are technically public anyway, and it's fine that we're stuck with them forever. The good thing is that they're constants, so we could potentially change the value while still maintaining backward compatibility, as long as we make it clear that you should always use the constant if you want to refer to the endpoint. |
It's not theoretical as unit tests wouldn't work without it being reset. |
Can you share an example? Ideally, most unit tests that test for specific scenarios shouldn't use the main instance anyway, but use a new instance to test the specific scenario. That's what the encapsulation from using instances is supposed to enable in the first place. Testing the whole functionality using the main instance can also be useful of course, but it's mostly an integration test then and I'd argue for that we should go with whatever the default behavior is, while using separate instances to cover specific scenarios. |
For example, all of the snapshot tests involve passing the entire HTML of the buffer into the
|
Feature Description
Optimization Detective currently has static classes for
OD_Storage_Lock
andOD_URL_Metrics_Post_Type
, but it has arest-api.php
includes file with global functions and constants. The REST API logic would make sense to also put into a static class so there is consistency. It would also help with reducing the API surface for functions not needing to be public.The text was updated successfully, but these errors were encountered: