Skip to content

Commit 90b8547

Browse files
Extract method
1 parent 3890572 commit 90b8547

File tree

1 file changed

+30
-17
lines changed

1 file changed

+30
-17
lines changed

src/Framework/TestCase.php

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ abstract class TestCase extends Assert implements Reorderable, SelfDescribing, T
219219
* @var list<non-empty-string>
220220
*/
221221
private array $expectedUserDeprecationMessageRegularExpression = [];
222+
private false|string $previousErrorLogTarget = false;
222223

223224
/**
224225
* @param non-empty-string $name
@@ -1298,21 +1299,7 @@ private function runTest(): mixed
12981299
{
12991300
$testArguments = array_merge($this->data, array_values($this->dependencyInput));
13001301

1301-
$capture = tmpfile();
1302-
1303-
if (ini_get('display_errors') === '0') {
1304-
ShutdownHandler::setMessage('Fatal error: Premature end of PHP process. Use display_errors=On to see the error message.');
1305-
}
1306-
1307-
if ($capture !== false) {
1308-
$capturePath = stream_get_meta_data($capture)['uri'];
1309-
1310-
if (@is_writable($capturePath)) {
1311-
$errorLogPrevious = ini_set('error_log', $capturePath);
1312-
} else {
1313-
$capture = false;
1314-
}
1315-
}
1302+
$capture = $this->startErrorLogCapture();
13161303

13171304
try {
13181305
/** @phpstan-ignore method.dynamicName */
@@ -1352,10 +1339,12 @@ private function runTest(): mixed
13521339
} finally {
13531340
if ($capture !== false) {
13541341
ShutdownHandler::resetMessage();
1342+
13551343
fclose($capture);
13561344

1357-
/** @phpstan-ignore variable.undefined (https://github.com/phpstan/phpstan/issues/12992) */
1358-
ini_set('error_log', $errorLogPrevious);
1345+
if ($this->previousErrorLogTarget !== false) {
1346+
ini_set('error_log', $this->previousErrorLogTarget);
1347+
}
13591348
}
13601349
}
13611350

@@ -2347,6 +2336,30 @@ private function handleExceptionFromInvokedCountMockObjectRule(Throwable $t): vo
23472336
}
23482337
}
23492338

2339+
/**
2340+
* @return false|resource
2341+
*/
2342+
private function startErrorLogCapture(): mixed
2343+
{
2344+
if (ini_get('display_errors') === '0') {
2345+
ShutdownHandler::setMessage('Fatal error: Premature end of PHP process. Use display_errors=On to see the error message.');
2346+
}
2347+
2348+
$capture = tmpfile();
2349+
2350+
if ($capture !== false) {
2351+
$capturePath = stream_get_meta_data($capture)['uri'];
2352+
2353+
if (@is_writable($capturePath)) {
2354+
$this->previousErrorLogTarget = ini_set('error_log', $capturePath);
2355+
} else {
2356+
$capture = false;
2357+
}
2358+
}
2359+
2360+
return $capture;
2361+
}
2362+
23502363
/**
23512364
* Creates a test stub for the specified interface or class.
23522365
*

0 commit comments

Comments
 (0)