Skip to content

Commit 8c3ca6a

Browse files
committed
improve types
1 parent f36da90 commit 8c3ca6a

13 files changed

Lines changed: 62 additions & 49 deletions

src/BulkWriteResult.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,20 @@
44

55
namespace Patchlevel\Rango;
66

7-
final class BulkWriteResult
7+
final readonly class BulkWriteResult
88
{
99
/**
1010
* @param array<int, mixed> $insertedIds
1111
* @param array<int, mixed> $upsertedIds
1212
*/
1313
public function __construct(
14-
private readonly int $insertedCount,
15-
private readonly int $matchedCount,
16-
private readonly int $modifiedCount,
17-
private readonly int $deletedCount,
18-
private readonly int $upsertedCount,
19-
private readonly array $insertedIds,
20-
private readonly array $upsertedIds,
14+
private int $insertedCount,
15+
private int $matchedCount,
16+
private int $modifiedCount,
17+
private int $deletedCount,
18+
private int $upsertedCount,
19+
private array $insertedIds,
20+
private array $upsertedIds,
2121
) {
2222
}
2323

src/Client.php

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

77
use Patchlevel\Rango\Sql\Identifier;
88
use PDO;
9-
109
use function sprintf;
1110

1211
final class Client
@@ -34,6 +33,16 @@ public function selectDatabase(string $name): Database
3433
return $this->getDatabase($name);
3534
}
3635

36+
/**
37+
* @template TDocument of array<string, mixed>
38+
*
39+
* @return Collection<TDocument>
40+
*/
41+
public function selectCollection(string $databaseName, string $collectionName): Collection
42+
{
43+
return $this->getDatabase($databaseName)->getCollection($collectionName);
44+
}
45+
3746
public function dropDatabase(string $name): void
3847
{
3948
SqlRunner::exec($this->pdo, sprintf('DROP SCHEMA IF EXISTS %s CASCADE', Identifier::quote($name)));

src/Collection.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,16 +72,16 @@ public function findOne(array $filter = [], array $options = []): array|null
7272
}
7373

7474
/**
75-
* @param list<array<string, mixed>> $documents
76-
* @param array<string, mixed> $options
75+
* @param list<TDocument> $documents
76+
* @param array<string, mixed> $options
7777
*/
7878
public function insertMany(array $documents, array $options = []): InsertManyResult
7979
{
8080
return $this->client->run(new Operation\InsertMany($this->databaseName, $this->collectionName, $documents, $options));
8181
}
8282

8383
/**
84-
* @param array<string, mixed> $document
84+
* @param TDocument $document
8585
* @param array<string, mixed> $options
8686
*/
8787
public function insertOne(array $document, array $options = []): InsertOneResult
@@ -91,7 +91,7 @@ public function insertOne(array $document, array $options = []): InsertOneResult
9191

9292
/**
9393
* @param array<string, mixed> $filter
94-
* @param array<string, mixed> $replacement
94+
* @param TDocument $replacement
9595
* @param array<string, mixed> $options
9696
*/
9797
public function replaceOne(array $filter, array $replacement, array $options = []): UpdateResult
@@ -161,7 +161,7 @@ public function findOneAndDelete(array $filter, array $options = []): array|null
161161

162162
/**
163163
* @param array<string, mixed> $filter
164-
* @param array<string, mixed> $replacement
164+
* @param TDocument $replacement
165165
* @param array<string, mixed> $options
166166
*
167167
* @return TDocument|null

src/Cursor.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@
2424
* @template TDocument of array<string, mixed>
2525
* @implements IteratorAggregate<int, TDocument>
2626
*/
27-
class Cursor implements IteratorAggregate, Countable
27+
final readonly class Cursor implements IteratorAggregate, Countable
2828
{
2929
/** @param list<string>|PDOStatement $data */
3030
public function __construct(
31-
private readonly array|PDOStatement $data = [],
31+
private array|PDOStatement $data = [],
3232
) {
3333
}
3434

@@ -62,24 +62,20 @@ public function count(): int
6262
return count($this->data);
6363
}
6464

65-
/** @return array<int|string, TDocument> */
65+
/** @return list<TDocument> */
6666
public function toArray(): array
6767
{
6868
if ($this->data instanceof PDOStatement) {
69-
$data = array_map(
69+
return array_map(
7070
fn ($item) => $this->decode(is_string($item) ? $item : ''),
7171
$this->data->fetchAll(PDO::FETCH_COLUMN),
7272
);
73-
74-
return $data;
7573
}
7674

77-
$data = array_map(
75+
return array_map(
7876
fn (string $item) => $this->decode($item),
7977
$this->data,
8078
);
81-
82-
return $data;
8379
}
8480

8581
/** @return array<string, mixed> */

src/Database.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,21 @@ public function __construct(
1212
) {
1313
}
1414

15-
/** @return Collection<array<string, mixed>> */
15+
/**
16+
* @template TDocument of array<string, mixed>
17+
*
18+
* @return Collection<TDocument>
19+
*/
1620
public function getCollection(string $collectionName): Collection
1721
{
1822
return new Collection($this->client, $this->databaseName, $collectionName);
1923
}
2024

21-
/** @return Collection<array<string, mixed>> */
25+
/**
26+
* @template TDocument of array<string, mixed>
27+
*
28+
* @return Collection<TDocument>
29+
*/
2230
public function selectCollection(string $collectionName): Collection
2331
{
2432
return $this->getCollection($collectionName);

src/DeleteResult.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
namespace Patchlevel\Rango;
66

7-
final class DeleteResult
7+
final readonly class DeleteResult
88
{
99
public function __construct(
10-
private readonly int $deletedCount,
10+
private int $deletedCount,
1111
) {
1212
}
1313

src/InsertManyResult.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66

77
use function count;
88

9-
final class InsertManyResult
9+
final readonly class InsertManyResult
1010
{
1111
/** @param array<int, mixed> $insertedIds */
1212
public function __construct(
13-
private readonly array $insertedIds,
13+
private array $insertedIds,
1414
) {
1515
}
1616

src/InsertOneResult.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
namespace Patchlevel\Rango;
66

7-
final class InsertOneResult
7+
final readonly class InsertOneResult
88
{
99
public function __construct(
10-
private readonly mixed $insertedId,
10+
private mixed $insertedId,
1111
) {
1212
}
1313

src/Query/AggregationBuilder.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@
1717
use function str_replace;
1818
use function str_starts_with;
1919

20-
final class AggregationBuilder
20+
final readonly class AggregationBuilder
2121
{
2222
public function __construct(
23-
private readonly PDO $pdo,
24-
private readonly FilterBuilder $filterBuilder,
25-
private readonly ProjectionBuilder $projectionBuilder,
23+
private PDO $pdo,
24+
private FilterBuilder $filterBuilder,
25+
private ProjectionBuilder $projectionBuilder,
2626
) {
2727
}
2828

src/Query/FilterBuilder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@
1919
use function str_contains;
2020
use function str_starts_with;
2121

22-
final class FilterBuilder
22+
final readonly class FilterBuilder
2323
{
2424
public function __construct(
25-
private readonly PDO $pdo,
25+
private PDO $pdo,
2626
) {
2727
}
2828

0 commit comments

Comments
 (0)