Skip to content

Commit 8bf200c

Browse files
committed
fix some phpstan errors
1 parent 15ff110 commit 8bf200c

26 files changed

Lines changed: 292 additions & 68 deletions

src/Client.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,22 @@ public function dropDatabase(string $name): void
3636
$this->pdo->exec(sprintf('DROP SCHEMA IF EXISTS %s CASCADE', $name));
3737
}
3838

39+
/** @return list<array{name: string}> */
3940
public function listDatabases(): array
4041
{
41-
return $this->run(new Operation\ListDatabases());
42+
/** @var list<array{name: string}> $databases */
43+
$databases = $this->run(new Operation\ListDatabases());
44+
45+
return $databases;
4246
}
4347

48+
/** @return list<array{name: string}> */
4449
public function listCollections(string $database): array
4550
{
46-
return $this->run(new Operation\ListCollections($database));
51+
/** @var list<array{name: string}> $collections */
52+
$collections = $this->run(new Operation\ListCollections($database));
53+
54+
return $collections;
4755
}
4856

4957
public function renameCollection(string $database, string $oldName, string $newName): void

src/Collection.php

Lines changed: 64 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace Patchlevel\Rango;
66

7-
/** @template T of array */
87
final class Collection
98
{
109
public function __construct(
@@ -20,7 +19,10 @@ public function __construct(
2019
*/
2120
public function countDocuments(array $filter = [], array $options = []): int
2221
{
23-
return $this->client->run(new Operation\Count($this->databaseName, $this->collectionName, $filter, $options));
22+
/** @var int $count */
23+
$count = $this->client->run(new Operation\Count($this->databaseName, $this->collectionName, $filter, $options));
24+
25+
return $count;
2426
}
2527

2628
/**
@@ -29,7 +31,10 @@ public function countDocuments(array $filter = [], array $options = []): int
2931
*/
3032
public function deleteMany(array $filter, array $options = []): DeleteResult
3133
{
32-
return $this->client->run(new Operation\Delete($this->databaseName, $this->collectionName, $filter, true));
34+
/** @var DeleteResult $result */
35+
$result = $this->client->run(new Operation\Delete($this->databaseName, $this->collectionName, $filter, true));
36+
37+
return $result;
3338
}
3439

3540
/**
@@ -38,7 +43,10 @@ public function deleteMany(array $filter, array $options = []): DeleteResult
3843
*/
3944
public function deleteOne(array $filter, array $options = []): DeleteResult
4045
{
41-
return $this->client->run(new Operation\Delete($this->databaseName, $this->collectionName, $filter, false));
46+
/** @var DeleteResult $result */
47+
$result = $this->client->run(new Operation\Delete($this->databaseName, $this->collectionName, $filter, false));
48+
49+
return $result;
4250
}
4351

4452
public function drop(): void
@@ -50,21 +58,25 @@ public function drop(): void
5058
* @param array<string, mixed> $filter
5159
* @param array<string, mixed> $options
5260
*
53-
* @return Cursor<T>
61+
* @return Cursor
5462
*/
5563
public function find(array $filter = [], array $options = []): Cursor
5664
{
57-
return $this->client->run(new Operation\Find($this->databaseName, $this->collectionName, $filter, $options));
65+
/** @var Cursor $cursor */
66+
$cursor = $this->client->run(new Operation\Find($this->databaseName, $this->collectionName, $filter, $options));
67+
68+
return $cursor;
5869
}
5970

6071
/**
6172
* @param array<string, mixed> $filter
6273
* @param array<string, mixed> $options
6374
*
64-
* @return T|null
75+
* @return array<string, mixed>|null
6576
*/
6677
public function findOne(array $filter = [], array $options = []): array|null
6778
{
79+
/** @var Cursor|null $result */
6880
$result = $this->client->run(new Operation\FindOne($this->databaseName, $this->collectionName, $filter, $options));
6981

7082
if (!$result instanceof Cursor) {
@@ -82,7 +94,10 @@ public function findOne(array $filter = [], array $options = []): array|null
8294
*/
8395
public function insertMany(array $documents, array $options = []): InsertManyResult
8496
{
85-
return $this->client->run(new Operation\InsertMany($this->databaseName, $this->collectionName, $documents, $options));
97+
/** @var InsertManyResult $result */
98+
$result = $this->client->run(new Operation\InsertMany($this->databaseName, $this->collectionName, $documents, $options));
99+
100+
return $result;
86101
}
87102

88103
/**
@@ -91,7 +106,10 @@ public function insertMany(array $documents, array $options = []): InsertManyRes
91106
*/
92107
public function insertOne(array $document, array $options = []): InsertOneResult
93108
{
94-
return $this->client->run(new Operation\InsertOne($this->databaseName, $this->collectionName, $document, $options));
109+
/** @var InsertOneResult $result */
110+
$result = $this->client->run(new Operation\InsertOne($this->databaseName, $this->collectionName, $document, $options));
111+
112+
return $result;
95113
}
96114

97115
/**
@@ -101,7 +119,10 @@ public function insertOne(array $document, array $options = []): InsertOneResult
101119
*/
102120
public function replaceOne(array $filter, array $replacement, array $options = []): UpdateResult
103121
{
104-
return $this->client->run(new Operation\ReplaceOne($this->databaseName, $this->collectionName, $filter, $replacement, $options));
122+
/** @var UpdateResult $result */
123+
$result = $this->client->run(new Operation\ReplaceOne($this->databaseName, $this->collectionName, $filter, $replacement, $options));
124+
125+
return $result;
105126
}
106127

107128
/**
@@ -111,7 +132,10 @@ public function replaceOne(array $filter, array $replacement, array $options = [
111132
*/
112133
public function updateMany(array $filter, array $update, array $options = []): UpdateResult
113134
{
114-
return $this->client->run(new Operation\Update($this->databaseName, $this->collectionName, $filter, $update, $options, true));
135+
/** @var UpdateResult $result */
136+
$result = $this->client->run(new Operation\Update($this->databaseName, $this->collectionName, $filter, $update, $options, true));
137+
138+
return $result;
115139
}
116140

117141
/**
@@ -121,18 +145,24 @@ public function updateMany(array $filter, array $update, array $options = []): U
121145
*/
122146
public function updateOne(array $filter, array $update, array $options = []): UpdateResult
123147
{
124-
return $this->client->run(new Operation\Update($this->databaseName, $this->collectionName, $filter, $update, $options, false));
148+
/** @var UpdateResult $result */
149+
$result = $this->client->run(new Operation\Update($this->databaseName, $this->collectionName, $filter, $update, $options, false));
150+
151+
return $result;
125152
}
126153

127154
/**
128155
* @param list<array<string, mixed>> $pipeline
129156
* @param array<string, mixed> $options
130157
*
131-
* @return Cursor<T>
158+
* @return Cursor
132159
*/
133160
public function aggregate(array $pipeline, array $options = []): Cursor
134161
{
135-
return $this->client->run(new Operation\Aggregate($this->databaseName, $this->collectionName, $pipeline, $options));
162+
/** @var Cursor $cursor */
163+
$cursor = $this->client->run(new Operation\Aggregate($this->databaseName, $this->collectionName, $pipeline, $options));
164+
165+
return $cursor;
136166
}
137167

138168
/**
@@ -143,17 +173,21 @@ public function aggregate(array $pipeline, array $options = []): Cursor
143173
*/
144174
public function distinct(string $fieldName, array $filter = [], array $options = []): array
145175
{
146-
return $this->client->run(new Operation\Distinct($this->databaseName, $this->collectionName, $fieldName, $filter, $options));
176+
/** @var list<mixed> $result */
177+
$result = $this->client->run(new Operation\Distinct($this->databaseName, $this->collectionName, $fieldName, $filter, $options));
178+
179+
return $result;
147180
}
148181

149182
/**
150183
* @param array<string, mixed> $filter
151184
* @param array<string, mixed> $options
152185
*
153-
* @return T|null
186+
* @return array<string, mixed>|null
154187
*/
155188
public function findOneAndDelete(array $filter, array $options = []): array|null
156189
{
190+
/** @var Cursor|null $result */
157191
$result = $this->client->run(new Operation\FindOneAndDelete($this->databaseName, $this->collectionName, $filter, $options));
158192

159193
if (!$result instanceof Cursor) {
@@ -170,10 +204,11 @@ public function findOneAndDelete(array $filter, array $options = []): array|null
170204
* @param array<string, mixed> $replacement
171205
* @param array<string, mixed> $options
172206
*
173-
* @return T|null
207+
* @return array<string, mixed>|null
174208
*/
175209
public function findOneAndReplace(array $filter, array $replacement, array $options = []): array|null
176210
{
211+
/** @var Cursor|null $result */
177212
$result = $this->client->run(new Operation\FindOneAndReplace($this->databaseName, $this->collectionName, $filter, $replacement, $options));
178213

179214
if (!$result instanceof Cursor) {
@@ -190,10 +225,11 @@ public function findOneAndReplace(array $filter, array $replacement, array $opti
190225
* @param array<string, mixed> $update
191226
* @param array<string, mixed> $options
192227
*
193-
* @return T|null
228+
* @return array<string, mixed>|null
194229
*/
195230
public function findOneAndUpdate(array $filter, array $update, array $options = []): array|null
196231
{
232+
/** @var Cursor|null $result */
197233
$result = $this->client->run(new Operation\FindOneAndUpdate($this->databaseName, $this->collectionName, $filter, $update, $options));
198234

199235
if (!$result instanceof Cursor) {
@@ -206,12 +242,15 @@ public function findOneAndUpdate(array $filter, array $update, array $options =
206242
}
207243

208244
/**
209-
* @param list<array<string, array<string, mixed>>> $operations
210-
* @param array<string, mixed> $options
245+
* @param list<array<string, list<array<string, mixed>>>> $operations
246+
* @param array<string, mixed> $options
211247
*/
212248
public function bulkWrite(array $operations, array $options = []): BulkWriteResult
213249
{
214-
return $this->client->run(new Operation\BulkWrite($this->databaseName, $this->collectionName, $operations, $options));
250+
/** @var BulkWriteResult $result */
251+
$result = $this->client->run(new Operation\BulkWrite($this->databaseName, $this->collectionName, $operations, $options));
252+
253+
return $result;
215254
}
216255

217256
/**
@@ -231,6 +270,9 @@ public function dropIndex(string $name): void
231270
/** @return list<array{name: string}> */
232271
public function listIndexes(): array
233272
{
234-
return $this->client->run(new Operation\ListIndexes($this->databaseName, $this->collectionName));
273+
/** @var list<array{name: string}> $indexes */
274+
$indexes = $this->client->run(new Operation\ListIndexes($this->databaseName, $this->collectionName));
275+
276+
return $indexes;
235277
}
236278
}

src/Cursor.php

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@
66

77
use Countable;
88
use IteratorAggregate;
9+
use PDO;
910
use PDOStatement;
1011
use Traversable;
1112

1213
use function array_map;
1314
use function count;
15+
use function is_array;
16+
use function is_string;
1417
use function json_decode;
1518

16-
/**
17-
* @template T
18-
* @implements IteratorAggregate<int, T>
19-
*/
19+
/** @implements IteratorAggregate<int, array<string, mixed>> */
2020
class Cursor implements IteratorAggregate, Countable
2121
{
2222
/** @param list<string>|PDOStatement $data */
@@ -29,15 +29,20 @@ public function __construct(
2929
public function getIterator(): Traversable
3030
{
3131
if ($this->data instanceof PDOStatement) {
32-
while ($item = $this->data->fetchColumn()) {
33-
yield json_decode($item, true);
32+
while (true) {
33+
$item = $this->data->fetchColumn();
34+
if ($item === false) {
35+
break;
36+
}
37+
38+
yield $this->decode((string) $item);
3439
}
3540

3641
return;
3742
}
3843

3944
foreach ($this->data as $item) {
40-
yield json_decode($item, true);
45+
yield $this->decode($item);
4146
}
4247
}
4348

@@ -50,19 +55,36 @@ public function count(): int
5055
return count($this->data);
5156
}
5257

53-
/** @return list<array<string, mixed>> */
58+
/** @return array<int|string, array<string, mixed>> */
5459
public function toArray(): array
5560
{
5661
if ($this->data instanceof PDOStatement) {
57-
return array_map(
58-
static fn (string $item) => json_decode($item, true),
62+
$data = array_map(
63+
fn ($item) => $this->decode(is_string($item) ? $item : ''),
5964
$this->data->fetchAll(PDO::FETCH_COLUMN),
6065
);
66+
67+
return $data;
6168
}
6269

63-
return array_map(
64-
static fn (string $item) => json_decode($item, true),
70+
$data = array_map(
71+
fn (string $item) => $this->decode($item),
6572
$this->data,
6673
);
74+
75+
return $data;
76+
}
77+
78+
/** @return array<string, mixed> */
79+
private function decode(string $json): array
80+
{
81+
$decoded = json_decode($json, true);
82+
83+
if (!is_array($decoded)) {
84+
return [];
85+
}
86+
87+
/** @var array<string, mixed> $decoded */
88+
return $decoded;
6789
}
6890
}

src/Database.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@ public function __construct(
1212
) {
1313
}
1414

15+
/** @return Collection */
1516
public function getCollection(string $collectionName): Collection
1617
{
1718
return new Collection($this->client, $this->databaseName, $collectionName);
1819
}
1920

21+
/** @return Collection */
2022
public function selectCollection(string $collectionName): Collection
2123
{
2224
return $this->getCollection($collectionName);
@@ -25,7 +27,10 @@ public function selectCollection(string $collectionName): Collection
2527
/** @return list<array{name: string}> */
2628
public function listCollections(): array
2729
{
28-
return $this->client->listCollections($this->databaseName);
30+
/** @var list<array{name: string}> $collections */
31+
$collections = $this->client->listCollections($this->databaseName);
32+
33+
return $collections;
2934
}
3035

3136
public function renameCollection(string $oldName, string $newName): void

src/Operation/Aggregate.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@
1010

1111
final class Aggregate implements Operation
1212
{
13-
/** @param array<string, mixed> $options */
13+
/**
14+
* @param list<array<string, mixed>> $pipeline
15+
* @param array<string, mixed> $options
16+
*/
1417
public function __construct(
1518
public readonly string $database,
1619
public readonly string $collection,
@@ -19,6 +22,7 @@ public function __construct(
1922
) {
2023
}
2124

25+
/** @return Cursor */
2226
public function execute(PDO $pdo, QueryBuilder $queryBuilder): Cursor
2327
{
2428
$sql = $queryBuilder->createAggregate($this->database, $this->collection, $this->pipeline, $this->options);

0 commit comments

Comments
 (0)