Skip to content

Commit f3837f0

Browse files
committed
Add missing types
1 parent 3c3cecb commit f3837f0

21 files changed

+104
-150
lines changed

src/Contracts/Data.php

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
class Data implements \ArrayAccess, \Countable, \IteratorAggregate
88
{
9-
protected $data = [];
9+
protected array $data = [];
1010

1111
public function __construct(array $data = [])
1212
{
@@ -28,14 +28,9 @@ public function offsetUnset($offset): void
2828
unset($this->data[$offset]);
2929
}
3030

31-
#[\ReturnTypeWillChange]
32-
public function offsetGet($offset)
31+
public function offsetGet($offset): mixed
3332
{
34-
if (isset($this->data[$offset])) {
35-
return $this->data[$offset];
36-
}
37-
38-
return null;
33+
return $this->data[$offset] ?? null;
3934
}
4035

4136
public function count(): int

src/Contracts/Http.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ interface Http
88
{
99
public function get(string $path, array $query = []);
1010

11-
public function post(string $path, $body = null, array $query = [], ?string $contentType = null);
11+
public function post(string $path, mixed $body = null, array $query = [], ?string $contentType = null);
1212

13-
public function put(string $path, $body = null, array $query = [], ?string $contentType = null);
13+
public function put(string $path, mixed $body = null, array $query = [], ?string $contentType = null);
1414

15-
public function patch(string $path, $body = null, array $query = []);
15+
public function patch(string $path, mixed $body = null, array $query = []);
1616

1717
public function delete(string $path, array $query = []);
1818
}

src/Contracts/SimilarDocumentsQuery.php

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
class SimilarDocumentsQuery
88
{
99
/**
10-
* @var int|string
10+
* @var int|non-empty-string
1111
*/
12-
private $id;
12+
private int|string $id;
1313

1414
/**
1515
* @var non-empty-string
@@ -42,16 +42,13 @@ class SimilarDocumentsQuery
4242
*/
4343
private ?array $filter = null;
4444

45-
/**
46-
* @var int|float|null
47-
*/
48-
private $rankingScoreThreshold;
45+
private int|float|null $rankingScoreThreshold = null;
4946

5047
/**
51-
* @param int|string $id
52-
* @param non-empty-string $embedder
48+
* @param int|non-empty-string $id
49+
* @param non-empty-string $embedder
5350
*/
54-
public function __construct($id, string $embedder)
51+
public function __construct(int|string $id, string $embedder)
5552
{
5653
$this->id = $id;
5754
$this->embedder = $embedder;
@@ -142,11 +139,9 @@ public function setRetrieveVectors(?bool $retrieveVectors): self
142139
}
143140

144141
/**
145-
* @param int|float|null $rankingScoreThreshold
146-
*
147142
* @return $this
148143
*/
149-
public function setRankingScoreThreshold($rankingScoreThreshold): self
144+
public function setRankingScoreThreshold(int|float|null $rankingScoreThreshold): self
150145
{
151146
$this->rankingScoreThreshold = $rankingScoreThreshold;
152147

@@ -155,7 +150,7 @@ public function setRankingScoreThreshold($rankingScoreThreshold): self
155150

156151
/**
157152
* @return array{
158-
* id: int|string,
153+
* id: int|non-empty-string,
159154
* embedder: non-empty-string,
160155
* offset?: non-negative-int,
161156
* limit?: positive-int,

src/Endpoints/Batches.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class Batches extends Endpoint
1010
{
1111
protected const PATH = '/batches';
1212

13-
public function get($batchUid): array
13+
public function get(int $batchUid): array
1414
{
1515
return $this->http->get(self::PATH.'/'.$batchUid);
1616
}

src/Endpoints/Delegates/HandlesBatches.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ trait HandlesBatches
1212
{
1313
protected Batches $batches;
1414

15-
public function getBatch($uid): array
15+
public function getBatch(int $uid): array
1616
{
1717
return $this->batches->get($uid);
1818
}

src/Endpoints/Delegates/HandlesDocuments.php

Lines changed: 17 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,12 @@
77
use Meilisearch\Contracts\DocumentsQuery;
88
use Meilisearch\Contracts\DocumentsResults;
99
use Meilisearch\Exceptions\ApiException;
10-
use Meilisearch\Exceptions\InvalidArgumentException;
1110
use Meilisearch\Exceptions\InvalidResponseBodyException;
1211

1312
trait HandlesDocuments
1413
{
15-
public function getDocument($documentId, ?array $fields = null)
14+
public function getDocument(string|int $documentId, ?array $fields = null): array
1615
{
17-
$this->assertValidDocumentId($documentId);
1816
$query = isset($fields) ? ['fields' => implode(',', $fields)] : [];
1917

2018
return $this->http->get(self::PATH.'/'.$this->uid.'/documents/'.$documentId, $query);
@@ -38,27 +36,27 @@ public function getDocuments(?DocumentsQuery $options = null): DocumentsResults
3836
}
3937
}
4038

41-
public function addDocuments(array $documents, ?string $primaryKey = null)
39+
public function addDocuments(array $documents, ?string $primaryKey = null): array
4240
{
4341
return $this->http->post(self::PATH.'/'.$this->uid.'/documents', $documents, ['primaryKey' => $primaryKey]);
4442
}
4543

46-
public function addDocumentsJson(string $documents, ?string $primaryKey = null)
44+
public function addDocumentsJson(string $documents, ?string $primaryKey = null): array
4745
{
4846
return $this->http->post(self::PATH.'/'.$this->uid.'/documents', $documents, ['primaryKey' => $primaryKey], 'application/json');
4947
}
5048

51-
public function addDocumentsCsv(string $documents, ?string $primaryKey = null, ?string $delimiter = null)
49+
public function addDocumentsCsv(string $documents, ?string $primaryKey = null, ?string $delimiter = null): array
5250
{
5351
return $this->http->post(self::PATH.'/'.$this->uid.'/documents', $documents, ['primaryKey' => $primaryKey, 'csvDelimiter' => $delimiter], 'text/csv');
5452
}
5553

56-
public function addDocumentsNdjson(string $documents, ?string $primaryKey = null)
54+
public function addDocumentsNdjson(string $documents, ?string $primaryKey = null): array
5755
{
5856
return $this->http->post(self::PATH.'/'.$this->uid.'/documents', $documents, ['primaryKey' => $primaryKey], 'application/x-ndjson');
5957
}
6058

61-
public function addDocumentsInBatches(array $documents, ?int $batchSize = 1000, ?string $primaryKey = null)
59+
public function addDocumentsInBatches(array $documents, ?int $batchSize = 1000, ?string $primaryKey = null): array
6260
{
6361
$promises = [];
6462

@@ -69,7 +67,7 @@ public function addDocumentsInBatches(array $documents, ?int $batchSize = 1000,
6967
return $promises;
7068
}
7169

72-
public function addDocumentsCsvInBatches(string $documents, ?int $batchSize = 1000, ?string $primaryKey = null, ?string $delimiter = null)
70+
public function addDocumentsCsvInBatches(string $documents, ?int $batchSize = 1000, ?string $primaryKey = null, ?string $delimiter = null): array
7371
{
7472
$promises = [];
7573

@@ -80,7 +78,7 @@ public function addDocumentsCsvInBatches(string $documents, ?int $batchSize = 10
8078
return $promises;
8179
}
8280

83-
public function addDocumentsNdjsonInBatches(string $documents, ?int $batchSize = 1000, ?string $primaryKey = null)
81+
public function addDocumentsNdjsonInBatches(string $documents, ?int $batchSize = 1000, ?string $primaryKey = null): array
8482
{
8583
$promises = [];
8684

@@ -91,27 +89,27 @@ public function addDocumentsNdjsonInBatches(string $documents, ?int $batchSize =
9189
return $promises;
9290
}
9391

94-
public function updateDocuments(array $documents, ?string $primaryKey = null)
92+
public function updateDocuments(array $documents, ?string $primaryKey = null): array
9593
{
9694
return $this->http->put(self::PATH.'/'.$this->uid.'/documents', $documents, ['primaryKey' => $primaryKey]);
9795
}
9896

99-
public function updateDocumentsJson(string $documents, ?string $primaryKey = null)
97+
public function updateDocumentsJson(string $documents, ?string $primaryKey = null): array
10098
{
10199
return $this->http->put(self::PATH.'/'.$this->uid.'/documents', $documents, ['primaryKey' => $primaryKey], 'application/json');
102100
}
103101

104-
public function updateDocumentsCsv(string $documents, ?string $primaryKey = null, ?string $delimiter = null)
102+
public function updateDocumentsCsv(string $documents, ?string $primaryKey = null, ?string $delimiter = null): array
105103
{
106104
return $this->http->put(self::PATH.'/'.$this->uid.'/documents', $documents, ['primaryKey' => $primaryKey, 'csvDelimiter' => $delimiter], 'text/csv');
107105
}
108106

109-
public function updateDocumentsNdjson(string $documents, ?string $primaryKey = null)
107+
public function updateDocumentsNdjson(string $documents, ?string $primaryKey = null): array
110108
{
111109
return $this->http->put(self::PATH.'/'.$this->uid.'/documents', $documents, ['primaryKey' => $primaryKey], 'application/x-ndjson');
112110
}
113111

114-
public function updateDocumentsInBatches(array $documents, ?int $batchSize = 1000, ?string $primaryKey = null)
112+
public function updateDocumentsInBatches(array $documents, ?int $batchSize = 1000, ?string $primaryKey = null): array
115113
{
116114
$promises = [];
117115

@@ -122,7 +120,7 @@ public function updateDocumentsInBatches(array $documents, ?int $batchSize = 100
122120
return $promises;
123121
}
124122

125-
public function updateDocumentsCsvInBatches(string $documents, ?int $batchSize = 1000, ?string $primaryKey = null, ?string $delimiter = null)
123+
public function updateDocumentsCsvInBatches(string $documents, ?int $batchSize = 1000, ?string $primaryKey = null, ?string $delimiter = null): array
126124
{
127125
$promises = [];
128126

@@ -133,7 +131,7 @@ public function updateDocumentsCsvInBatches(string $documents, ?int $batchSize =
133131
return $promises;
134132
}
135133

136-
public function updateDocumentsNdjsonInBatches(string $documents, ?int $batchSize = 1000, ?string $primaryKey = null)
134+
public function updateDocumentsNdjsonInBatches(string $documents, ?int $batchSize = 1000, ?string $primaryKey = null): array
137135
{
138136
$promises = [];
139137

@@ -154,7 +152,7 @@ public function updateDocumentsNdjsonInBatches(string $documents, ?int $batchSiz
154152
* @param non-empty-string $function
155153
* @param array{filter?: non-empty-string|list<non-empty-string>|null, context?: array<non-empty-string, mixed>} $options
156154
*/
157-
public function updateDocumentsByFunction(string $function, array $options = [])
155+
public function updateDocumentsByFunction(string $function, array $options = []): array
158156
{
159157
return $this->http->post(self::PATH.'/'.$this->uid.'/documents/edit', array_merge(['function' => $function], $options));
160158
}
@@ -164,10 +162,8 @@ public function deleteAllDocuments(): array
164162
return $this->http->delete(self::PATH.'/'.$this->uid.'/documents');
165163
}
166164

167-
public function deleteDocument($documentId): array
165+
public function deleteDocument(string|int $documentId): array
168166
{
169-
$this->assertValidDocumentId($documentId);
170-
171167
return $this->http->delete(self::PATH.'/'.$this->uid.'/documents/'.$documentId);
172168
}
173169

@@ -186,17 +182,6 @@ public function deleteDocuments(array $options): array
186182
}
187183
}
188184

189-
private function assertValidDocumentId($documentId): void
190-
{
191-
if (!\is_string($documentId) && !\is_int($documentId)) {
192-
throw InvalidArgumentException::invalidType('documentId', ['string', 'int']);
193-
}
194-
195-
if (\is_string($documentId) && '' === trim($documentId)) {
196-
throw InvalidArgumentException::emptyArgument('documentId');
197-
}
198-
}
199-
200185
private static function batchCsvString(string $documents, int $batchSize): \Generator
201186
{
202187
$parsedDocuments = preg_split("/\r\n|\n|\r/", trim($documents));

src/Endpoints/Delegates/HandlesIndex.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,31 +17,49 @@ public function getIndexes(?IndexesQuery $options = null): IndexesResults
1717
return $this->index->all($options ?? null);
1818
}
1919

20+
/**
21+
* @param non-empty-string $uid
22+
*/
2023
public function getRawIndex(string $uid): array
2124
{
2225
return $this->index($uid)->fetchRawInfo();
2326
}
2427

28+
/**
29+
* @param non-empty-string $uid
30+
*/
2531
public function index(string $uid): Indexes
2632
{
2733
return new Indexes($this->http, $uid);
2834
}
2935

36+
/**
37+
* @param non-empty-string $uid
38+
*/
3039
public function getIndex(string $uid): Indexes
3140
{
3241
return $this->index($uid)->fetchInfo();
3342
}
3443

44+
/**
45+
* @param non-empty-string $uid
46+
*/
3547
public function deleteIndex(string $uid): array
3648
{
3749
return $this->index($uid)->delete();
3850
}
3951

52+
/**
53+
* @param non-empty-string $uid
54+
*/
4055
public function createIndex(string $uid, array $options = []): array
4156
{
4257
return $this->index->create($uid, $options);
4358
}
4459

60+
/**
61+
* @param non-empty-string $uid
62+
*/
4563
public function updateIndex(string $uid, array $options = []): array
4664
{
4765
return $this->index($uid)->update($options);

src/Endpoints/Delegates/HandlesTasks.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ trait HandlesTasks
1515
{
1616
protected Tasks $tasks;
1717

18-
public function getTask($uid): array
18+
public function getTask(int $uid): array
1919
{
2020
return $this->tasks->get($uid);
2121
}
@@ -42,15 +42,17 @@ public function cancelTasks(?CancelTasksQuery $options = null): array
4242
/**
4343
* @throws TimeOutException
4444
*/
45-
public function waitForTask($uid, int $timeoutInMs = 5000, int $intervalInMs = 50): array
45+
public function waitForTask(int $uid, int $timeoutInMs = 5000, int $intervalInMs = 50): array
4646
{
4747
return $this->tasks->waitTask($uid, $timeoutInMs, $intervalInMs);
4848
}
4949

5050
/**
51+
* @param array<int> $uids
52+
*
5153
* @throws TimeOutException
5254
*/
53-
public function waitForTasks($uids, int $timeoutInMs = 5000, int $intervalInMs = 50): array
55+
public function waitForTasks(array $uids, int $timeoutInMs = 5000, int $intervalInMs = 50): array
5456
{
5557
return $this->tasks->waitTasks($uids, $timeoutInMs, $intervalInMs);
5658
}

src/Endpoints/Delegates/TasksQueryTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ protected function baseArray(): array
148148

149149
private function formatDate(?\DateTimeInterface $date): ?string
150150
{
151-
return null !== $date ? $date->format(\DateTimeInterface::RFC3339) : null;
151+
return $date?->format(\DateTimeInterface::RFC3339);
152152
}
153153

154154
private function formatArray(?array $array): ?string

src/Endpoints/Indexes.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ public function fetchInfo(): self
136136
return $this->fill($response);
137137
}
138138

139-
public function update($body): array
139+
public function update(array $body): array
140140
{
141141
return $this->http->patch(self::PATH.'/'.$this->uid, $body);
142142
}
@@ -156,7 +156,7 @@ public function swapIndexes(array $indexes): array
156156

157157
// Tasks
158158

159-
public function getTask($uid): array
159+
public function getTask(int $uid): array
160160
{
161161
return $this->http->get('/tasks/'.$uid);
162162
}
@@ -179,11 +179,9 @@ public function getTasks(?TasksQuery $options = null): TasksResults
179179
// Search
180180

181181
/**
182-
* @return SearchResult|array
183-
*
184182
* @phpstan-return ($options is array{raw: true|non-falsy-string|positive-int} ? array : SearchResult)
185183
*/
186-
public function search(?string $query, array $searchParams = [], array $options = [])
184+
public function search(?string $query, array $searchParams = [], array $options = []): SearchResult|array
187185
{
188186
$result = $this->rawSearch($query, $searchParams);
189187

0 commit comments

Comments
 (0)