Skip to content
New issue

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

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

Already on GitHub? # to your account

Report Full: fix two line wrapping bugs #125

Merged
merged 4 commits into from
Dec 11, 2023

Conversation

anomiex
Copy link
Contributor

@anomiex anomiex commented Dec 4, 2023

Description

This is a re-creation of squizlabs/PHP_CodeSniffer#3925:

The first is that the ANSI escape codes applied to bold the message when -s is used were not being taken into account when wrapping the lines for width, causing some lines to be wrapped unnecessarily.

The second is that when lines were wrapped in the middle of a long message, the | characters making up the table were being bolded along with the message.

Suggested changelog entry

  • Avoid unnecessarily wrapping lines in the full report when -s is used.
  • Fix incorrect bolding of pipes in the full report tables when -s is used and messages wrap.

Related issues/external references

Fixes #124
Fixes squizlabs/PHP_CodeSniffer#3924
Closes squizlabs/PHP_CodeSniffer#3925

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
    • This change is only breaking for integrators, not for external standards or end-users.
  • Documentation improvement

PR checklist

  • I have checked there is no other PR open for the same change.
  • I have read the Contribution Guidelines.
  • I grant the project the right to include and distribute the code under the BSD-3-Clause license (and I have the right to grant these rights).
  • I have added tests to cover my changes.
    • I see no existing tests for any of the reporting formats.
  • I have verified that the code complies with the projects coding standards.
  • [Required for new sniffs] I have added XML documentation for the sniff.

@jrfnl
Copy link
Member

jrfnl commented Dec 10, 2023

@anomiex Thanks for this PR. I'm looking at it now. To keep this moving forward, could you please rebase the PR ? I made some changes to the workflows since you created this branch and it would be great to see a passing build using the current workflows.

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.

@anomiex Thanks for reporting this issue and your work on this patch.

I've now looked at and tested this PR extensively, including with the test code from a previous fix/iteration on this code which was merged in squizlabs/PHP_CodeSniffer#2093.

Findings:

  • Your code fixes the issue, but does still contain an "off by one" error somewhere.
    This can be seen when running the test code using the exact max length:
    phpcs -s ./test.php --report-width=110 --standard=PSR12
    phpcs -s ./test.php --report-width=111 --standard=PSR12
    Using the first command the line for the first message will still be wrapped, while when using the second command, it won't but will have a one space difference with the delimiter line.

From #124:

A more complex fix might be to wordwrap() only the message, and then manually calculate the length of the last line to decide whether to append the source directly or to append it as a new line.

As I was looking at this now anyway, I've prepared an update to your fix, which does just that and surprisingly, actually simplifies the code instead of making it more complex.

If you are okay with this, I can push the update in a separate commit to this PR for you to have a look at ?

As a future iteration, I would really like to safeguard these fixes (both this one as well as the ones from squizlabs/PHP_CodeSniffer#2093) with tests, but as testing the reports is greenfields, I consider that "future scope" and not something which needs to be addressed in this PR.

I also discovered another bug while testing this fix. I will pull that as a separate PR, but wouldn't mind a review from you on that PR.

@jrfnl jrfnl changed the title Fix two line wrapping bugs in default report formatter Report Full: fix two line wrapping bugs Dec 10, 2023
@jrfnl jrfnl mentioned this pull request Dec 10, 2023
1 task
@jrfnl
Copy link
Member

jrfnl commented Dec 10, 2023

I also discovered another bug while testing this fix. I will pull that as a separate PR, but wouldn't mind a review from you on that PR.

FYI: I've opened PR #154 for this other bug.

@jrfnl jrfnl mentioned this pull request Dec 10, 2023
3 tasks
@jrfnl jrfnl added this to the 3.x Next milestone Dec 10, 2023
@anomiex
Copy link
Contributor Author

anomiex commented Dec 10, 2023

Using the first command the line for the first message will still be wrapped, while when using the second command, it won't but will have a one space difference with the delimiter line.

That's how the existing code works, it comes from the - 1 at

$maxErrorSpace = ($width - $paddingLength - 1);

Try a file like this, for example:

<?php

$x = TRUE;
$ vendor/bin/phpcs --report-width=83 --standard=PSR12 test.php 

FILE: /tmp/test/test.php
-----------------------------------------------------------------------------------
FOUND 1 ERROR AFFECTING 1 LINE
-----------------------------------------------------------------------------------
 3 | ERROR | [x] TRUE, FALSE and NULL must be lowercase; expected "true" but found
   |       |     "TRUE"
-----------------------------------------------------------------------------------
PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
-----------------------------------------------------------------------------------
$ vendor/bin/phpcs --report-width=82 --standard=PSR12 test.php 

FILE: /tmp/test/test.php
----------------------------------------------------------------------------------
FOUND 1 ERROR AFFECTING 1 LINE
----------------------------------------------------------------------------------
 3 | ERROR | [x] TRUE, FALSE and NULL must be lowercase; expected "true" but
   |       |     found "TRUE"
----------------------------------------------------------------------------------
PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------------------

As I was looking at this now anyway, I've prepared an update to your fix, which does just that and surprisingly, actually simplifies the code instead of making it more complex.

If you are okay with this, I can push the update in a separate commit to this PR for you to have a look at ?

I'll be interested to see your take on it.

The first is that the ANSI escape codes applied to bold the message when
`-s` is used were not being taken into account when wrapping the lines
for width, causing some lines to be wrapped unnecessarily.

The second is that when lines were wrapped in the middle of a long
message, the `|` characters making up the table were being bolded along
with the message.
This commit takes the fix one step further by adding the padding only after the message has been word-wrapped.
Includes correct handling of padding for multi-line error messages.

It then takes the last line of the resulting message and detemines in isolation whether the source code suffix can fit on that line or needs to be placed on a new line.
@jrfnl
Copy link
Member

jrfnl commented Dec 10, 2023

Using the first command the line for the first message will still be wrapped, while when using the second command, it won't but will have a one space difference with the delimiter line.

That's how the existing code works, it comes from the - 1 at

$maxErrorSpace = ($width - $paddingLength - 1);

Good catch! I've updated my change to take that into account now.

As I was looking at this now anyway, I've prepared an update to your fix, which does just that and surprisingly, actually simplifies the code instead of making it more complex.
If you are okay with this, I can push the update in a separate commit to this PR for you to have a look at ?

I'll be interested to see your take on it.

Pushing it now.

Copy link
Contributor Author

@anomiex anomiex left a comment

Choose a reason for hiding this comment

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

Ah, wordwrap() already handles embedded newlines, so the code doesn't have to handle them itself. Good catch!

src/Reports/Full.php Outdated Show resolved Hide resolved
src/Reports/Full.php Outdated Show resolved Hide resolved
@jrfnl
Copy link
Member

jrfnl commented Dec 10, 2023

Ah, wordwrap() already handles embedded newlines, so the code doesn't have to handle them itself. Good catch!

It does indeed, though I have to admit I needed to test that out myself as well (which I did when creating the update): https://3v4l.org/7ENWc

jrfnl and others added 2 commits December 11, 2023 01:14
Prevent inconsistency in handling of single-line vs multi-line length calculation.

Co-authored-by: Brad Jorsch <anomie@users.sourceforge.net>
Don't hard code the color code length to prevent this breaking on potential future changes.

Co-authored-by: Brad Jorsch <anomie@users.sourceforge.net>
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.

As far as I'm concerned this is ready for merge. @anomiex All okay from your perspective too ?

@anomiex
Copy link
Contributor Author

anomiex commented Dec 11, 2023

It does indeed, though I have to admit I needed to test that out myself as well (which I did when creating the update): https://3v4l.org/7ENWc

I did much the same thing! 😀 https://3v4l.org/knYT4

As far as I'm concerned this is ready for merge. @anomiex All okay from your perspective too ?

Looks good to me too!

@jrfnl jrfnl merged commit 18cb0d4 into PHPCSStandards:master Dec 11, 2023
38 checks passed
jrfnl added a commit that referenced this pull request Dec 11, 2023
* Fix two line wrapping bugs in default report formatter

The first is that the ANSI escape codes applied to bold the message when
`-s` is used were not being taken into account when wrapping the lines
for width, causing some lines to be wrapped unnecessarily.

The second is that when lines were wrapped in the middle of a long
message, the `|` characters making up the table were being bolded along
with the message.

* Report Full: iterate on line wrapping bug fix

This commit takes the fix one step further by adding the padding only after the message has been word-wrapped.
Includes correct handling of padding for multi-line error messages.

It then takes the last line of the resulting message and determines in isolation whether the source code suffix can fit on that line or needs to be placed on a new line.


Co-authored-by: Brad Jorsch <anomie@users.sourceforge.net>
Co-authored-by: jrfnl <jrfnl@users.noreply.github.com>
@jrfnl
Copy link
Member

jrfnl commented Dec 11, 2023

Thanks @anomiex !

@jrfnl jrfnl mentioned this pull request Oct 31, 2024
11 tasks
# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unnecessary line wrapping with -s Unnecessary line wrapping with -s
2 participants