From 382069574e9cfada9244ddd2a106d6c7ca35a9aa Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sun, 3 Nov 2024 16:36:10 +0100 Subject: [PATCH] Generators/Markdown: reset error_reporting to original value 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. --- src/Generators/Markdown.php | 3 ++- tests/Core/Generators/HTMLTest.php | 28 ++++++++++++++++++++++++++ tests/Core/Generators/MarkdownTest.php | 28 ++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 1 deletion(-) diff --git a/src/Generators/Markdown.php b/src/Generators/Markdown.php index de207ec601..97e8717c1f 100644 --- a/src/Generators/Markdown.php +++ b/src/Generators/Markdown.php @@ -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() diff --git a/tests/Core/Generators/HTMLTest.php b/tests/Core/Generators/HTMLTest.php index 4140937ab7..211d8c446e 100644 --- a/tests/Core/Generators/HTMLTest.php +++ b/tests/Core/Generators/HTMLTest.php @@ -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 diff --git a/tests/Core/Generators/MarkdownTest.php b/tests/Core/Generators/MarkdownTest.php index 9a3e540044..394b0ce5c1 100644 --- a/tests/Core/Generators/MarkdownTest.php +++ b/tests/Core/Generators/MarkdownTest.php @@ -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