WordPress-Coding-Standards icon indicating copy to clipboard operation
WordPress-Coding-Standards copied to clipboard

Docs: Add documentation for WordPress.PHP.TypeCasts

Open brentwilson-clariio opened this issue 4 months ago • 3 comments

Related to #1722

This PR adds a new documentation file: WordPress/Docs/PHP/TypeCastsStandard.xml

What it covers

Documents the rule enforced by the WordPress.PHP.TypeCasts sniff:

  • Normalizes legacy float casts ((double), (real)) to (float).

Notes

Other type-cast related rules in the WordPress standard are enforced by separate sniffs:

  • Short form keywords ((int), (bool), (string)): PSR12.Keywords.ShortFormTypeKeywords
  • No spaces inside parentheses: Squiz.WhiteSpace.CastSpacing
  • Exactly one space after the cast: Generic.Formatting.SpaceAfterCast

brentwilson-clariio avatar Aug 26 '25 22:08 brentwilson-clariio

I just realized something else. From php.net (https://www.php.net/manual/en/language.types.type-juggling.php):

Caution The (binary) cast and b prefix exists for forward support. Currently (binary) and (string) are identical, however this may change and should not be relied upon.

Since the manual says that there is no guarantee that (binary) and (string) will continue to be identical, I guess we should not recommend it in the documentation. Instead maybe the documentation should say that using (binary) is not recommended without providing an alternative? That may be the reason the sniff message does not recommend using (string).

rodrigoprimo avatar Oct 24 '25 19:10 rodrigoprimo

Since the manual says that there is no guarantee that (binary) and (string) will continue to be identical, I guess we should not recommend it in the documentation. Instead maybe the documentation should say that using (binary) is not recommended without providing an alternative? That may be the reason the sniff message does not recommend using (string).

Just so you know: the (binary) cast is deprecated as of PHP 8.5 and will be removed in PHP 9.0. The suggested replacement is using (string). See: https://wiki.php.net/rfc/deprecations_php_8_5#deprecate_non-standard_cast_names

jrfnl avatar Oct 24 '25 21:10 jrfnl

Thanks for your input, @jrfnl. In that case, I think that @brentwilson-clariio can ignore my last comment and the documentation can mention that (string) should be used instead of (binary).

rodrigoprimo avatar Oct 27 '25 12:10 rodrigoprimo

@brentwilson-clariio , I was just wondering if you'll have a chance to finish this off in the near future. It would be great if this PR could be included in the next WPCS release.

If you haven't got time or lost interest, please let us know, and we'll see if we can find someone to take over. Thanks!

rodrigoprimo avatar Dec 18 '25 10:12 rodrigoprimo

@rodrigoprimo Let me see if I can circle back to this in the next week or two to get it finished up.

brentwilson-clariio avatar Dec 18 '25 22:12 brentwilson-clariio