From 3d2ccfc4250e2bd2c8681ee2d3d79963e6c7dada Mon Sep 17 00:00:00 2001 From: Sander Muller Date: Tue, 18 Jun 2024 10:03:49 +0200 Subject: [PATCH] fix set_error_handler --- .gitignore | 1 + src/Map.php | 9 +++++++-- src/RemoteLRS.php | 12 ++++++++---- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index e4dc905..2abfc26 100644 --- a/.gitignore +++ b/.gitignore @@ -16,3 +16,4 @@ doc/coverage # wild todo.md +.idea diff --git a/src/Map.php b/src/Map.php index 09eff6e..d289a31 100644 --- a/src/Map.php +++ b/src/Map.php @@ -41,18 +41,23 @@ public function asVersion($version = null) { public function set($code, $value) { $this->_map[$code] = $value; } - private function _unset($code) { + + private function unset($code) { unset($this->_map[$code]); } public function isEmpty() { + if (! is_countable($this->_map)) { + return true; + } + return count($this->_map) === 0; } public function __call($func, $args) { switch ($func) { case 'unset': - return $this->_unset($args[0]); + return $this->unset($args[0]); break; default: throw new \BadMethodCallException(get_class($this) . "::$func() does not exist"); diff --git a/src/RemoteLRS.php b/src/RemoteLRS.php index 5a354c3..01b289a 100644 --- a/src/RemoteLRS.php +++ b/src/RemoteLRS.php @@ -130,14 +130,12 @@ protected function sendRequest($method, $resource) { $success = false; - // // errors from fopen are reported to PHP as E_WARNING which prevents us // from getting a reasonable message, so set an error handler here for // the immediate call to turn it into an exception, and then restore // normal handling - // set_error_handler( - function ($errno, $errstr, $errfile, $errline, array $errcontext) { + function ($errno, $errstr, $errfile, $errline) { // "!== false" is intentional. strpos() can return 0, which is falsey, but returning // 0 matches our "true" condition. Using strict equality to avoid that confusion. if ($errno == E_NOTICE && strpos($errstr, 'Array to string conversion') !== false) { @@ -162,7 +160,13 @@ function ($errno, $errstr, $errfile, $errline, array $errcontext) { $fp = fopen($url, 'rb', false, $context); if (! $fp) { - $content = "Request failed: $php_errormsg"; + $content = "Request failed"; + + $last_error = error_get_last(); + + if($last_error && $last_error['type'] === E_ERROR) { + $content .= ": " . $last_error['message']; + } } } catch (\ErrorException $ex) {