Skip to content

WP/AlternativeFunctions: add XML documentation#2687

Merged
GaryJones merged 5 commits intoWordPress:developfrom
rodrigoprimo:alternative-functions-docs
Feb 24, 2026
Merged

WP/AlternativeFunctions: add XML documentation#2687
GaryJones merged 5 commits intoWordPress:developfrom
rodrigoprimo:alternative-functions-docs

Conversation

@rodrigoprimo
Copy link
Collaborator

Description

This PR adds XML documentation for the WordPress.WP.AlternativeFunctions sniff.

The documentation is based on the work started by @pamprn09 in #2496 and @bhubbard in #2588. I squashed the original commits and, in a separate commit, made subsequent changes based on code review suggestions.

I suggest squashing those two commits before merging. I'm opening the PR without doing that to make it easier to tell my changes apart from the original changes.

While reviewing #2588, I mentioned the following:

I wonder whether the documentation should contain a single generic <standard>/<code_comparison> block that mentions that some PHP functions shouldn't be used and WP functions should be used instead, or if it should contain one <standard>/<code_comparison> per function group. I can see examples of both approaches in the codebase: WordPress.WP.DeprecatedFunctions uses the generic and short approach, while WordPress.DateTime.RestrictedFunctions uses the function group approach (but the sniff contains just two function groups, while WordPress.WP.AlternativeFunctions contains much more than that).

In this PR, I opted for the first approach and created a single <standard>/<code_comparison> block. I include a few examples using the main "categories" of functions flagged by the sniff, without including every single group, to avoid having too many examples. Let me know if you prefer one <standard>/<code_comparison> per function group or something else.

Suggested changelog entry

N/A

Related issues/external references

Related to: #1722
Supersedes: #2496 and #2588
Closes #2496
Closes #2588

pamprn09 and others added 2 commits January 30, 2026 15:54
Co-authored-by: pamprn09 <pamprn09@users.noreply.github.com>
Co-authored-by: bhubbard <bhubbard@users.noreply.github.com>
This commit makes a few changes to the original commit that suggested
adding documentation for `AlternativeFunctions`:
- Use a single generic description without specific reasons, as the reasons
  vary by function and may change as new functions are added.
- Remove the translation and escaping examples, which are not flagged by
  this sniff.
- Include a few examples using the main "categories" of functions flagged by
the sniff, without including every single group to avoid having too many
examples.
- Use generic titles for the code comparison blocks.
@jrfnl
Copy link
Member

jrfnl commented Feb 1, 2026

In this PR, I opted for the first approach and created a single /<code_comparison> block. I include a few examples using the main "categories" of functions flagged by the sniff, without including every single group, to avoid having too many examples. Let me know if you prefer one /<code_comparison> per function group or something else.

I agree with the single <standard> block approach, but can imagine the code comparisons may be clearer if they were split into multiple <code_comparison> blocks. @dingo-d @GaryJones What do you think ?

@dingo-d
Copy link
Member

dingo-d commented Feb 2, 2026

In this PR, I opted for the first approach and created a single /<code_comparison> block. I include a few examples using the main "categories" of functions flagged by the sniff, without including every single group, to avoid having too many examples. Let me know if you prefer one /<code_comparison> per function group or something else.

I agree with the single <standard> block approach, but can imagine the code comparisons may be clearer if they were split into multiple <code_comparison> blocks. @dingo-d @GaryJones What do you think ?

Sounds good to me. I think this will help with readability.

@rodrigoprimo
Copy link
Collaborator Author

@jrfnl, do you mean one <code_comparison> per function group defined in the sniff, or per the broader categories I used in the examples (HTTP, JSON, URL parsing, etc)?

@jrfnl
Copy link
Member

jrfnl commented Feb 2, 2026

@jrfnl, do you mean one <code_comparison> per function group defined in the sniff, or per the broader categories I used in the examples (HTTP, JSON, URL parsing, etc)?

I'm leaving that up to you. Won't know what I'd prefer until I see it ;-)

@rodrigoprimo
Copy link
Collaborator Author

After playing with the options we discussed, I ended up preferring a single <code_comparison> with one representative example, similar to DeprecatedFunctionsStandard.xml.

I prefer this approach because I don't want to include every single function the sniff checks for to avoid the maintenance burden of updating the list whenever a function is added or removed. And a partial list left me wondering what value multiple examples add to the documentation, while also raising the question of why include those functions and not the others.

What do you think of this approach?

Copy link
Member

@dingo-d dingo-d left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me 👍🏼

Copy link
Member

@jrfnl jrfnl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rodrigoprimo Looking at the doc as it is now, I think this doc doesn't add any value in its current state.

I believe we should explain why people should follow this rule, not just blindly repeat the rule.

In nearly all cases, the WP native versions of these functions were created to overcome

  • differences in PHP cross-version behaviour, or
  • differences in cross-OS behaviour, or
  • differences in server configurations etc.

I believe it is probably helpful to mention something along those lines in the <standard> description.

We may actually want to go back to some other recently merged docs to double-check if we should improve the clarity of the information in those too.

@rodrigoprimo
Copy link
Collaborator Author

I believe we should explain why people should follow this rule, not just blindly repeat the rule.

I pushed a new commit, adding a second paragraph to the standard description explaining why these functions should be used. Let me know what you think.

We may actually want to go back to some other recently merged docs to double-check if we should improve the clarity of the information in those too.

That is a good point. I will review the recently merged docs PRs and create follow-up PRs adding the why to the standard description wherever applicable. At first glance, here are the PRs I plan to address: #2676, #2679, #2680, and #2689.

Copy link
Member

@jrfnl jrfnl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for making that update @rodrigoprimo, the description looks much better now.

When looking at the docs again though, I realized that the one example which is left now, while correct, is also one which will only conditionally be flagged (when we suspect an external URL is being requested, not an internal file).

This sniff flags various other functions unconditionally. I imagine, it might lessen confusion if the code sample referenced one of those instead.
Think: wp_rand() vs mt_rand() or wp_parse_url() vs parse_url() (yes, I know there is a condition for that one too, but condition is dated, so I hope that it will rarely come into play nowadays).

Replace the file_get_contents()/wp_remote_get() example with
mt_rand()/wp_rand() as suggested during the review of the PR.
@rodrigoprimo
Copy link
Collaborator Author

Ok, I replaced the file_get_contents()/wp_remote_get() example with mt_rand()/wp_rand() as suggested.

Copy link
Member

@jrfnl jrfnl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for fixing that up @rodrigoprimo ! All good from me.

@jrfnl jrfnl requested a review from dingo-d February 23, 2026 22:09
@jrfnl
Copy link
Member

jrfnl commented Feb 23, 2026

@dingo-d As quite a lot has changed since you reviewed, I've re-requested your review. Hope you don't mind.

@jrfnl jrfnl added this to the 3.3.x milestone Feb 23, 2026
@jrfnl
Copy link
Member

jrfnl commented Feb 24, 2026

For whomever does the second review: please squashmerge.

@GaryJones GaryJones merged commit b4ca43c into WordPress:develop Feb 24, 2026
31 checks passed
@jrfnl jrfnl modified the milestones: 3.3.x, 3.4.0 Feb 24, 2026
@rodrigoprimo rodrigoprimo deleted the alternative-functions-docs branch February 25, 2026 12:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants