Skip to content

Commit

Permalink
Common: prepareForOutput(): Use Unicode output on Windows as well on …
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
MatmaRex committed Nov 17, 2024
1 parent 41bc621 commit 8e976bc
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 15 deletions.
3 changes: 2 additions & 1 deletion src/Reports/Code.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,8 @@ public function generateFileReport($report, File $phpcsFile, $showSources=false,
if (strpos($tokenContent, "\t") !== false) {
$token = $tokens[$i];
$token['content'] = $tokenContent;
if (stripos(PHP_OS, 'WIN') === 0) {
if (stripos(PHP_OS, 'WIN') === 0 && PHP_VERSION_ID < 70100) {
// Printing Unicode characters like '»' to Windows console only works since PHP 7.1.
$tab = "\000";
} else {
$tab = "\033[30;1m»\033[0m";
Expand Down
5 changes: 3 additions & 2 deletions src/Util/Common.php
Original file line number Diff line number Diff line change
Expand Up @@ -283,8 +283,9 @@ public static function prepareForOutput($content, $exclude=[])
"\t" => '\t',
" " => '·',
];
if (stripos(PHP_OS, 'WIN') === 0) {
// Do not replace spaces on Windows.
if (stripos(PHP_OS, 'WIN') === 0 && PHP_VERSION_ID < 70100) {
// Do not replace spaces on old PHP on Windows.
// Printing Unicode characters like '·' to Windows console only works since PHP 7.1.
unset($replacements[" "]);
}

Expand Down
53 changes: 41 additions & 12 deletions tests/Core/Util/Common/PrepareForOutputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,45 +27,74 @@ final class PrepareForOutputTest extends TestCase
* @param string $content The content to prepare.
* @param string[] $exclude A list of characters to leave invisible.
* @param string $expected Expected function output.
* @param string $expectedWin Expected function output on Windows (unused in this test).
* @param string $expectedOld Expected function output on PHP<7.1 on Windows (unused in this test).
*
* @requires OS ^(?!WIN).*
* @dataProvider dataPrepareForOutput
*
* @return void
*/
public function testPrepareForOutput($content, $exclude, $expected, $expectedWin)
public function testPrepareForOutput($content, $exclude, $expected, $expectedOld)
{
$this->assertSame($expected, Common::prepareForOutput($content, $exclude));

}//end testPrepareForOutput()


/**
* Test formatting whitespace characters, on Windows.
* Test formatting whitespace characters, on modern PHP on Windows.
*
* @param string $content The content to prepare.
* @param string[] $exclude A list of characters to leave invisible.
* @param string $expected Expected function output (unused in this test).
* @param string $expectedWin Expected function output on Windows.
* @param string $expected Expected function output.
* @param string $expectedOld Expected function output on PHP<7.1 on Windows (unused in this test).
*
* @requires OS ^WIN.*.
* @requires PHP 7.1
* @dataProvider dataPrepareForOutput
*
* @return void
*/
public function testPrepareForOutputWindows($content, $exclude, $expected, $expectedWin)
public function testPrepareForOutputWindows($content, $exclude, $expected, $expectedOld)
{
$this->assertSame($expectedWin, Common::prepareForOutput($content, $exclude));
$this->assertSame($expected, Common::prepareForOutput($content, $exclude));

}//end testPrepareForOutputWindows()


/**
* Test formatting whitespace characters, on PHP<7.1 on Windows.
*
* @param string $content The content to prepare.
* @param string[] $exclude A list of characters to leave invisible.
* @param string $expected Expected function output (unused in this test).
* @param string $expectedOld Expected function output on PHP<7.1 on Windows.
*
* @requires OS ^WIN.*.
* @requires PHP < 7.1
* @dataProvider dataPrepareForOutput
*
* @return void
*/
public function testPrepareForOutputOldPHPWindows($content, $exclude, $expected, $expectedOld)
{
// PHPUnit 4.8 (used on PHP 5.4) does not support the `@requires PHP < 7.1` syntax,
// so double-check to avoid test failures.
if (PHP_VERSION_ID >= 70100) {
$this->markTestSkipped("Only for PHP < 7.1");
}

$this->assertSame($expectedOld, Common::prepareForOutput($content, $exclude));

}//end testPrepareForOutputOldPHPWindows()


/**
* Data provider.
*
* @see testPrepareForOutput()
* @see testPrepareForOutputWindows()
* @see testPrepareForOutputOldPHPWindows()
*
* @return array<string, array<string, mixed>>
*/
Expand All @@ -76,25 +105,25 @@ public static function dataPrepareForOutput()
'content' => "\r\n\t",
'exclude' => [],
'expected' => "\033[30;1m\\r\\n\\t\033[0m",
'expectedWin' => "\033[30;1m\\r\\n\\t\033[0m",
'expectedOld' => "\033[30;1m\\r\\n\\t\033[0m",
],
'Spaces are replaced with a unique mark' => [
'content' => " ",
'exclude' => [],
'expected' => "\033[30;1m····\033[0m",
'expectedWin' => " ",
'expectedOld' => " ",
],
'Other characters are unaffected' => [
'content' => "{echo 1;}",
'exclude' => [],
'expected' => "{echo\033[30;1m·\033[0m1;}",
'expectedWin' => "{echo 1;}",
'expectedOld' => "{echo 1;}",
],
'No replacements' => [
'content' => 'nothing-should-be-replaced',
'exclude' => [],
'expected' => 'nothing-should-be-replaced',
'expectedWin' => 'nothing-should-be-replaced',
'expectedOld' => 'nothing-should-be-replaced',
],
'Excluded whitespace characters are unaffected' => [
'content' => "\r\n\t ",
Expand All @@ -103,7 +132,7 @@ public static function dataPrepareForOutput()
"\n",
],
'expected' => "\r\n\033[30;1m\\\033[0m",
'expectedWin' => "\r\n\033[30;1m\\t\033[0m ",
'expectedOld' => "\r\n\033[30;1m\\t\033[0m ",
],
];

Expand Down

0 comments on commit 8e976bc

Please sign in to comment.