Skip to content

Commit 112bdab

Browse files
committed
New Common::stripColors() method
This commit introduces a new static `Common::stripColors()` method to allow for stripping CLI color codes from an arbitrary text. Includes starting to use the function in a few places. Includes unit tests.
1 parent 572a15a commit 112bdab

File tree

4 files changed

+118
-7
lines changed

4 files changed

+118
-7
lines changed

src/Reporter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ public function printReport($report)
236236
ob_end_clean();
237237

238238
if ($this->config->colors !== true || $reportFile !== null) {
239-
$generatedReport = preg_replace('`\033\[[0-9;]+m`', '', $generatedReport);
239+
$generatedReport = Common::stripColors($generatedReport);
240240
}
241241

242242
if ($reportFile !== null) {

src/Ruleset.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -433,11 +433,7 @@ public function showSniffDeprecations()
433433
$sniffCode = substr($sniffCode, 0, ($maxMessageWidth - 3)).'...';
434434
}
435435

436-
$message = '- '.$sniffCode.PHP_EOL;
437-
if ($this->config->colors === true) {
438-
$message = '- '."\033[36m".$sniffCode."\033[0m".PHP_EOL;
439-
}
440-
436+
$message = '- '."\033[36m".$sniffCode."\033[0m".PHP_EOL;
441437
$maxActualWidth = max($maxActualWidth, strlen($sniffCode));
442438

443439
// Normalize new line characters in custom message.
@@ -470,8 +466,13 @@ public function showSniffDeprecations()
470466
echo $summaryLine.PHP_EOL;
471467
}
472468

469+
$messages = implode(PHP_EOL, $messages);
470+
if ($this->config->colors === false) {
471+
$messages = Common::stripColors($messages);
472+
}
473+
473474
echo str_repeat('-', min(($maxActualWidth + 4), $reportWidth)).PHP_EOL;
474-
echo implode(PHP_EOL, $messages);
475+
echo $messages;
475476

476477
$closer = wordwrap('Deprecated sniffs are still run, but will stop working at some point in the future.', $reportWidth, PHP_EOL);
477478
echo PHP_EOL.PHP_EOL.$closer.PHP_EOL.PHP_EOL;

src/Util/Common.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,20 @@ public static function resumeStatusMessages()
396396
}//end resumeStatusMessages()
397397

398398

399+
/**
400+
* Strip colors from a text for output to screen.
401+
*
402+
* @param string $text The text to process.
403+
*
404+
* @return string
405+
*/
406+
public static function stripColors($text)
407+
{
408+
return preg_replace('`\033\[[0-9;]+m`', '', $text);
409+
410+
}//end stripColors()
411+
412+
399413
/**
400414
* Returns true if the specified string is in the camel caps format.
401415
*

tests/Core/Util/StripColorsTest.php

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?php
2+
/**
3+
* Tests for the \PHP_CodeSniffer\Util\Sniffs\Common::stripColors() method.
4+
*
5+
* @author Juliette Reinders Folmer <[email protected]>
6+
* @copyright 2024 Juliette Reinders Folmer. All rights reserved.
7+
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
8+
*/
9+
10+
namespace PHP_CodeSniffer\Tests\Core\Util;
11+
12+
use PHP_CodeSniffer\Util\Common;
13+
use PHPUnit\Framework\TestCase;
14+
15+
/**
16+
* Tests for the \PHP_CodeSniffer\Util\Sniffs\Common::stripColors() method.
17+
*
18+
* @covers \PHP_CodeSniffer\Util\Common::stripColors
19+
*/
20+
final class StripColorsTest extends TestCase
21+
{
22+
23+
24+
/**
25+
* Test stripping color codes from a text.
26+
*
27+
* @param string $text The text provided.
28+
* @param string $expected Expected function output.
29+
*
30+
* @dataProvider dataStripColors
31+
*
32+
* @return void
33+
*/
34+
public function testStripColors($text, $expected)
35+
{
36+
$this->assertSame($expected, Common::stripColors($text));
37+
38+
}//end testStripColors()
39+
40+
41+
/**
42+
* Data provider.
43+
*
44+
* @see testStripColors()
45+
*
46+
* @return array<string, array<string, string>>
47+
*/
48+
public static function dataStripColors()
49+
{
50+
return [
51+
'Text is empty' => [
52+
'text' => '',
53+
'expected' => '',
54+
],
55+
'Text enclosed in color code' => [
56+
'text' => "\033[36mSome text\033[0m",
57+
'expected' => 'Some text',
58+
],
59+
'Text containing color code' => [
60+
'text' => "Some text \033[33mSome other text",
61+
'expected' => 'Some text Some other text',
62+
],
63+
'Text enclosed in color code, bold' => [
64+
'text' => "\033[1;32mSome text\033[0m",
65+
'expected' => 'Some text',
66+
],
67+
'Text enclosed in color code, with escaped text' => [
68+
'text' => "\033[30;1m\\n\033[0m",
69+
'expected' => '\n',
70+
],
71+
'Text enclosed in color code, bold, dark, italic' => [
72+
'text' => "\033[1;2;3mtext\033[0m",
73+
'expected' => 'text',
74+
],
75+
'Text enclosed in color code, foreground color' => [
76+
'text' => "\033[38;5;255mtext\033[0m",
77+
'expected' => 'text',
78+
],
79+
'Text enclosed in color code, foreground color and background color' => [
80+
'text' => "\033[38;5;200;48;5;255mtext\033[0m",
81+
'expected' => 'text',
82+
],
83+
'Multiline text containing multiple color codes' => [
84+
'text' => "First \033[36mSecond\033[0m
85+
Third \033[1;2;3mFourth
86+
Next line\033[0m Last",
87+
'expected' => 'First Second
88+
Third Fourth
89+
Next line Last',
90+
],
91+
];
92+
93+
}//end dataStripColors()
94+
95+
96+
}//end class

0 commit comments

Comments
 (0)