Skip to content

Commit

Permalink
Generators/Markdown: reset error_reporting to original value
Browse files Browse the repository at this point in the history
As things were, the `Markdown` class changes the PHP error level (to prevent potentially getting a warning about the timezone not being set), but doesn't reset the error level back to the original error level once the "risky" code has been executed.

Fixed now.

This was previously already fixed for the `HTML` class in PR squizlabs/PHP_CodeSniffer 488

Includes adding a test to safeguard this for both classes. These tests need to be run in isolation so as not get interference from the fact that the code is run in a test environment. I.e. without the `@runInSeparateProcess`, the test wouldn't fail when it should.
  • Loading branch information
jrfnl committed Nov 15, 2024
1 parent 56784f1 commit 3820695
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/Generators/Markdown.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,10 @@ protected function printFooter()
{
// Turn off errors so we don't get timezone warnings if people
// don't have their timezone set.
error_reporting(0);
$errorLevel = error_reporting(0);
echo 'Documentation generated on '.date('r');
echo ' by [PHP_CodeSniffer '.Config::VERSION.'](https://github.com/PHPCSStandards/PHP_CodeSniffer)'.PHP_EOL;
error_reporting($errorLevel);

}//end printFooter()

Expand Down
28 changes: 28 additions & 0 deletions tests/Core/Generators/HTMLTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,32 @@ public function testFooter()
}//end testFooter()


/**
* Safeguard that the footer logic doesn't permanently change the error level.
*
* @runInSeparateProcess
* @preserveGlobalState disabled
*
* @return void
*/
public function testFooterResetsErrorReportingToOriginalSetting()
{
$expected = error_reporting();

// Set up the ruleset.
$standard = __DIR__.'/OneDocTest.xml';
$config = new ConfigDouble(["--standard=$standard"]);
$ruleset = new Ruleset($config);

// We know there will be output, but we're not interested in the output for this test.
ob_start();
$generator = new HTMLDouble($ruleset);
$generator->printRealFooter();
ob_end_clean();

$this->assertSame($expected, error_reporting());

}//end testFooterResetsErrorReportingToOriginalSetting()


}//end class
28 changes: 28 additions & 0 deletions tests/Core/Generators/MarkdownTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,32 @@ public function testFooter()
}//end testFooter()


/**
* Safeguard that the footer logic doesn't permanently change the error level.
*
* @runInSeparateProcess
* @preserveGlobalState disabled
*
* @return void
*/
public function testFooterResetsErrorReportingToOriginalSetting()
{
$expected = error_reporting();

// Set up the ruleset.
$standard = __DIR__.'/OneDocTest.xml';
$config = new ConfigDouble(["--standard=$standard"]);
$ruleset = new Ruleset($config);

// We know there will be output, but we're not interested in the output for this test.
ob_start();
$generator = new MarkdownDouble($ruleset);
$generator->printRealFooter();
ob_end_clean();

$this->assertSame($expected, error_reporting());

}//end testFooterResetsErrorReportingToOriginalSetting()


}//end class

0 comments on commit 3820695

Please sign in to comment.