Skip to content

Commit 3820695

Browse files
committed
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.
1 parent 56784f1 commit 3820695

File tree

3 files changed

+58
-1
lines changed

3 files changed

+58
-1
lines changed

src/Generators/Markdown.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,10 @@ protected function printFooter()
6969
{
7070
// Turn off errors so we don't get timezone warnings if people
7171
// don't have their timezone set.
72-
error_reporting(0);
72+
$errorLevel = error_reporting(0);
7373
echo 'Documentation generated on '.date('r');
7474
echo ' by [PHP_CodeSniffer '.Config::VERSION.'](https://github.com/PHPCSStandards/PHP_CodeSniffer)'.PHP_EOL;
75+
error_reporting($errorLevel);
7576

7677
}//end printFooter()
7778

tests/Core/Generators/HTMLTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,32 @@ public function testFooter()
101101
}//end testFooter()
102102

103103

104+
/**
105+
* Safeguard that the footer logic doesn't permanently change the error level.
106+
*
107+
* @runInSeparateProcess
108+
* @preserveGlobalState disabled
109+
*
110+
* @return void
111+
*/
112+
public function testFooterResetsErrorReportingToOriginalSetting()
113+
{
114+
$expected = error_reporting();
115+
116+
// Set up the ruleset.
117+
$standard = __DIR__.'/OneDocTest.xml';
118+
$config = new ConfigDouble(["--standard=$standard"]);
119+
$ruleset = new Ruleset($config);
120+
121+
// We know there will be output, but we're not interested in the output for this test.
122+
ob_start();
123+
$generator = new HTMLDouble($ruleset);
124+
$generator->printRealFooter();
125+
ob_end_clean();
126+
127+
$this->assertSame($expected, error_reporting());
128+
129+
}//end testFooterResetsErrorReportingToOriginalSetting()
130+
131+
104132
}//end class

tests/Core/Generators/MarkdownTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,32 @@ public function testFooter()
9999
}//end testFooter()
100100

101101

102+
/**
103+
* Safeguard that the footer logic doesn't permanently change the error level.
104+
*
105+
* @runInSeparateProcess
106+
* @preserveGlobalState disabled
107+
*
108+
* @return void
109+
*/
110+
public function testFooterResetsErrorReportingToOriginalSetting()
111+
{
112+
$expected = error_reporting();
113+
114+
// Set up the ruleset.
115+
$standard = __DIR__.'/OneDocTest.xml';
116+
$config = new ConfigDouble(["--standard=$standard"]);
117+
$ruleset = new Ruleset($config);
118+
119+
// We know there will be output, but we're not interested in the output for this test.
120+
ob_start();
121+
$generator = new MarkdownDouble($ruleset);
122+
$generator->printRealFooter();
123+
ob_end_clean();
124+
125+
$this->assertSame($expected, error_reporting());
126+
127+
}//end testFooterResetsErrorReportingToOriginalSetting()
128+
129+
102130
}//end class

0 commit comments

Comments
 (0)