Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move "snake case" related utility to dedicated SnakeCaseHelper + bug fix in method #2190

Merged
merged 2 commits into from
Jan 10, 2023

Conversation

jrfnl
Copy link
Member

@jrfnl jrfnl commented Jan 10, 2023

Move "snake case" related utility to dedicated SnakeCaseHelper

The "snake case" related utility method is only used by a small set of sniffs, so is better placed in a dedicated class.

This commit moves the get_snake_case_name_suggestion() method to the new WordPressCS\WordPress\Helpers\SnakeCaseHelper and starts using that class in the relevant sniffs.

Note:
It is expected for PHPCSUtils to have dedicated methods for the same at some point in the future. If/when those methods become available, it is recommended for the sniffs to start using the PHPCSUtils methods.
With that in mind, this class is marked as not part of the public API.

Related to #1465

SnakeCaseHelper::get_suggestion(): bug fix - underscore handling

The SnakeCaseHelper::get_suggestion() method should have no opinion on the use of leading/trailing/double underscores in a name.

In practice, however, it did and the suggested name would be incorrect.

Fixed now.

Includes tests to safeguard this fix in the ValidFunctionName test case file, though the current methodology for testing would not catch a regression.

To illustrate the issue - originally, the error messages for these new tests would read:

216 | ERROR | Method name "___Leading_Underscores" in class UnderScoreHandling is not in snake case format, try "leading_underscores"
 217 | ERROR | Method name "Multiple_______Underscores" in class UnderScoreHandling is not in snake case format, try "multiple____underscores"
 218 | ERROR | Method name "Trailing_Underscores___" in class UnderScoreHandling is not in snake case format, try "trailing_underscores"

With the fix, the error messages for the new tests read:

 216 | ERROR | Method name "___Leading_Underscores" in class UnderScoreHandling is not in snake case format, try "___leading_underscores"
 217 | ERROR | Method name "Multiple_______Underscores" in class UnderScoreHandling is not in snake case format, try "multiple_______underscores"
 218 | ERROR | Method name "Trailing_Underscores___" in class UnderScoreHandling is not in snake case format, try "trailing_underscores___"

The "snake case" related utility method is only used by a small set of sniffs, so is better placed in a dedicated class.

This commit moves the `get_snake_case_name_suggestion()` method to the new `WordPressCS\WordPress\Helpers\SnakeCaseHelper` and starts using that class in the relevant sniffs.

**Note**:
It is expected for PHPCSUtils to have dedicated methods for the same at some point in the future. If/when those methods become available, it is recommended for the sniffs to start using the PHPCSUtils methods.
With that in mind, this class is marked as not part of the public API.

Related to 1465
The `SnakeCaseHelper::get_suggestion()` method should have no opinion on the use of leading/trailing/double underscores in a name.

In practice, however, it did and the suggested name would be incorrect.

Fixed now.

Includes tests to safeguard this fix in the `ValidFunctionName` test case file, though the current methodology for testing would not catch a regression.

To illustrate the issue - originally, the error messages for these new tests would read:
```
216 | ERROR | Method name "___Leading_Underscores" in class UnderScoreHandling is not in snake case format, try "leading_underscores"
 217 | ERROR | Method name "Multiple_______Underscores" in class UnderScoreHandling is not in snake case format, try "multiple____underscores"
 218 | ERROR | Method name "Trailing_Underscores___" in class UnderScoreHandling is not in snake case format, try "trailing_underscores"
```

With the fix, the error messages for the new tests read:
```
 216 | ERROR | Method name "___Leading_Underscores" in class UnderScoreHandling is not in snake case format, try "___leading_underscores"
 217 | ERROR | Method name "Multiple_______Underscores" in class UnderScoreHandling is not in snake case format, try "multiple_______underscores"
 218 | ERROR | Method name "Trailing_Underscores___" in class UnderScoreHandling is not in snake case format, try "trailing_underscores___"
```
Copy link
Member

@GaryJones GaryJones left a comment

Choose a reason for hiding this comment

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

@dingo-d dingo-d merged commit 3c0ce82 into develop Jan 10, 2023
@dingo-d dingo-d deleted the feature/move-snakecase-method-to-helper-class_ branch January 10, 2023 14:17
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.

3 participants