Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8e976bc

Browse files
committed
Common: prepareForOutput(): Use Unicode output on Windows as well on 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
1 parent 41bc621 commit 8e976bc

File tree

3 files changed

+46
-15
lines changed

3 files changed

+46
-15
lines changed

‎src/Reports/Code.php‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,8 @@ public function generateFileReport($report, File $phpcsFile, $showSources=false,
228228
if (strpos($tokenContent, "\t") !== false) {
229229
$token = $tokens[$i];
230230
$token['content'] = $tokenContent;
231-
if (stripos(PHP_OS, 'WIN') === 0) {
231+
if (stripos(PHP_OS, 'WIN') === 0 && PHP_VERSION_ID < 70100) {
232+
// Printing Unicode characters like '»' to Windows console only works since PHP 7.1.
232233
$tab = "000円";
233234
} else {
234235
$tab = "033円[30;1m»033円[0m";

‎src/Util/Common.php‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,8 +283,9 @@ public static function prepareForOutput($content, $exclude=[])
283283
"\t" => '\t',
284284
"" => '·',
285285
];
286-
if (stripos(PHP_OS, 'WIN') === 0) {
287-
// Do not replace spaces on Windows.
286+
if (stripos(PHP_OS, 'WIN') === 0 && PHP_VERSION_ID < 70100) {
287+
// Do not replace spaces on old PHP on Windows.
288+
// Printing Unicode characters like '·' to Windows console only works since PHP 7.1.
288289
unset($replacements[""]);
289290
}
290291

‎tests/Core/Util/Common/PrepareForOutputTest.php‎

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,45 +27,74 @@ final class PrepareForOutputTest extends TestCase
2727
* @param string $content The content to prepare.
2828
* @param string[] $exclude A list of characters to leave invisible.
2929
* @param string $expected Expected function output.
30-
* @param string $expectedWin Expected function output on Windows (unused in this test).
30+
* @param string $expectedOld Expected function output on PHP<7.1 on Windows (unused in this test).
3131
*
3232
* @requires OS ^(?!WIN).*
3333
* @dataProvider dataPrepareForOutput
3434
*
3535
* @return void
3636
*/
37-
public function testPrepareForOutput($content, $exclude, $expected, $expectedWin)
37+
public function testPrepareForOutput($content, $exclude, $expected, $expectedOld)
3838
{
3939
$this->assertSame($expected, Common::prepareForOutput($content, $exclude));
4040

4141
}//end testPrepareForOutput()
4242

4343

4444
/**
45-
* Test formatting whitespace characters, on Windows.
45+
* Test formatting whitespace characters, on modern PHP on Windows.
4646
*
4747
* @param string $content The content to prepare.
4848
* @param string[] $exclude A list of characters to leave invisible.
49-
* @param string $expected Expected function output (unused in this test).
50-
* @param string $expectedWin Expected function output on Windows.
49+
* @param string $expected Expected function output.
50+
* @param string $expectedOld Expected function output on PHP<7.1 on Windows (unused in this test).
5151
*
5252
* @requires OS ^WIN.*.
53+
* @requires PHP 7.1
5354
* @dataProvider dataPrepareForOutput
5455
*
5556
* @return void
5657
*/
57-
public function testPrepareForOutputWindows($content, $exclude, $expected, $expectedWin)
58+
public function testPrepareForOutputWindows($content, $exclude, $expected, $expectedOld)
5859
{
59-
$this->assertSame($expectedWin, Common::prepareForOutput($content, $exclude));
60+
$this->assertSame($expected, Common::prepareForOutput($content, $exclude));
6061

6162
}//end testPrepareForOutputWindows()
6263

6364

65+
/**
66+
* Test formatting whitespace characters, on PHP<7.1 on Windows.
67+
*
68+
* @param string $content The content to prepare.
69+
* @param string[] $exclude A list of characters to leave invisible.
70+
* @param string $expected Expected function output (unused in this test).
71+
* @param string $expectedOld Expected function output on PHP<7.1 on Windows.
72+
*
73+
* @requires OS ^WIN.*.
74+
* @requires PHP < 7.1
75+
* @dataProvider dataPrepareForOutput
76+
*
77+
* @return void
78+
*/
79+
public function testPrepareForOutputOldPHPWindows($content, $exclude, $expected, $expectedOld)
80+
{
81+
// PHPUnit 4.8 (used on PHP 5.4) does not support the `@requires PHP < 7.1` syntax,
82+
// so double-check to avoid test failures.
83+
if (PHP_VERSION_ID >= 70100) {
84+
$this->markTestSkipped("Only for PHP < 7.1");
85+
}
86+
87+
$this->assertSame($expectedOld, Common::prepareForOutput($content, $exclude));
88+
89+
}//end testPrepareForOutputOldPHPWindows()
90+
91+
6492
/**
6593
* Data provider.
6694
*
6795
* @see testPrepareForOutput()
6896
* @see testPrepareForOutputWindows()
97+
* @see testPrepareForOutputOldPHPWindows()
6998
*
7099
* @return array<string, array<string, mixed>>
71100
*/
@@ -76,25 +105,25 @@ public static function dataPrepareForOutput()
76105
'content' => "\r\n\t",
77106
'exclude' => [],
78107
'expected' => "033円[30;1m\\r\\n\\t033円[0m",
79-
'expectedWin' => "033円[30;1m\\r\\n\\t033円[0m",
108+
'expectedOld' => "033円[30;1m\\r\\n\\t033円[0m",
80109
],
81110
'Spaces are replaced with a unique mark' => [
82111
'content' => "",
83112
'exclude' => [],
84113
'expected' => "033円[30;1m····033円[0m",
85-
'expectedWin' => "",
114+
'expectedOld' => "",
86115
],
87116
'Other characters are unaffected' => [
88117
'content' => "{echo 1;}",
89118
'exclude' => [],
90119
'expected' => "{echo033円[30;1m·033円[0m1;}",
91-
'expectedWin' => "{echo 1;}",
120+
'expectedOld' => "{echo 1;}",
92121
],
93122
'No replacements' => [
94123
'content' => 'nothing-should-be-replaced',
95124
'exclude' => [],
96125
'expected' => 'nothing-should-be-replaced',
97-
'expectedWin' => 'nothing-should-be-replaced',
126+
'expectedOld' => 'nothing-should-be-replaced',
98127
],
99128
'Excluded whitespace characters are unaffected' => [
100129
'content' => "\r\n\t",
@@ -103,7 +132,7 @@ public static function dataPrepareForOutput()
103132
"\n",
104133
],
105134
'expected' => "\r\n033円[30;1m\\033円[0m",
106-
'expectedWin' => "\r\n033円[30;1m\\t033円[0m ",
135+
'expectedOld' => "\r\n033円[30;1m\\t033円[0m ",
107136
],
108137
];
109138

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /