Skip to content

Commit 9c8f30f

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 c5f7f69 commit 9c8f30f

File tree

4 files changed

+118
-7
lines changed

4 files changed

+118
-7
lines changed

src/Reporter.php

+1-1
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

+7-6
Original file line numberDiff line numberDiff line change
@@ -414,11 +414,7 @@ public function showSniffDeprecations()
414414
$sniffCode = substr($sniffCode, 0, ($maxMessageWidth - 3)).'...';
415415
}
416416

417-
$message = '- '.$sniffCode.PHP_EOL;
418-
if ($this->config->colors === true) {
419-
$message = '- '."\033[36m".$sniffCode."\033[0m".PHP_EOL;
420-
}
421-
417+
$message = '- '."\033[36m".$sniffCode."\033[0m".PHP_EOL;
422418
$maxActualWidth = max($maxActualWidth, strlen($sniffCode));
423419

424420
// Normalize new line characters in custom message.
@@ -451,8 +447,13 @@ public function showSniffDeprecations()
451447
echo $summaryLine.PHP_EOL;
452448
}
453449

450+
$messages = implode(PHP_EOL, $messages);
451+
if ($this->config->colors === false) {
452+
$messages = Common::stripColors($messages);
453+
}
454+
454455
echo str_repeat('-', min(($maxActualWidth + 4), $reportWidth)).PHP_EOL;
455-
echo implode(PHP_EOL, $messages);
456+
echo $messages;
456457

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

src/Util/Common.php

+14
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,20 @@ public static function prepareForOutput($content, $exclude=[])
312312
}//end prepareForOutput()
313313

314314

315+
/**
316+
* Strip colors from a text for output to screen.
317+
*
318+
* @param string $text The text to process.
319+
*
320+
* @return string
321+
*/
322+
public static function stripColors($text)
323+
{
324+
return preg_replace('`\033\[[0-9;]+m`', '', $text);
325+
326+
}//end stripColors()
327+
328+
315329
/**
316330
* Returns true if the specified string is in the camel caps format.
317331
*

tests/Core/Util/StripColorsTest.php

+96
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)