-
Notifications
You must be signed in to change notification settings - Fork 45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix StreamFactoryTest error #191
Conversation
src/Factory/StreamFactory.php
Outdated
// 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; | ||
|
||
// 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, | ||
$errorMessage | ||
)); | ||
}; | ||
|
||
set_error_handler(function (int $errno, string $errstr) use ($errorHandler) { | ||
$errorHandler($errstr); | ||
}); | ||
set_error_handler( | ||
static function (int $errno, string $errstr) use ($filename, $mode): void { | ||
throw new RuntimeException( | ||
"Unable to open $filename using mode $mode: $errstr", | ||
$errno | ||
); | ||
} | ||
); | ||
|
||
try { | ||
$resource = fopen($filename, $mode); | ||
} catch (ValueError $exception) { | ||
$errorHandler($exception->getMessage()); | ||
} | ||
restore_error_handler(); | ||
|
||
if ($exc) { | ||
/** @var RuntimeException $exc */ | ||
throw $exc; | ||
} finally { | ||
restore_error_handler(); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you explain how removing the catch
fixes this? That makes the try
statement obsolete since it'll never catch anything.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi. Yes, you are right. I forgot to add catch
to throw the exception. Fixed it.
@l0gicgate the current version of slim/psr7 does not pass this test either. Because this test is incorrect. Expected exception "ValueError", but PSR-17 states that "createStreamFromFile" only throw "RuntimeException" or "InvalidArgumentException". Error message:
I can fix this, but it is a PSR-17 violation. |
This discrepancy was fixed in http-interop/http-factory-tests v0. 9. 0 |
Fix phpstan error for php 8.0
Parameter #1 $callback of function set_error_handler expects (callable(int, string, string, int, array): bool)|null, Closure(int, string): void given.