Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ruleset::explain(): refactor method to remove use of output buffers + add test #118

Merged
merged 1 commit into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 <[email protected]>
* @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