Skip to content

Conversation

brentwilson-clariio
Copy link

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

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.

@brentwilson-clariio Thanks for picking this up! I've just had a quick look and left some feedback for you to consider.

Comment on lines 11 to 18
Note: Other type-cast rules in the WordPress standard
are enforced by different sniffs:
- Short forms like (int), (bool), (string):
PSR12.Keywords.ShortFormTypeKeywords
- No spaces inside parentheses:
Squiz.WhiteSpace.CastSpacing
- Exactly one space after the cast:
Generic.Formatting.SpaceAfterCast
Copy link
Member

Choose a reason for hiding this comment

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

This whole block doesn't belong here. Each sniff functions independently of other sniffs. That also means that the docs should be independent of each other and should just focus on what this sniff does.

Choose a reason for hiding this comment

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

Thanks! I’ve removed the paragraph referencing other sniffs. The docs now only describe what WordPress.PHP.TypeCasts enforces.

<code_comparison>
<code title="Invalid: legacy float casts">
<![CDATA[
<?php
Copy link
Member

Choose a reason for hiding this comment

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

PHP open tag is not needed here. (Same for the "invalid" code sample)

Choose a reason for hiding this comment

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

Removed <?php from all code samples as suggested.

<documentation title="Type Casts">
<standard>
<![CDATA[
This sniff normalizes float type cast keywords.
Copy link
Member

Choose a reason for hiding this comment

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

This sentence seems redundant.

Choose a reason for hiding this comment

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

Reworded the opener to be shorter and clearer. It now states the rules enforced without repeating itself.

@@ -0,0 +1,38 @@
<?xml version="1.0"?>
<documentation title="Type Casts">
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
<documentation title="Type Casts">
<?xml version="1.0"?>
<documentation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://phpcsstandards.github.io/PHPCSDevTools/phpcsdocs.xsd"
title="Type Casts"
>

Choose a reason for hiding this comment

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

Added the schema header and xsi:noNamespaceSchemaLocation pointing to phpcsdocs.xsd.

Comment on lines 7 to 9
Use (float) instead of legacy synonyms:
- Disallowed: (double), (real)
- Allowed: (float)
Copy link
Member

Choose a reason for hiding this comment

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

This is incorrect/incomplete.

The sniff:

  • Forbids the use of non-standard casts for floats.
  • Forbids the use of the (unset) cast.
  • Discourages the use of the (binary) cast.

Considering the above, I believe the code samples also need work.

Choose a reason for hiding this comment

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

Updated the standard text and the code examples to cover all three behaviors:
• (double)/(real) → error (normalize to (float))
• (unset) → error (removed in PHP 8.0; suggest unset())
• (binary) → warning (discouraged)

</standard>

<code_comparison>
<code title="Invalid: legacy float casts">
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
<code title="Invalid: legacy float casts">
<code title="Invalid: Using legacy float casts.">

Choose a reason for hiding this comment

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

Adjusted the code block titles to your suggested wording.

$size = <em>(real)</em> $value;
]]>
</code>
<code title="Valid: normalized float cast">
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
<code title="Valid: normalized float cast">
<code title="Valid: Using normalized float casts.">

Choose a reason for hiding this comment

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

Adjusted the code block titles to your suggested wording.

Choose a reason for hiding this comment

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

Updated code block titles to “Valid: Using normalized float casts.” (and similar phrasing for the other examples), as suggested.

]]>
</standard>

<code_comparison>
Copy link
Member

Choose a reason for hiding this comment

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

The code comparison should have "valid" on the left (first code sample), "invalid" on the right (second code sample).

Choose a reason for hiding this comment

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

Thanks for the feedback! I’ve reordered the code examples so that Valid is on the left and Invalid is on the right, as suggested.

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.

2 participants