Skip to content

Conversation

delpieron-hash
Copy link

Improve the WordPress (WP) coding standards formatter (--wp flag) to achieve closer compliance with the official WordPress PHP Coding Standards. While full parity isn’t possible with existing formatters, this configuration achieves close to full coverage.

Changes

Expanded the WP class in fmt.stub.php with rules for additional formatting passes.

Disabled PSR-2 Rules

  • PSR2IndentWithSpace: WP uses real tabs, not spaces for indentation.
  • PSR2CurlyOpenNextLine: WP uses same-line braces.

Added WordPress Rules

  • LongArray / disable ShortArray: WP prescribes long array() syntax instead of short [] due to readability and beginner friendliness.
  • MergeElseIf / disable SplitElseIf: Due to colon syntax compatibility, elseif is preferred.
  • ExtraCommaInArray / disable StripExtraCommaInArray: Trailing commas are recommended for easier reordering and cleaner diffs.
  • YodaComparisons : Left-side constant placement is advised for ==, !=, === and !== conditions (5 === $value), but not for <, >, <=, >=. While not 100% compliant, enforcing yoda conditions expresses WP intent better.
  • SpaceAroundControlStructures, SpaceAfterExclamationMark, SpaceAroundParentheses: WP uses spaces in control structures and around operators: if ( ! $condition ), array( 1, 2, 3 ), foo() && bar().
  • AutoPreincrement: WP favors pre-increment (++$a) and pre-decrement (--$a) forms to post-increment/decrement ($a++ / $a--) due to performance and bug prevention.
  • AddMissingParentheses: Parentheses must be used for instantiation new Foo().
  • AlignEquals: WP uses spaces to align consecutive lines of code for readability.
  • AlignGroupDoubleArrow / disable AlignDoubleArrow: Alignment is determined contextually within code blocks.
  • disable EncapsulateNamespaces: Curly brace syntax not allowed for namespace declarations.
  • StripSpaceWithinControlStructures: WP encourages brace style without empty lines.

Removed Rules

  • StripNewlineWithinClassBody: Not required by WP coding standards.

Motivation

The original WordPress standards implementation (#33) provided only partial support and used some inconsistent PSR-2 defaults. This update expands the rule set for near-complete compliance with the WordPress PHP Coding Standards, making the formatter more reliable for WordPress plugin and theme developers.

References

Enhance WP formatter with comprehensive WordPress PHP coding standards:
- Long array syntax (array() not [])
- Tab indentation instead of spaces
- Yoda conditions for comparisons
- Spaces around control structures
- Merged elseif statements
- Pre-increment operators
- Variable and array alignment

Ref: https://developer.wordpress.org/coding-standards/wordpress-coding-standards/php/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant