Skip to content

Commit 41bc621

Browse files
committed
Common: prepareForOutput(): Use ANSI color codes on Windows as well
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.
1 parent 880a25b commit 41bc621

File tree

3 files changed

+7
-18
lines changed

3 files changed

+7
-18
lines changed

src/Tokenizers/PHP.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -516,10 +516,6 @@ protected function tokenize($string)
516516
{
517517
if (PHP_CODESNIFFER_VERBOSITY > 1) {
518518
echo "\t*** START PHP TOKENIZING ***".PHP_EOL;
519-
$isWin = false;
520-
if (stripos(PHP_OS, 'WIN') === 0) {
521-
$isWin = true;
522-
}
523519
}
524520

525521
$tokens = @token_get_all($string);
@@ -584,11 +580,7 @@ protected function tokenize($string)
584580
) {
585581
$token[1] .= "\n";
586582
if (PHP_CODESNIFFER_VERBOSITY > 1) {
587-
if ($isWin === true) {
588-
echo '\n';
589-
} else {
590-
echo "\033[30;1m\\n\033[0m";
591-
}
583+
echo "\033[30;1m\\n\033[0m";
592584
}
593585

594586
if ($tokens[($stackPtr + 1)][1] === "\n") {

src/Util/Common.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -267,8 +267,7 @@ public static function escapeshellcmd($cmd)
267267
/**
268268
* Prepares token content for output to screen.
269269
*
270-
* Replaces invisible characters so they are visible. On non-Windows
271-
* operating systems it will also colour the invisible characters.
270+
* Replaces invisible characters so they are visible, and colour them.
272271
*
273272
* @param string $content The content to prepare.
274273
* @param string[] $exclude A list of characters to leave invisible.
@@ -291,11 +290,9 @@ public static function prepareForOutput($content, $exclude=[])
291290

292291
$replacements = array_diff_key($replacements, array_fill_keys($exclude, true));
293292

294-
if (stripos(PHP_OS, 'WIN') !== 0) {
295-
// On non-Windows, colour runs of invisible characters.
296-
$match = implode('', array_keys($replacements));
297-
$content = preg_replace("/([$match]+)/", "\033[30;1m$1\033[0m", $content);
298-
}
293+
// Colour runs of invisible characters.
294+
$match = implode('', array_keys($replacements));
295+
$content = preg_replace("/([$match]+)/", "\033[30;1m$1\033[0m", $content);
299296

300297
$content = strtr($content, $replacements);
301298

tests/Core/Util/Common/PrepareForOutputTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public static function dataPrepareForOutput()
7676
'content' => "\r\n\t",
7777
'exclude' => [],
7878
'expected' => "\033[30;1m\\r\\n\\t\033[0m",
79-
'expectedWin' => "\\r\\n\\t",
79+
'expectedWin' => "\033[30;1m\\r\\n\\t\033[0m",
8080
],
8181
'Spaces are replaced with a unique mark' => [
8282
'content' => " ",
@@ -103,7 +103,7 @@ public static function dataPrepareForOutput()
103103
"\n",
104104
],
105105
'expected' => "\r\n\033[30;1m\\\033[0m",
106-
'expectedWin' => "\r\n\\t ",
106+
'expectedWin' => "\r\n\033[30;1m\\t\033[0m ",
107107
],
108108
];
109109

0 commit comments

Comments
 (0)