Skip to content

Commit

Permalink
Merge pull request #118 from PHPCSStandards/feature/ruleset-explain-r…
Browse files Browse the repository at this point in the history
…efactor-and-add-tests

Ruleset::explain(): refactor method to remove use of output buffers + add test
  • Loading branch information
jrfnl authored Dec 4, 2023
2 parents 4d9aef0 + 4dc78b2 commit 77cee16
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 23 deletions.
43 changes: 20 additions & 23 deletions src/Ruleset.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -257,7 +253,9 @@ public function explain()

echo $summaryLine;

ob_start();
$lastStandard = null;
$lastCount = 0;
$sniffsInStandard = [];

foreach ($sniffs as $i => $sniff) {
if ($i === $sniffCount) {
Expand All @@ -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()
Expand Down
60 changes: 60 additions & 0 deletions tests/Core/Ruleset/ExplainTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
/**
* Tests to verify that the "explain" command functions as expected.
*
* @author Juliette Reinders Folmer <phpcs_nospam@adviesenzo.nl>
* @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

0 comments on commit 77cee16

Please sign in to comment.