From 8616a09193aa7ffa24c78849a6f61b380bbf7b46 Mon Sep 17 00:00:00 2001 From: Felix Jahn Date: Thu, 16 May 2024 19:15:50 +0200 Subject: [PATCH] fixed throwing errors in exists-check functions (#54) Suppresses warning triggered by `stat`/`lstat` when file does not exit. By injecting another error handler which resets error reporting the suppressed warning gets caught and does not appear in error logs. --- src/NativeWrapper.php | 11 +++- tests/BypassFinals/issue52.phpt | 101 ++++++++++++++++++++++++++++++++ 2 files changed, 109 insertions(+), 3 deletions(-) create mode 100644 tests/BypassFinals/issue52.phpt diff --git a/src/NativeWrapper.php b/src/NativeWrapper.php index 6c7fe70..6347642 100644 --- a/src/NativeWrapper.php +++ b/src/NativeWrapper.php @@ -198,14 +198,19 @@ public function unlink(string $path): bool public function url_stat(string $path, int $flags) { + if ($flags & STREAM_URL_STAT_QUIET) { + set_error_handler(static fn (): bool => true); + } try { $func = $flags & STREAM_URL_STAT_LINK ? 'lstat' : 'stat'; - return $flags & STREAM_URL_STAT_QUIET - ? @$this->native($func, $path) - : $this->native($func, $path); + return $this->native($func, $path); } catch (\RuntimeException $e) { // SplFileInfo::isFile throws exception return false; + } finally { + if ($flags & STREAM_URL_STAT_QUIET) { + restore_error_handler(); + } } } diff --git a/tests/BypassFinals/issue52.phpt b/tests/BypassFinals/issue52.phpt new file mode 100644 index 0000000..1fa1d58 --- /dev/null +++ b/tests/BypassFinals/issue52.phpt @@ -0,0 +1,101 @@ +