-
-
Notifications
You must be signed in to change notification settings - Fork 61
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
Use ANSI color codes and Unicode characters on Windows as well #662
base: master
Are you sure you want to change the base?
Conversation
@MatmaRex Thanks for the PR. However, similar to the other PR, I need to ask you to please either provide a reproduction scenario, screenshots or tests to demonstrate what you are trying to solve. Preferably all three of these should be provided with any PR which changes things in the UI. I can take a guess what you are trying to solve, but my guess may be wrong, so without any of the above, I can't even begin to properly evaluate the PR.
PHPCS 3.x still has a minimum supported PHP version of PHP 5.4, so we cannot rely on features from PHP 7.2. I could tentatively earmark this PR for PHPCS 4.0 , which will have a PHP 7.2 minimum, but that still doesn't negate my remarks above. |
While using Windows 10 or newer, and PHP 7.2 or newer, run PHPCS with
Note:
The If you really want to support these old systems, the only thing to do would be to disable |
Oh, right, the "visible" characters are still applied in The encoding issue is interesting – it happens with PHP 7.0, but not with PHP 7.1, which I tested with.
I'm not very familiar with GitHub's CI – can you point out which of the jobs run on Windows? |
Might just be that PHP added support for the Windows ANSI codes in PHP 7.1 and only added the function related to it in PHP 7.2.
At this time, there are no runs on Windows as there are no tests covering code which has special conditions for Windows. As soon as such tests would be added, the Windows OS would have to be added to the test matrix/workflow. (No worries about that, I can do that). |
2ec254b
to
e64ea14
Compare
The ANSI codes and the Unicode characters actually appear unrelated. I found a PHP doc page that documents the latter as a change in PHP 7.1. https://www.php.net/manual/en/migration71.windows-support.php
I guess I can try adding some. Probably in yet another pull request. |
e64ea14
to
294252d
Compare
This reduces the size of the output, and lessens weird effects of the ANSI color code characters on text wrapping, when the result of this method is used in a sniff error message (e.g. see LanguageConstructSpacingSniff).
Windows' console supports ANSI escape sequences since Windows 10. https://learn.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences PHP enables this by default since PHP 7.2. https://www.php.net/manual/en/function.sapi-windows-vt100-support.php On older PHP versions, the output with --colors will not be correct, but this is already the case when using this option. PHP CodeSniffer special-cased Windows in this code in 2014 (bfd095d). This workaround is no longer needed today in 2024. Note that the --colors option was already supported on Windows. This only affects inline highlighting in error and debug messages.
294252d
to
9a33b8c
Compare
I extended the changes here slightly, adding two additional commits (see commit messages for details):
|
9a33b8c
to
ff05ec9
Compare
…PHP 7.1+ PHP 7.1 fixed problems with printing Unicode characters to Windows console that forced us to avoid using them. https://www.php.net/manual/en/migration71.windows-support.php
ff05ec9
to
8e976bc
Compare
Sorry about the noise, I didn't realize at first that PHPUnit 4.8 (used on PHP 5.4) does not support the |
@jrfnl I greatly appreciate your reviews so far. I'd love to hear your thoughts on these patches again whenever you have the time. Please do not feel hurried though, I just want to make it clear that I'm still interested in completing these changes! |
@MatmaRex Sorry for the delay, looking at it again now. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@MatmaRex Okay, so I have looked at this again.
Aside from the PR being blocked by the lack of tests for the Code report (which may be fixed via a future iteration on #664), I'm still concerned about the side-effects of this PR.
I've left a number of comments inline for you to consider.
I'm also a little concerned about the performance impact of the liberal use of array_*
functions in this utility function. On modern PHP versions, it appears to be ~25% slower, while on older PHP versions, it appears to be about ~200% slower.
Before: https://3v4l.org/E3bVW/perf
After: https://3v4l.org/7NQUU/perf
Hope this helps.
src/Tokenizers/PHP.php
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure I understand the reasoning for this change ?
I mean, in contrast to the "Output now has fewer redundant ANSI color codes" changelog suggestion, this ends up creating more redundant ANSI color codes (at least on Windows).
Screenshots generated using the following command on the previously posted code snippet on PHP 7.1 on Windows.
phpcs ./phpcs-662.php --tab-width=4 --standard=generic -vv
Also note (not necessarily to be addressed in this PR, probably should be a separate one): the --no-colors
option can also be used on non-Windows OSes and the previous implementation already didn't respect that.
The PHP
class does have access to the Config
though via the Tokenizer::$config
option, so maybe it should respect the colors
setting ?
"\t" => '\t', | ||
" " => '·', | ||
]; | ||
if (stripos(PHP_OS, 'WIN') === 0 && PHP_VERSION_ID < 70100) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this condition sufficient ?
If I read the https://www.php.net/manual/en/migration71.windows-support.php page, I get the impression an additional check may be needed against the value of the internal_encoding
ini setting - and if that's empty, the default_charset
ini setting - to verify it is set to UTF-8
?
Also note that internal_encoding
and default_charset
were both only introduced in PHP 5.6: https://www.php.net/manual/en/migration56.deprecated.php#migration56.deprecated.iconv-mbstring-encoding
Same remark applies to the Code
report change.
} | ||
// Colour runs of invisible characters. | ||
$match = implode('', array_keys($replacements)); | ||
$content = preg_replace("/([$match]+)/", "\033[30;1m$1\033[0m", $content); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This ends up adding more redundant colour codes on Windows for PHP < 7.1, similar to what is happening in the PHP tokenizer.
Description
Use ANSI color codes and Unicode characters on Windows as well, when possible. See commit messages for details.
Suggested changelog entry
--colors
option, the output now has more colors.Related issues/external references
(none)
Types of changes
PR checklist