Add three new sniffs for LearnDash coding standards#8
Add three new sniffs for LearnDash coding standards#8nikolaystrikhar wants to merge 6 commits intomainfrom
Conversation
- DisallowNonPrivateConstSniff: Prevents public/protected class constants - DisallowHardcodedPostTypeSlugsSniff: Catches hardcoded post type slugs - MultilineConditionFormattingSniff: Enforces proper multiline condition formatting Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
The sniff can now automatically fix: - Single-line conditions with multiple expressions → split to multiline - Operators at end of line → move to start of next line - Nested conditions with proper indentation alignment Run with: phpcbf --sniffs=LearnDash.CodeAnalysis.MultilineConditionFormatting Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Use a simple public $indent property (default: tab) instead of
auto-detection. This follows the WordPress sniff pattern and can
be configured in phpcs.xml if needed:
<rule ref="LearnDash.CodeAnalysis.MultilineConditionFormatting">
<properties>
<property name="indent" value=" "/>
</properties>
</rule>
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
| // Get the string content without quotes. | ||
| $content = trim( $token['content'], '"\'' ); |
There was a problem hiding this comment.
This check is too loose and results in false positives on text like
esc_html( learndash_get_custom_label_lower( 'groups' ) ),
There was a problem hiding this comment.
There is a huge problem with it though, how do we decide which context is right and wrong and maintain it easily? Imagine I skip one case and then it becomes problematic to update again, we add more functions... I don't like the maintainability aspect of this sniff. Maybe it was a bad idea and we just delete it. Thoughts @estevao90 @d4mation ?
src/LearnDash/Sniffs/Strings/DisallowHardcodedPostTypeSlugsSniff.php
Outdated
Show resolved
Hide resolved
| $fix = $phpcs_file->addFixableWarning( | ||
| 'Conditions with multiple boolean expressions should be split across multiple lines, with one condition per line.', | ||
| $open_paren, | ||
| 'SingleLineMultipleConditions' | ||
| ); |
There was a problem hiding this comment.
warnings don't cause CI to fail, so maybe we should use error instead? The other existing sniffs all use error.
| /** | ||
| * Fixes a single-line condition by splitting it across multiple lines. | ||
| * | ||
| * @param File $phpcs_file The file being scanned. | ||
| * @param int[] $operators Array of operator token positions. | ||
| * @param int $open_paren The opening parenthesis position. | ||
| * @param int $close_paren The closing parenthesis position. | ||
| * @param string $base_indent The base indentation. | ||
| * @param int $depth The nesting depth. | ||
| * | ||
| * @return void | ||
| */ | ||
| private function fix_single_line_condition( File $phpcs_file, array $operators, int $open_paren, int $close_paren, string $base_indent, int $depth ): void { |
There was a problem hiding this comment.
We may want an additional check to try to remove unnecessary parenthesis like this:
if (
( ! empty( $this->transient_data['result_count'] ) )
&& ( ! empty( $this->transient_data['total_count'] ) )
&& ( $this->transient_data['result_count'] !== $this->transient_data['total_count'] )
) {There was a problem hiding this comment.
Addressed, good idea! One note though, we can't fully trust it and should always check how it changes the code.
There was a problem hiding this comment.
I found some issues with indents and fixed them btw (it was mixing spaces and tabs)
…tter enforcement of coding standards. This update ensures that conditions with multiple boolean expressions and operators not at the start of the line are treated as errors, enhancing the automatic fixing capabilities of the sniff.
… standards enforcement. This class previously enforced the use of constants for post type slugs, but has been deemed unnecessary for current standards.
…cessary parentheses checks - Introduced a new method `get_normalized_indent` to standardize indentation handling. - Added functionality to check for unnecessary parentheses around single conditions, improving code clarity and adherence to coding standards. - Updated indentation calculations for better alignment of closing parentheses and nested conditions.
Summary
This PR adds two new sniffs to enforce LearnDash coding standards:
All sniffs are auto-fixable with
phpcbf.1.
LearnDash.Classes.DisallowNonPrivateConstPurpose
Public and protected class constants cannot be deprecated, so we should use static properties instead to allow for future deprecation.
What it catches
What passes
Error codes
PublicConst- Explicit public constantProtectedConst- Protected constantImplicitPublicConst- Constant without visibility modifier2.
LearnDash.CodeAnalysis.MultilineConditionFormattingPurpose
When conditions have multiple boolean expressions, they should be formatted for readability:
&&,||) at the beginning of lines, not the endWhat it catches
What passes (after auto-fix)
Error codes
SingleLineMultipleConditions- Multiple boolean expressions on a single lineOperatorNotAtLineStart- Boolean operator appears after other code on the same lineUnnecessaryParentheses- Parentheses around a single condition with no boolean operatorsSupported control structures
ifelseifwhileforswitchUsage
Add to your
phpcs.xml:Or run individually:
Test cases verified
MultilineConditionFormattingSniff
if ( $a && $b || $c )$a &&$b$a&& $b( ! empty( $a ) )! empty( $a )( $b || $c )with&&at endwhile ( $a && $b )elseif ( $a && $b )Test plan
🤖 Generated with Claude Code