Avoid passing positional parameters in Optimization Detective#1338
Merged
westonruter merged 3 commits intotrunkfrom Jul 10, 2024
Merged
Avoid passing positional parameters in Optimization Detective#1338westonruter merged 3 commits intotrunkfrom
westonruter merged 3 commits intotrunkfrom
Conversation
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
a4f99d3 to
ce07f44
Compare
ce07f44 to
9662ed6
Compare
9662ed6 to
74ac1bb
Compare
… add/tag-visitor-context * 'trunk' of https://github.com/WordPress/performance: Add missing covers tags Pass explicit param to indicate whether processing fragment or entire document Improve copy Add note explaining why require_once is in function Add preconnects for the most popular embeds
Member
Author
|
The base PR has been merged so this is ready to go! |
Member
Author
See also proposed usage in the auto-sizes plugin: https://github.com/WordPress/performance/pull/1322/files |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This fixes something that has been bugging me for awhile.
The way that you register new tag visitors for Optimization Detective is via the
od_register_tag_visitorsaction. At the moment, this action is passed instances of the following three classes as positional parameters:OD_Tag_Visitor_RegistryOD_URL_Metrics_Group_CollectionOD_Link_CollectionIn reality, only the first parameter is needed. The other two are extras for convenience. Nevertheless, passing them as positional parameters means that developers have to remember which one is which, and when new possible classes are introduced, they just get added on to the end. The situation is similar for the tag visitor, as this callable is passed instances of the following classes:
OD_HTML_Tag_ProcessorOD_URL_Metrics_Group_CollectionOD_Link_CollectionAgain, when writing a tag visitor and you need to use the
OD_Link_Collectioninstance to add a link to the head, you have to skip over an unusedOD_URL_Metrics_Group_Collectioninstance:Not only is the
$url_metrics_group_collectionnot used here, but the possible strict type hints on the tag visitor callable mean that these positional parameters would be locked in stone forever. There would not be any flexibility to reorder the parameters or change the names of the classes.These issues can be fixed by introducing a context object that has these class instances exposed as properties. Compare the above with the below:
The benefits here are:
Similarly, there is no need for the additional parameters being passed to the
od_register_tag_visitorssince the parameters are passed as part of the context anyway. So they can just be eliminated entirely. They are not needed by Image Prioritizer or Embed Optimizer.