dev: use WP_Abilities_Registry::$instance instead of adding a new global - #19
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## trunk #19 +/- ##
========================================
Coverage ? 90.64%
Complexity ? 99
========================================
Files ? 7
Lines ? 556
Branches ? 0
========================================
Hits ? 504
Misses ? 52
Partials ? 0
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
gziolo
left a comment
There was a problem hiding this comment.
I don't have a strong preference here. WordPress uses globals for many fundamental singletones like $post, $wp_scripts, $wp_rest_server. However, we need to make it fully testable and be able to reset the instance between test runs.
Also unfortunately, our PHPUnit CI is passing when the tests itself fail - I'll take a look at that too! 😅 |
gziolo
left a comment
There was a problem hiding this comment.
Using reflection makes the test pass 👍

What
This PR changes
WP_Abilities_Registryto store it's instance on::$instance, instead of on a newglobal $wp_abilitiesvariable.(Note: I'm just code-reviewing and leaving feedback. If you think this warrants a wider discussion I can open a matching issue, otherwise I think a discussion here following by [accept | reject] is enough)
Why
Global variables are hard-to-type-and-test antipatterns. Just because legacy WP code used them before there were better patterns doesn't mean we need to introduce new ones.
Using a private instance gives core contributors and end-devs more insight into usage, and as developers working on the Abilities API, we get a much smaller surface for breaking changes to worry about.