From 4b912a7d0fc97ca47c976524e55130b6e7da3922 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sat, 26 Apr 2025 16:07:44 +0200 Subject: [PATCH] Standards: introduce `prepareInstalledStandardsForDisplay()` method This commit introduces a new `Standards::prepareInstalledStandardsForDisplay()` method. This new method only prepares the "list" into displayable phrases. It doesn't include output formatting with new lines, that's up to the actual display. This new method allows for getting rid of some output buffering calls in select places. Includes improving the readability of the "coding standard is not installed" error. Includes deprecating the `Standards::printInstalledStandards()` method, which is now no longer used by PHPCS itself. --- src/Config.php | 5 +---- src/Runner.php | 7 ++----- src/Util/Standards.php | 29 +++++++++++++++++++++++------ 3 files changed, 26 insertions(+), 15 deletions(-) diff --git a/src/Config.php b/src/Config.php index 1a3f9c293f..7800463196 100644 --- a/src/Config.php +++ b/src/Config.php @@ -685,10 +685,7 @@ public function processShortArgument($arg, $pos) ob_end_clean(); throw new DeepExitException($output, 0); case 'i' : - ob_start(); - Standards::printInstalledStandards(); - $output = ob_get_contents(); - ob_end_clean(); + $output = Standards::prepareInstalledStandardsForDisplay().PHP_EOL; throw new DeepExitException($output, 0); case 'v' : if ($this->quiet === true) { diff --git a/src/Runner.php b/src/Runner.php index fccd1e112d..351a09afaf 100644 --- a/src/Runner.php +++ b/src/Runner.php @@ -258,11 +258,8 @@ public function init() if (Standards::isInstalledStandard($standard) === false) { // They didn't select a valid coding standard, so help them // out by letting them know which standards are installed. - $error = 'ERROR: the "'.$standard.'" coding standard is not installed. '; - ob_start(); - Standards::printInstalledStandards(); - $error .= ob_get_contents(); - ob_end_clean(); + $error = 'ERROR: the "'.$standard.'" coding standard is not installed.'.PHP_EOL.PHP_EOL; + $error .= Standards::prepareInstalledStandardsForDisplay().PHP_EOL; throw new DeepExitException($error, 3); } } diff --git a/src/Util/Standards.php b/src/Util/Standards.php index ebee5c49b0..d22f233086 100644 --- a/src/Util/Standards.php +++ b/src/Util/Standards.php @@ -312,28 +312,45 @@ public static function getInstalledStandardPath($standard) /** - * Prints out a list of installed coding standards. + * Prepares a list of installed coding standards for display. * - * @return void + * @return string */ - public static function printInstalledStandards() + public static function prepareInstalledStandardsForDisplay() { $installedStandards = self::getInstalledStandards(); $numStandards = count($installedStandards); + $output = ''; if ($numStandards === 0) { - echo 'No coding standards are installed.'.PHP_EOL; + $output .= 'No coding standards are installed.'; } else { $lastStandard = array_pop($installedStandards); if ($numStandards === 1) { - echo "The only coding standard installed is $lastStandard".PHP_EOL; + $output .= "The only coding standard installed is $lastStandard"; } else { $standardList = implode(', ', $installedStandards); $standardList .= ' and '.$lastStandard; - echo 'The installed coding standards are '.$standardList.PHP_EOL; + $output .= 'The installed coding standards are '.$standardList; } } + return $output; + + }//end prepareInstalledStandardsForDisplay() + + + /** + * Prints out a list of installed coding standards. + * + * @deprecated 4.0.0 Use `echo Standards::prepareInstalledStandardsForDisplay()` instead. + * + * @return void + */ + public static function printInstalledStandards() + { + echo self::prepareInstalledStandardsForDisplay(), PHP_EOL; + }//end printInstalledStandards()