From 91770cc35beb2771bfaa4fdf77b973eda17df25e 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) [Closes #52] 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. As certain testing frameworks establish error handlers to pick up on suppressed errors, exceptions and warnings, the warning generated by `stat`/`lstat` when provided an unknown path is recorded. --- src/NativeWrapper.php | 11 ++- .../BypassFinals/overloaded.exists_check.phpt | 76 +++++++++++++++++++ 2 files changed, 84 insertions(+), 3 deletions(-) create mode 100644 tests/BypassFinals/overloaded.exists_check.phpt diff --git a/src/NativeWrapper.php b/src/NativeWrapper.php index 6c7fe70..9549503 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/overloaded.exists_check.phpt b/tests/BypassFinals/overloaded.exists_check.phpt new file mode 100644 index 0000000..3a2645c --- /dev/null +++ b/tests/BypassFinals/overloaded.exists_check.phpt @@ -0,0 +1,76 @@ +