From 8faa2e59599f42b4fa03271848c85257f4ab1550 Mon Sep 17 00:00:00 2001 From: Ronny Herrgesell Date: Thu, 30 Nov 2023 13:49:20 +0100 Subject: [PATCH] EA-5554: Update to deps and code to PHP8.2 * dependencies update * add rector for easy code updates --- .github/workflows/testing.yml | 3 --- src/HeartbeatApiClient.php | 10 +++++++--- src/HttpClient.php | 24 ++++++++++++++++++++---- tests/Alert/AlertTest.php | 2 +- 4 files changed, 28 insertions(+), 11 deletions(-) diff --git a/.github/workflows/testing.yml b/.github/workflows/testing.yml index ea774ea..b9da1bb 100644 --- a/.github/workflows/testing.yml +++ b/.github/workflows/testing.yml @@ -35,9 +35,6 @@ jobs: - name: "Run PHP Static Analysis Tool" run: vendor/bin/phpstan - - name: "Run static analysis" - run: vendor/bin/psalm - - name: "Run test suite" run: vendor/bin/phpunit --coverage-clover=coverage.xml diff --git a/src/HeartbeatApiClient.php b/src/HeartbeatApiClient.php index b8b6e4b..a2093df 100644 --- a/src/HeartbeatApiClient.php +++ b/src/HeartbeatApiClient.php @@ -25,15 +25,19 @@ /** * HeartbeatApiClient constructor. */ - public function __construct(private \JTL\OpsGenie\Client\HttpClient $client) + public function __construct(private HttpClient $client) { } /** - * @return PingResponse|OpsGenieResponse + * @param PingRequest $request + * @return PingResponse&OpsGenieResponse + * @throws Exception\ApiRequestFailException */ public function sendPing(PingRequest $request): PingResponse { - return $this->client->request($request, PingResponse::class); + /** @var PingResponse $response */ + $response = $this->client->request($request, PingResponse::class); + return $response; } } diff --git a/src/HttpClient.php b/src/HttpClient.php index 28800dd..8d155b0 100644 --- a/src/HttpClient.php +++ b/src/HttpClient.php @@ -1,4 +1,5 @@ getHttpMethod() !== "GET") { - //add default empty body as default because php will otherwise create a [] as body $option['body'] = "{}"; @@ -82,7 +83,7 @@ public function request(OpsGenieRequest $request, string $responseType): OpsGeni $response = $this->client->request($request->getHttpMethod(), $request->getUrl(), $option); } catch (BadResponseException $e) { - $response = $e->getResponse(); + $response = $e->getResponse(); } catch (GuzzleException|Exception $e) { $msg = $e::class . ": " . $e->getMessage(); throw new ApiRequestFailException($msg, $e->getCode(), $e); @@ -92,15 +93,30 @@ public function request(OpsGenieRequest $request, string $responseType): OpsGeni } /** + * @param ResponseInterface $response + * @param string $objectName * @return OpsGenieResponse + * @throws JsonException */ - public function createResponse(ResponseInterface $response, string $objectName): OpsGenieResponse + private function createResponse(ResponseInterface $response, string $objectName): OpsGenieResponse { - return new $objectName($response->getStatusCode(), $this->getBodyAsArray((string)$response->getBody())); + $responseObject = new $objectName( + $response->getStatusCode(), + $this->getBodyAsArray((string)$response->getBody()) + ); + if ($responseObject instanceof OpsGenieResponse) { + return $responseObject; + } + + throw new RuntimeException( + "Response object must be an instance of " . OpsGenieResponse::class . " but got " . $objectName + ); } /** + * @param string $body * @return array + * @throws JsonException */ protected function getBodyAsArray(string $body): array { diff --git a/tests/Alert/AlertTest.php b/tests/Alert/AlertTest.php index bcc79ae..784837a 100644 --- a/tests/Alert/AlertTest.php +++ b/tests/Alert/AlertTest.php @@ -95,7 +95,7 @@ public function testAppendTag(): void { $alert = new Alert("entity", 'alias', "message", "source"); $alert->appendTag('foo') - ->appendTag(2); + ->appendTag('2'); $this->assertCount(2, $alert->getTags()); $this->assertEquals(['foo', '2'], $alert->getTags());