Skip to content

Commit f731e0f

Browse files
committed
Rely on CursorInterface when possible
1 parent 781cc7c commit f731e0f

10 files changed

+31
-26
lines changed

src/Model/CodecCursor.php

+8-5
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
use MongoDB\BSON\Document;
2222
use MongoDB\BSON\Int64;
2323
use MongoDB\Codec\DocumentCodec;
24-
use MongoDB\Driver\Cursor;
2524
use MongoDB\Driver\CursorId;
2625
use MongoDB\Driver\CursorInterface;
2726
use MongoDB\Driver\Server;
@@ -60,10 +59,11 @@ public function current(): ?object
6059

6160
/**
6261
* @template NativeClass of Object
63-
* @param DocumentCodec<NativeClass> $codec
62+
* @param CursorInterface<int, NativeClass> &Iterator<int, NativeClass> $cursor
63+
* @param DocumentCodec<NativeClass> $codec
6464
* @return self<NativeClass>
6565
*/
66-
public static function fromCursor(Cursor $cursor, DocumentCodec $codec): self
66+
public static function fromCursor(CursorInterface&Iterator $cursor, DocumentCodec $codec): self
6767
{
6868
$cursor->setTypeMap(self::TYPEMAP);
6969

@@ -132,8 +132,11 @@ public function valid(): bool
132132
return $this->cursor->valid();
133133
}
134134

135-
/** @param DocumentCodec<TValue> $codec */
136-
private function __construct(private Cursor $cursor, private DocumentCodec $codec)
135+
/**
136+
* @param CursorInterface<int, TValue> &Iterator<int, TValue> $cursor
137+
* @param DocumentCodec<TValue> $codec
138+
*/
139+
private function __construct(private CursorInterface&Iterator $cursor, private DocumentCodec $codec)
137140
{
138141
}
139142
}

src/Operation/Aggregate.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
use Iterator;
2121
use MongoDB\Codec\DocumentCodec;
2222
use MongoDB\Driver\Command;
23-
use MongoDB\Driver\Cursor;
2423
use MongoDB\Driver\CursorInterface;
2524
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
2625
use MongoDB\Driver\ReadConcern;
@@ -318,7 +317,7 @@ private function createCommandOptions(): array
318317
* @see https://php.net/manual/en/mongodb-driver-server.executereadcommand.php
319318
* @see https://php.net/manual/en/mongodb-driver-server.executereadwritecommand.php
320319
*/
321-
private function executeCommand(Server $server, Command $command): Cursor
320+
private function executeCommand(Server $server, Command $command): CursorInterface&Iterator
322321
{
323322
$options = [];
324323

src/Operation/DatabaseCommand.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@
1717

1818
namespace MongoDB\Operation;
1919

20+
use Iterator;
2021
use MongoDB\Driver\Command;
21-
use MongoDB\Driver\Cursor;
22+
use MongoDB\Driver\CursorInterface;
2223
use MongoDB\Driver\ReadPreference;
2324
use MongoDB\Driver\Server;
2425
use MongoDB\Driver\Session;
@@ -83,7 +84,7 @@ public function __construct(private string $databaseName, array|object $command,
8384
*
8485
* @see Executable::execute()
8586
*/
86-
public function execute(Server $server): Cursor
87+
public function execute(Server $server): CursorInterface&Iterator
8788
{
8889
$cursor = $server->executeCommand($this->databaseName, $this->command, $this->createOptions());
8990

src/Operation/Find.php

+1
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,7 @@ public function execute(Server $server): CursorInterface&Iterator
313313
$cursor = $server->executeQuery($this->databaseName . '.' . $this->collectionName, new Query($this->filter, $this->createQueryOptions()), $this->createExecuteOptions());
314314

315315
if (isset($this->options['codec'])) {
316+
/** @var CursorInterface<int, object>&Iterator<int, object> $cursor */
316317
return CodecCursor::fromCursor($cursor, $this->options['codec']);
317318
}
318319

tests/Database/DatabaseFunctionalTest.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use MongoDB\Collection;
77
use MongoDB\Database;
88
use MongoDB\Driver\BulkWrite;
9-
use MongoDB\Driver\Cursor;
9+
use MongoDB\Driver\CursorInterface;
1010
use MongoDB\Driver\ReadConcern;
1111
use MongoDB\Driver\ReadPreference;
1212
use MongoDB\Driver\WriteConcern;
@@ -79,7 +79,7 @@ public function testCommand(): void
7979

8080
$cursor = $this->database->command($command, $options);
8181

82-
$this->assertInstanceOf(Cursor::class, $cursor);
82+
$this->assertInstanceOf(CursorInterface::class, $cursor);
8383
$commandResult = current($cursor->toArray());
8484

8585
$this->assertCommandSucceeded($commandResult);
@@ -99,7 +99,7 @@ public function testCommandDoesNotInheritReadPreference(): void
9999

100100
$cursor = $this->database->command($command);
101101

102-
$this->assertInstanceOf(Cursor::class, $cursor);
102+
$this->assertInstanceOf(CursorInterface::class, $cursor);
103103
$this->assertTrue($cursor->getServer()->isPrimary());
104104
}
105105

@@ -113,7 +113,7 @@ public function testCommandAppliesTypeMapToCursor(): void
113113

114114
$cursor = $this->database->command($command, $options);
115115

116-
$this->assertInstanceOf(Cursor::class, $cursor);
116+
$this->assertInstanceOf(CursorInterface::class, $cursor);
117117
$commandResult = current($cursor->toArray());
118118

119119
$this->assertCommandSucceeded($commandResult);

tests/DocumentationExamplesTest.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use MongoDB\Collection;
99
use MongoDB\Database;
1010
use MongoDB\Driver\Cursor;
11+
use MongoDB\Driver\CursorInterface;
1112
use MongoDB\Driver\Exception\CommandException;
1213
use MongoDB\Driver\Exception\Exception;
1314
use MongoDB\Driver\ReadPreference;
@@ -1961,7 +1962,7 @@ public function testQueryableEncryption(): void
19611962
$this->assertInstanceOf(Binary::class, $result['encryptedUnindexed']);
19621963
}
19631964

1964-
private function assertCursorCount($count, Cursor $cursor): void
1965+
private function assertCursorCount($count, CursorInterface $cursor): void
19651966
{
19661967
$this->assertCount($count, $cursor->toArray());
19671968
}

tests/Operation/WatchFunctionalTest.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use MongoDB\Codec\DecodeIfSupported;
1212
use MongoDB\Codec\DocumentCodec;
1313
use MongoDB\Codec\EncodeIfSupported;
14-
use MongoDB\Driver\Cursor;
14+
use MongoDB\Driver\CursorInterface;
1515
use MongoDB\Driver\Exception\CommandException;
1616
use MongoDB\Driver\Exception\ConnectionTimeoutException;
1717
use MongoDB\Driver\Exception\LogicException;
@@ -732,7 +732,7 @@ public function testInitialCursorIsNotClosed(): void
732732

733733
$cursor = $iterator->getInnerIterator();
734734

735-
$this->assertInstanceOf(Cursor::class, $cursor);
735+
$this->assertInstanceOf(CursorInterface::class, $cursor);
736736
$this->assertFalse($cursor->isDead());
737737
}
738738

@@ -1382,7 +1382,7 @@ public function testOriginalReadPreferenceIsPreservedOnResume(): void
13821382
ChangeStream::class,
13831383
);
13841384
$cursor = $getCursor();
1385-
assert($cursor instanceof Cursor);
1385+
assert($cursor instanceof CursorInterface);
13861386
self::assertTrue($cursor->getServer()->isSecondary());
13871387
}
13881388

tests/SpecTests/AtlasDataLakeSpecTest.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace MongoDB\Tests\SpecTests;
44

5-
use MongoDB\Driver\Cursor;
5+
use MongoDB\Driver\CursorInterface;
66
use MongoDB\Tests\CommandObserver;
77

88
use function current;
@@ -109,7 +109,7 @@ public function testConnectWithoutAuth(): void
109109
$client = static::createTestClient($uri);
110110
$cursor = $client->selectDatabase($this->getDatabaseName())->command(['ping' => 1]);
111111

112-
$this->assertInstanceOf(Cursor::class, $cursor);
112+
$this->assertInstanceOf(CursorInterface::class, $cursor);
113113
$this->assertCommandSucceeded(current($cursor->toArray()));
114114
}
115115

@@ -121,7 +121,7 @@ public function testConnectwithSCRAMSHA1(): void
121121
$client = static::createTestClient(null, ['authMechanism' => 'SCRAM-SHA-1']);
122122
$cursor = $client->selectDatabase($this->getDatabaseName())->command(['ping' => 1]);
123123

124-
$this->assertInstanceOf(Cursor::class, $cursor);
124+
$this->assertInstanceOf(CursorInterface::class, $cursor);
125125
$this->assertCommandSucceeded(current($cursor->toArray()));
126126
}
127127

@@ -133,7 +133,7 @@ public function testConnectwithSCRAMSHA256(): void
133133
$client = static::createTestClient(null, ['authMechanism' => 'SCRAM-SHA-256']);
134134
$cursor = $client->selectDatabase($this->getDatabaseName())->command(['ping' => 1]);
135135

136-
$this->assertInstanceOf(Cursor::class, $cursor);
136+
$this->assertInstanceOf(CursorInterface::class, $cursor);
137137
$this->assertCommandSucceeded(current($cursor->toArray()));
138138
}
139139
}

tests/SpecTests/Operation.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use MongoDB\Client;
77
use MongoDB\Collection;
88
use MongoDB\Database;
9-
use MongoDB\Driver\Cursor;
9+
use MongoDB\Driver\CursorInterface;
1010
use MongoDB\Driver\Exception\BulkWriteException;
1111
use MongoDB\Driver\Exception\Exception;
1212
use MongoDB\Driver\Server;
@@ -181,7 +181,7 @@ public function assert(FunctionalTestCase $test, Context $context, bool $bubbleE
181181
* exception to be thrown sooner and ensures that any expected
182182
* getMore command(s) can be observed even if a ResultExpectation
183183
* is not used (e.g. Command Monitoring spec). */
184-
if ($result instanceof Cursor) {
184+
if ($result instanceof CursorInterface) {
185185
$result = $result->toArray();
186186
} elseif ($result instanceof MapReduceResult) {
187187
/* For mapReduce operations, we ignore the mapReduce metadata

tests/UnifiedSpecTests/EntityMap.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use MongoDB\Collection;
99
use MongoDB\Database;
1010
use MongoDB\Driver\ClientEncryption;
11-
use MongoDB\Driver\Cursor;
11+
use MongoDB\Driver\CursorInterface;
1212
use MongoDB\Driver\Session;
1313
use MongoDB\GridFS\Bucket;
1414
use MongoDB\Tests\UnifiedSpecTests\Constraint\IsBsonType;
@@ -124,7 +124,7 @@ public function getRoot(): self
124124
*/
125125
public function closeCursor(string $cursorId): void
126126
{
127-
assertInstanceOf(Cursor::class, $this[$cursorId]);
127+
assertInstanceOf(CursorInterface::class, $this[$cursorId]);
128128
unset($this->map[$cursorId]);
129129
}
130130

@@ -170,7 +170,7 @@ private static function isSupportedType(): Constraint
170170
isInstanceOf(Session::class),
171171
isInstanceOf(Bucket::class),
172172
isInstanceOf(ChangeStream::class),
173-
isInstanceOf(Cursor::class),
173+
isInstanceOf(CursorInterface::class),
174174
IsBsonType::any(),
175175
);
176176

0 commit comments

Comments
 (0)