Skip to content

Commit

Permalink
Add coverage ignore for code that only runs in PHP 8.0
Browse files Browse the repository at this point in the history
  • Loading branch information
edudobay committed Nov 24, 2020
1 parent 804d0ee commit 4298b11
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions src/Factory/StreamFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,29 +53,35 @@ public function createStreamFromFile(
string $mode = 'r',
StreamInterface $cache = null
): StreamInterface {
// When fopen fails, PHP normally raises a warning. Add an error
// When fopen fails, PHP 7 normally raises a warning. Add an error
// handler to check for errors and throw an exception instead.
// On PHP 8, exceptions are thrown.
$exc = null;

set_error_handler(function (int $errno, string $errstr) use ($filename, $mode, &$exc) {
// Would not be initialized if fopen throws on PHP >= 8.0
$resource = null;

$errorHandler = function (string $errorMessage) use ($filename, $mode, &$exc) {
$exc = new RuntimeException(sprintf(
'Unable to open %s using mode %s: %s',
$filename,
$mode,
$errstr
$errorMessage
));
};

set_error_handler(function (int $errno, string $errstr) use ($errorHandler) {
$errorHandler($errstr);
});

try {
$resource = fopen($filename, $mode);
// @codeCoverageIgnoreStart
// (Can only be executed in PHP >= 8.0)
} catch (\ValueError $exception) {
throw new RuntimeException(sprintf(
'Unable to open %s using mode %s: %s',
$filename,
$mode,
$exception->getMessage()
));
$errorHandler($exception->getMessage());
}
// @codeCoverageIgnoreEnd
restore_error_handler();

if ($exc) {
Expand Down

0 comments on commit 4298b11

Please sign in to comment.