Skip to content

Commit 998c19c

Browse files
committed
Config: introduce prepareConfigDataForDisplay() method
This commit introduces a new `Config::prepareConfigDataForDisplay()` method. This new method allows for getting rid of some output buffering calls in select places. Includes: * Deprecating the `Config::printConfigData()` method, which is now no longer used by PHPCS itself. * Tests for both methods. * Various minor efficiency fixes to the code.
1 parent 288e4ad commit 998c19c

File tree

2 files changed

+150
-16
lines changed

2 files changed

+150
-16
lines changed

src/Config.php

+33-16
Original file line numberDiff line numberDiff line change
@@ -898,12 +898,9 @@ public function processLongArgument($arg, $pos)
898898
}
899899
throw new DeepExitException($output, 0);
900900
case 'config-show':
901-
ob_start();
902-
$data = self::getAllConfigData();
903-
echo 'Using config file: '.self::$configDataFile.PHP_EOL.PHP_EOL;
904-
$this->printConfigData($data);
905-
$output = ob_get_contents();
906-
ob_end_clean();
901+
$data = self::getAllConfigData();
902+
$output = 'Using config file: '.self::$configDataFile.PHP_EOL.PHP_EOL;
903+
$output .= $this->prepareConfigDataForDisplay($data);
907904
throw new DeepExitException($output, 0);
908905
case 'runtime-set':
909906
if (isset($this->cliArgs[($pos + 1)]) === false
@@ -1761,33 +1758,53 @@ public static function getAllConfigData()
17611758

17621759

17631760
/**
1764-
* Prints out the gathered config data.
1761+
* Prepares the gathered config data for display.
17651762
*
1766-
* @param array $data The config data to print.
1763+
* @param array<string, string> $data The config data to format for display.
17671764
*
1768-
* @return void
1765+
* @return string
17691766
*/
1770-
public function printConfigData($data)
1767+
public function prepareConfigDataForDisplay($data)
17711768
{
1769+
if (empty($data) === true) {
1770+
return '';
1771+
}
1772+
17721773
$max = 0;
17731774
$keys = array_keys($data);
17741775
foreach ($keys as $key) {
17751776
$len = strlen($key);
1776-
if (strlen($key) > $max) {
1777+
if ($len > $max) {
17771778
$max = $len;
17781779
}
17791780
}
17801781

1781-
if ($max === 0) {
1782-
return;
1783-
}
1784-
17851782
$max += 2;
17861783
ksort($data);
1784+
1785+
$output = '';
17871786
foreach ($data as $name => $value) {
1788-
echo str_pad($name.': ', $max).$value.PHP_EOL;
1787+
$output .= str_pad($name.': ', $max).$value.PHP_EOL;
17891788
}
17901789

1790+
return $output;
1791+
1792+
}//end prepareConfigDataForDisplay()
1793+
1794+
1795+
/**
1796+
* Prints out the gathered config data.
1797+
*
1798+
* @param array<string, string> $data The config data to print.
1799+
*
1800+
* @deprecated 4.0.0 Use `echo Config::prepareConfigDataForDisplay()` instead.
1801+
*
1802+
* @return void
1803+
*/
1804+
public function printConfigData($data)
1805+
{
1806+
echo $this->prepareConfigDataForDisplay($data);
1807+
17911808
}//end printConfigData()
17921809

17931810

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
<?php
2+
/**
3+
* Tests for the Config::prepareConfigDataForDisplay() method.
4+
*
5+
* @copyright 2025 PHPCSStandards and contributors
6+
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
7+
*/
8+
9+
namespace PHP_CodeSniffer\Tests\Core\Config;
10+
11+
use PHP_CodeSniffer\Tests\ConfigDouble;
12+
use PHPUnit\Framework\TestCase;
13+
14+
/**
15+
* Tests for the Config::prepareConfigDataForDisplay() method.
16+
*
17+
* @covers \PHP_CodeSniffer\Config::prepareConfigDataForDisplay
18+
* @covers \PHP_CodeSniffer\Config::printConfigData
19+
*/
20+
final class PrepareConfigDataForDisplayTest extends TestCase
21+
{
22+
23+
24+
/**
25+
* Verify that the config data is prepared for display correctly.
26+
*
27+
* - The method always returns a string.
28+
* - Config data is sorted based on the keys.
29+
* - The display formatting takes the length of the keys into account correctly.
30+
*
31+
* @param array<string, string> $data The Config data.
32+
* @param string $expected Expected formatted string.
33+
*
34+
* @dataProvider dataPrepareConfigDataForDisplay
35+
*
36+
* @return void
37+
*/
38+
public function testPrepareConfigDataForDisplay($data, $expected)
39+
{
40+
$config = new ConfigDouble();
41+
$actual = $config->prepareConfigDataForDisplay($data);
42+
43+
$this->assertSame($expected, $actual);
44+
45+
}//end testPrepareConfigDataForDisplay()
46+
47+
48+
/**
49+
* Data provider.
50+
*
51+
* @return array<string, array<string, array<string, string>|string>>
52+
*/
53+
public static function dataPrepareConfigDataForDisplay()
54+
{
55+
// phpcs:disable Squiz.Strings.ConcatenationSpacing.PaddingFound -- Readability is more important.
56+
return [
57+
'No config data' => [
58+
'data' => [],
59+
'expected' => '',
60+
],
61+
'Only one config item' => [
62+
'data' => [
63+
'colors' => '1',
64+
],
65+
'expected' => 'colors: 1'.PHP_EOL,
66+
],
67+
'Data with keys of varying length in non-alphabetically order' => [
68+
'data' => [
69+
'tab_width' => '2',
70+
'encoding' => 'utf-8',
71+
'ignore_errors_on_exit' => '1',
72+
'default_standard' => 'PSR12',
73+
],
74+
'expected' => 'default_standard: PSR12'.PHP_EOL
75+
.'encoding: utf-8'.PHP_EOL
76+
.'ignore_errors_on_exit: 1'.PHP_EOL
77+
.'tab_width: 2'.PHP_EOL,
78+
],
79+
'Invalid config data: no keys' => [
80+
'data' => [
81+
'1',
82+
'2',
83+
],
84+
'expected' => '0: 1'.PHP_EOL.'1: 2'.PHP_EOL,
85+
],
86+
'Unexpected, but handled, data with non-string scalar values' => [
87+
'data' => [
88+
'encoding' => 'utf-8',
89+
'ignore_errors_on_exit' => true,
90+
'tab_width' => 2,
91+
],
92+
'expected' => 'encoding: utf-8'.PHP_EOL
93+
.'ignore_errors_on_exit: 1'.PHP_EOL
94+
.'tab_width: 2'.PHP_EOL,
95+
],
96+
];
97+
// phpcs:enable
98+
99+
}//end dataPrepareConfigDataForDisplay()
100+
101+
102+
/**
103+
* Perfunctory test for the deprecated Config::printConfigData() method.
104+
*
105+
* @return void
106+
*/
107+
public function testPrintConfigData()
108+
{
109+
$this->expectOutputString('');
110+
111+
$config = new ConfigDouble();
112+
$config->printConfigData([]);
113+
114+
}//end testPrintConfigData()
115+
116+
117+
}//end class

0 commit comments

Comments
 (0)