From 368c73f182f21f6e8b346283229f221fdb1f042e Mon Sep 17 00:00:00 2001 From: Andrei Ivchenkov Date: Thu, 12 Dec 2024 01:33:06 +0300 Subject: [PATCH] fixed errors after phpstan update --- src/Http/Client.php | 4 ++-- src/Search/SearchResult.php | 24 ++++++++++++------------ tests/Endpoints/SearchTest.php | 19 ++++++++++++++++++- 3 files changed, 32 insertions(+), 15 deletions(-) diff --git a/src/Http/Client.php b/src/Http/Client.php index afffc154..2d5486e5 100644 --- a/src/Http/Client.php +++ b/src/Http/Client.php @@ -47,9 +47,9 @@ public function __construct( $this->http = $httpClient ?? Psr18ClientDiscovery::find(); $this->requestFactory = $reqFactory ?? Psr17FactoryDiscovery::findRequestFactory(); $this->streamFactory = $streamFactory ?? Psr17FactoryDiscovery::findStreamFactory(); - $this->headers = array_filter([ + $this->headers = [ 'User-Agent' => implode(';', array_merge($clientAgents, [Meilisearch::qualifiedVersion()])), - ]); + ]; if (null !== $apiKey && '' !== $apiKey) { $this->headers['Authorization'] = \sprintf('Bearer %s', $apiKey); } diff --git a/src/Search/SearchResult.php b/src/Search/SearchResult.php index 225197a6..3873a72b 100644 --- a/src/Search/SearchResult.php +++ b/src/Search/SearchResult.php @@ -19,16 +19,16 @@ class SearchResult implements \Countable, \IteratorAggregate * and its value will not be modified by the methods in this class. * Please, use `hitsCount` if you want to know the real size of the `hits` array at any time. */ - private ?int $estimatedTotalHits; - private ?int $hitsCount; - private ?int $offset; - private ?int $limit; - private ?int $semanticHitCount; + private ?int $estimatedTotalHits = null; + private int $hitsCount; + private ?int $offset = null; + private ?int $limit = null; + private int $semanticHitCount; - private ?int $hitsPerPage; - private ?int $page; - private ?int $totalPages; - private ?int $totalHits; + private ?int $hitsPerPage = null; + private ?int $page = null; + private ?int $totalPages = null; + private ?int $totalHits = null; private int $processingTimeMs; private bool $numberedPagination; @@ -126,12 +126,12 @@ public function getHits(): array return $this->hits; } - public function getOffset(): int + public function getOffset(): ?int { return $this->offset; } - public function getLimit(): int + public function getLimit(): ?int { return $this->limit; } @@ -154,7 +154,7 @@ public function count(): int return $this->hitsCount; } - public function getEstimatedTotalHits(): int + public function getEstimatedTotalHits(): ?int { return $this->estimatedTotalHits; } diff --git a/tests/Endpoints/SearchTest.php b/tests/Endpoints/SearchTest.php index eea4046c..0ce5d5af 100644 --- a/tests/Endpoints/SearchTest.php +++ b/tests/Endpoints/SearchTest.php @@ -26,9 +26,17 @@ public function testBasicSearch(): void $response = $this->index->search('prince'); $this->assertEstimatedPagination($response->toArray()); - self::assertSame(2, $response->getEstimatedTotalHits()); self::assertCount(2, $response->getHits()); + self::assertSame(2, $response->getEstimatedTotalHits()); + self::assertSame(0, $response->getOffset()); + self::assertSame(20, $response->getLimit()); + + self::assertNull($response->getHitsPerPage()); + self::assertNull($response->getPage()); + self::assertNull($response->getTotalPages()); + self::assertNull($response->getTotalHits()); + $response = $this->index->search('prince', [], [ 'raw' => true, ]); @@ -44,6 +52,15 @@ public function testBasicSearchWithFinitePagination(): void $this->assertFinitePagination($response->toArray()); self::assertCount(2, $response->getHits()); + self::assertSame(2, $response->getHitsPerPage()); + self::assertSame(1, $response->getPage()); + self::assertSame(1, $response->getTotalPages()); + self::assertSame(2, $response->getTotalHits()); + + self::assertNull($response->getEstimatedTotalHits()); + self::assertNull($response->getOffset()); + self::assertNull($response->getLimit()); + $response = $this->index->search('prince', ['hitsPerPage' => 2], [ 'raw' => true, ]);