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

Ruleset::explain(): refactor method to remove use of output buffers + add test #118

Merged
merged 1 commit into from
Dec 4, 2023

Conversation

jrfnl
Copy link
Member

@jrfnl jrfnl commented Dec 4, 2023

Description

As pointed out in squizlabs/PHP_CodeSniffer#3876 / #84, the Ruleset::explain() method was using output buffers incorrectly - it contained three ob_start() calls and only one ob_end*() call, which meant that at the end, there would be at least one unclosed output buffer.

The (incorrect) use of the output buffers also meant that any test calling this method would be marked as risky with a "Test code or tested code did not (only) close its own output buffers" notice.

In my opinion, the use of output buffers is unnecessary for the "explain" command, so I've refactored the code to remove the use of output buffering completely.

The actual function output remains the same, with only a tiny difference - where previously, the separator marker line for a standard of which only 1 sniff was used would be 1 dash too long, the separator marker line will now match the length of the title line for the standard (tiny bug fix).

This change now allows for the method to be tested, so a test has been added as well.

The test uses the PSR1 standard as:

  1. That standard is not expected to change anymore, which should make this test quite stable.
  2. That standard has "sub-standards" with both 1 as well as "more than 1" sniffs, meaning the output of the subtitles can be tested.

While it may seem redundant to have a test for this code as this is not code which changes often, the deprecation notice fixed in squizlabs/PHP_CodeSniffer#3876 / #84 would have been caught by this test, while now it was only by the luck of the draw of me running phpcs -e while on PHP 8.3 (early) that we found that deprecation.

Suggested changelog entry

N/A (internal change only)

… add test

As pointed out in 3876 / 84, the `Ruleset::explain()` method was using output buffers incorrectly - it contained three `ob_start()` calls and only one `ob_end*()` call, which meant that at the end, there would be at least one unclosed output buffer.

The (incorrect) use of the output buffers also meant that any test calling this method would be marked as risky with a "Test code or tested code did not (only) close its own output buffers" notice.

In my opinion, the use of output buffers is unnecessary for the "explain" command, so I've refactored the code to remove the use of output buffering completely.

The actual function output remains the same, with only a tiny difference - where previously, the separator marker line for a standard of which only 1 sniff was used would be 1 dash too long, the separator marker line will now match the length of the title line for the standard (tiny bug fix).

This change now allows for the method to be tested, so a test has been added as well.

The test uses the PSR1 standard as:
1. That standard is not expected to change anymore, which should make this test quite stable.
2. That standard has "sub-standards" with both 1 as well as "more than 1" sniffs, meaning the output of the subtitles can be tested.

While it may seem redundant to have a test for this code as this is not code which changes often, the deprecation notice fixed in 3876 / 84 would have been caught by this test, while now it was only by the luck of the draw of me running `phpcs -e` while on PHP 8.3 (early) that we found that deprecation.
@jrfnl jrfnl added this to the 3.8.0 milestone Dec 4, 2023
@jrfnl jrfnl merged commit 77cee16 into master Dec 4, 2023
65 checks passed
@jrfnl jrfnl deleted the feature/ruleset-explain-refactor-and-add-tests branch December 4, 2023 12:36
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.

1 participant