From 33f0169865c7cfbd67bf59e724776b10fb048d8a Mon Sep 17 00:00:00 2001 From: Kim Pepper Date: Mon, 16 Dec 2024 12:29:24 +1100 Subject: [PATCH] Fix phpstan errors Signed-off-by: Kim Pepper --- src/OpenSearch/EndpointInterface.php | 8 ++----- src/OpenSearch/Endpoints/AbstractEndpoint.php | 4 ++-- .../Endpoints/AsyncSearch/Submit.php | 2 +- .../PreviewTransform.php | 2 +- .../PutTransform.php | 2 +- .../UpdateTransform.php | 4 ++-- src/OpenSearch/Endpoints/Monitoring/Bulk.php | 2 +- .../Endpoints/SearchableSnapshots/Mount.php | 6 ++--- tests/Endpoints/AbstractEndpointTest.php | 10 ++++---- util/template/set-bulk-body | 24 ++++++++++--------- util/template/set-part | 2 +- 11 files changed, 31 insertions(+), 35 deletions(-) diff --git a/src/OpenSearch/EndpointInterface.php b/src/OpenSearch/EndpointInterface.php index 57018f16..6beeb406 100644 --- a/src/OpenSearch/EndpointInterface.php +++ b/src/OpenSearch/EndpointInterface.php @@ -66,16 +66,12 @@ public function setId(int|string|null $docID): static; /** * Get the body of the request. - * - * @return array|string|null */ - public function getBody(): array|string|null; + public function getBody(): string|iterable|null; /** * Set the body of the request. - * - * @param array $body */ - public function setBody(array $body): static; + public function setBody(string|iterable|null $body): static; } diff --git a/src/OpenSearch/Endpoints/AbstractEndpoint.php b/src/OpenSearch/Endpoints/AbstractEndpoint.php index 2f7dcb00..49b3411b 100644 --- a/src/OpenSearch/Endpoints/AbstractEndpoint.php +++ b/src/OpenSearch/Endpoints/AbstractEndpoint.php @@ -150,12 +150,12 @@ public function setId(int|string|null $docID): static return $this; } - public function getBody(): array|string|null + public function getBody(): string|iterable|null { return $this->body; } - public function setBody(array|string $body): static + public function setBody(string|iterable|null $body): static { $this->body = $body; diff --git a/src/OpenSearch/Endpoints/AsyncSearch/Submit.php b/src/OpenSearch/Endpoints/AsyncSearch/Submit.php index d10e801b..21edf79a 100644 --- a/src/OpenSearch/Endpoints/AsyncSearch/Submit.php +++ b/src/OpenSearch/Endpoints/AsyncSearch/Submit.php @@ -87,7 +87,7 @@ public function getMethod(): string return 'POST'; } - public function setBody($body): Submit + public function setBody($body): static { if (isset($body) !== true) { return $this; diff --git a/src/OpenSearch/Endpoints/DataFrameTransformDeprecated/PreviewTransform.php b/src/OpenSearch/Endpoints/DataFrameTransformDeprecated/PreviewTransform.php index e2db220a..3f949ea1 100644 --- a/src/OpenSearch/Endpoints/DataFrameTransformDeprecated/PreviewTransform.php +++ b/src/OpenSearch/Endpoints/DataFrameTransformDeprecated/PreviewTransform.php @@ -40,7 +40,7 @@ public function getMethod(): string return 'POST'; } - public function setBody($body): PreviewTransform + public function setBody($body): static { if (isset($body) !== true) { return $this; diff --git a/src/OpenSearch/Endpoints/DataFrameTransformDeprecated/PutTransform.php b/src/OpenSearch/Endpoints/DataFrameTransformDeprecated/PutTransform.php index ca259759..675dcced 100644 --- a/src/OpenSearch/Endpoints/DataFrameTransformDeprecated/PutTransform.php +++ b/src/OpenSearch/Endpoints/DataFrameTransformDeprecated/PutTransform.php @@ -50,7 +50,7 @@ public function getMethod(): string return 'PUT'; } - public function setBody($body): PutTransform + public function setBody($body): static { if (isset($body) !== true) { return $this; diff --git a/src/OpenSearch/Endpoints/DataFrameTransformDeprecated/UpdateTransform.php b/src/OpenSearch/Endpoints/DataFrameTransformDeprecated/UpdateTransform.php index 30b3dd2d..b5d6f13f 100644 --- a/src/OpenSearch/Endpoints/DataFrameTransformDeprecated/UpdateTransform.php +++ b/src/OpenSearch/Endpoints/DataFrameTransformDeprecated/UpdateTransform.php @@ -50,7 +50,7 @@ public function getMethod(): string return 'POST'; } - public function setBody($body): UpdateTransform + public function setBody($body): static { if (isset($body) !== true) { return $this; @@ -60,7 +60,7 @@ public function setBody($body): UpdateTransform return $this; } - public function setTransformId($transform_id): UpdateTransform + public function setTransformId($transform_id): static { if (isset($transform_id) !== true) { return $this; diff --git a/src/OpenSearch/Endpoints/Monitoring/Bulk.php b/src/OpenSearch/Endpoints/Monitoring/Bulk.php index a40ffdab..797a3f6b 100644 --- a/src/OpenSearch/Endpoints/Monitoring/Bulk.php +++ b/src/OpenSearch/Endpoints/Monitoring/Bulk.php @@ -52,7 +52,7 @@ public function getMethod(): string return 'POST'; } - public function setBody($body): Bulk + public function setBody($body): static { if (isset($body) !== true) { return $this; diff --git a/src/OpenSearch/Endpoints/SearchableSnapshots/Mount.php b/src/OpenSearch/Endpoints/SearchableSnapshots/Mount.php index 3f51d266..af223c10 100644 --- a/src/OpenSearch/Endpoints/SearchableSnapshots/Mount.php +++ b/src/OpenSearch/Endpoints/SearchableSnapshots/Mount.php @@ -54,7 +54,7 @@ public function getMethod(): string return 'POST'; } - public function setBody($body): Mount + public function setBody($body): static { if (isset($body) !== true) { return $this; @@ -64,7 +64,7 @@ public function setBody($body): Mount return $this; } - public function setRepository($repository): Mount + public function setRepository($repository): static { if (isset($repository) !== true) { return $this; @@ -74,7 +74,7 @@ public function setRepository($repository): Mount return $this; } - public function setSnapshot($snapshot): Mount + public function setSnapshot($snapshot): static { if (isset($snapshot) !== true) { return $this; diff --git a/tests/Endpoints/AbstractEndpointTest.php b/tests/Endpoints/AbstractEndpointTest.php index fd5b7c20..a2da5295 100644 --- a/tests/Endpoints/AbstractEndpointTest.php +++ b/tests/Endpoints/AbstractEndpointTest.php @@ -21,19 +21,17 @@ namespace OpenSearch\Tests\Endpoints; +use OpenSearch\EndpointInterface; use OpenSearch\Endpoints\AbstractEndpoint; -use OpenSearch\Endpoints\EndpointInterface; use PHPUnit\Framework\MockObject\MockObject; +use PHPUnit\Framework\TestCase; /** * @covers \OpenSearch\Endpoints\AbstractEndpoint */ -class AbstractEndpointTest extends \PHPUnit\Framework\TestCase +class AbstractEndpointTest extends TestCase { - /** - * @var EndpointInterface&MockObject - */ - private $endpoint; + private EndpointInterface|MockObject $endpoint; protected function setUp(): void { diff --git a/util/template/set-bulk-body b/util/template/set-bulk-body index ece8963f..4ec05057 100644 --- a/util/template/set-bulk-body +++ b/util/template/set-bulk-body @@ -1,20 +1,22 @@ - public function setBody($body): static + public function setBody(string|iterable|null $body): static { - if (isset($body) !== true) { + if (is_null($body)) { return $this; } - if (is_array($body) === true || $body instanceof Traversable) { - foreach ($body as $item) { - $this->body .= $this->serializer->serialize($item) . "\n"; + + if (is_string($body)) { + if (!str_ends_with($body, "\n")) { + $body .= "\n"; } - } elseif (is_string($body)) { $this->body = $body; - if (substr($body, -1) != "\n") { - $this->body .= "\n"; - } - } else { - throw new InvalidArgumentException("Body must be an array, traversable object or string"); + return $this; } + + // Must be an iterable. + foreach ($body as $item) { + $this->body .= $this->serializer->serialize($item) . "\n"; + } + return $this; } diff --git a/util/template/set-part b/util/template/set-part index e84cc82e..47e7d422 100644 --- a/util/template/set-part +++ b/util/template/set-part @@ -1,7 +1,7 @@ public function set:Part($:part): static { - if (isset($:part) !== true) { + if (is_null($:part)) { return $this; } $this->:part = $:part;