diff --git a/src/Ruleset.php b/src/Ruleset.php index aa80f73643..82facade1e 100644 --- a/src/Ruleset.php +++ b/src/Ruleset.php @@ -240,14 +240,10 @@ public function explain() $sniffs = array_keys($this->sniffCodes); sort($sniffs); - ob_start(); + $sniffCount = count($sniffs); - $lastStandard = null; - $lastCount = 0; - $sniffCount = count($sniffs); - - // Add a dummy entry to the end so we loop - // one last time and clear the output buffer. + // Add a dummy entry to the end so we loop one last time + // and echo out the collected info about the last standard. $sniffs[] = ''; $summaryLine = PHP_EOL."The $this->name standard contains 1 sniff".PHP_EOL; @@ -257,7 +253,9 @@ public function explain() echo $summaryLine; - ob_start(); + $lastStandard = null; + $lastCount = 0; + $sniffsInStandard = []; foreach ($sniffs as $i => $sniff) { if ($i === $sniffCount) { @@ -269,32 +267,31 @@ public function explain() } } + // Reached the first item in the next standard. + // Echo out the info collected from the previous standard. if ($currentStandard !== $lastStandard) { - $sniffList = ob_get_contents(); - ob_end_clean(); - - echo PHP_EOL.$lastStandard.' ('.$lastCount.' sniff'; + $subTitle = $lastStandard.' ('.$lastCount.' sniff'; if ($lastCount > 1) { - echo 's'; + $subTitle .= 's'; } - echo ')'.PHP_EOL; - echo str_repeat('-', (strlen($lastStandard.$lastCount) + 10)); - echo PHP_EOL; - echo $sniffList; + $subTitle .= ')'; - $lastStandard = $currentStandard; - $lastCount = 0; + echo PHP_EOL.$subTitle.PHP_EOL; + echo str_repeat('-', strlen($subTitle)).PHP_EOL; + echo ' '.implode(PHP_EOL.' ', $sniffsInStandard).PHP_EOL; + + $lastStandard = $currentStandard; + $lastCount = 0; + $sniffsInStandard = []; if ($currentStandard === null) { break; } - - ob_start(); }//end if - echo ' '.$sniff.PHP_EOL; - $lastCount++; + $sniffsInStandard[] = $sniff; + ++$lastCount; }//end foreach }//end explain() diff --git a/tests/Core/Ruleset/ExplainTest.php b/tests/Core/Ruleset/ExplainTest.php new file mode 100644 index 0000000000..934801b674 --- /dev/null +++ b/tests/Core/Ruleset/ExplainTest.php @@ -0,0 +1,60 @@ + + * @copyright 2023 Juliette Reinders Folmer. All rights reserved. + * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence + */ + +namespace PHP_CodeSniffer\Tests\Core\Ruleset; + +use PHP_CodeSniffer\Config; +use PHP_CodeSniffer\Ruleset; +use PHPUnit\Framework\TestCase; + +/** + * Test the Ruleset::explain() function. + * + * @covers \PHP_CodeSniffer\Ruleset::explain + */ +class ExplainTest extends TestCase +{ + + + /** + * Test the output of the "explain" command. + * + * @return void + */ + public function testExplain() + { + // Set up the ruleset. + $config = new Config(['--standard=PSR1', '-e']); + $ruleset = new Ruleset($config); + + $expected = PHP_EOL; + $expected .= 'The PSR1 standard contains 8 sniffs'.PHP_EOL.PHP_EOL; + $expected .= 'Generic (4 sniffs)'.PHP_EOL; + $expected .= '------------------'.PHP_EOL; + $expected .= ' Generic.Files.ByteOrderMark'.PHP_EOL; + $expected .= ' Generic.NamingConventions.UpperCaseConstantName'.PHP_EOL; + $expected .= ' Generic.PHP.DisallowAlternativePHPTags'.PHP_EOL; + $expected .= ' Generic.PHP.DisallowShortOpenTag'.PHP_EOL.PHP_EOL; + $expected .= 'PSR1 (3 sniffs)'.PHP_EOL; + $expected .= '---------------'.PHP_EOL; + $expected .= ' PSR1.Classes.ClassDeclaration'.PHP_EOL; + $expected .= ' PSR1.Files.SideEffects'.PHP_EOL; + $expected .= ' PSR1.Methods.CamelCapsMethodName'.PHP_EOL.PHP_EOL; + $expected .= 'Squiz (1 sniff)'.PHP_EOL; + $expected .= '---------------'.PHP_EOL; + $expected .= ' Squiz.Classes.ValidClassName'.PHP_EOL; + + $this->expectOutputString($expected); + + $ruleset->explain(); + + }//end testExplain() + + +}//end class