From 4484f1c997ac5497d693b43a7a0b8787428d7d4b Mon Sep 17 00:00:00 2001 From: Dave Mc Nicoll Date: Wed, 1 Nov 2023 11:29:08 -0400 Subject: [PATCH] - Added some more type checking --- src/Dump.php | 2 +- src/Handler.php | 2 +- src/Task.php | 10 +++++++--- src/Transport/Curl.php | 8 +++++--- src/Transport/GuzzleClient.php | 2 +- src/Transport/TransportInterface.php | 2 +- 6 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/Dump.php b/src/Dump.php index 752239b..39c5e15 100644 --- a/src/Dump.php +++ b/src/Dump.php @@ -22,7 +22,7 @@ namespace Negundo\Client { static::$instances[] = $this; } - public function pushData(...$content) : ? object + public function pushData(...$content) : object|null|bool { $data = $this->dumpHandler->dumpData(...$content); diff --git a/src/Handler.php b/src/Handler.php index 1a488a1..7a14c12 100644 --- a/src/Handler.php +++ b/src/Handler.php @@ -55,7 +55,7 @@ abstract class Handler { }); } - public function pushData(\Throwable $ex) : ? object + public function pushData(\Throwable $ex) : null|object|bool { // Make sure not to spam the server if an ErrorMessage or Exception was already sent (like inside a loop) $exceptionHash = $this->exceptionHandler->hash($ex); diff --git a/src/Task.php b/src/Task.php index 9f0a7ef..b5b1790 100644 --- a/src/Task.php +++ b/src/Task.php @@ -25,7 +25,7 @@ namespace Negundo\Client { static::$instances[] = $this; } - public function newReport(string $message, ? string $title = null, ? array $data = []) : ? object + public function newReport(string $message, ? string $title = null, ? array $data = []) : object|null|bool { $report = $this->taskHandler->sendReport($message, $title, $data); @@ -45,9 +45,13 @@ namespace Negundo\Client { namespace { if (! function_exists('ntask') ) { - function ntask(string $message, ? string $title = null, ? array $data) { + function ntask(string $message, ? string $title = null, ? array $data = null) { foreach (\Negundo\Client\Task::$instances as $instance) { - $instance->newReport($message, $title, $data); + $sent = $instance->newReport($message, $title, $data); + + if (! $sent ) { + throw new \Exception(sprintf('Could not send report titled `%s`.', $title)); + } } } } diff --git a/src/Transport/Curl.php b/src/Transport/Curl.php index 934d58e..62a7476 100644 --- a/src/Transport/Curl.php +++ b/src/Transport/Curl.php @@ -10,7 +10,7 @@ class Curl implements TransportInterface { public $headers = []; - public function push(string $url, array $data) : ? object + public function push(string $url, array $data) : object|null|bool { $ch = curl_init(); @@ -22,7 +22,9 @@ class Curl implements TransportInterface { curl_setopt($ch, CURLOPT_TIMEOUT_MS, $this->timeout * 200); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); - if ( ( false === curl_exec($ch) ) && $this->throwErrors ) { + $exec = curl_exec($ch); + + if ( ( false === $exec ) && $this->throwErrors ) { $errno = curl_errno($ch); $errors = array_filter([ curl_error($ch) , static::CURL_ERROR[$errno] ?? null ]); @@ -31,7 +33,7 @@ class Curl implements TransportInterface { curl_close($ch); - return null; + return $exec; } const CURL_ERROR = [ diff --git a/src/Transport/GuzzleClient.php b/src/Transport/GuzzleClient.php index e062723..5d843b9 100644 --- a/src/Transport/GuzzleClient.php +++ b/src/Transport/GuzzleClient.php @@ -12,7 +12,7 @@ class GuzzleClient implements TransportInterface { public array $headers = []; - public function push(string $url, array $data) : ? object + public function push(string $url, array $data) : object|null|bool { return ( new Client([ 'timeout' => $this->timeout, diff --git a/src/Transport/TransportInterface.php b/src/Transport/TransportInterface.php index ce704ce..50b0433 100644 --- a/src/Transport/TransportInterface.php +++ b/src/Transport/TransportInterface.php @@ -3,5 +3,5 @@ namespace Negundo\Client\Transport; interface TransportInterface { - public function push(string $url, array $data) : ? object; + public function push(string $url, array $data) : object|null|bool; }