Skip to content

Commit bb4bb56

Browse files
committed
Remove CursorInterface::getId compatibility layer
1 parent 00fa5e4 commit bb4bb56

File tree

4 files changed

+21
-142
lines changed

4 files changed

+21
-142
lines changed

src/ChangeStream.php

Lines changed: 6 additions & 31 deletions
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\CursorId;
2524
use MongoDB\Driver\Exception\ConnectionException;
2625
use MongoDB\Driver\Exception\RuntimeException;
2726
use MongoDB\Driver\Exception\ServerException;
@@ -33,10 +32,6 @@
3332
use function assert;
3433
use function call_user_func;
3534
use function in_array;
36-
use function sprintf;
37-
use function trigger_error;
38-
39-
use const E_USER_DEPRECATED;
4035

4136
/**
4237
* Iterator for a change stream.
@@ -88,12 +83,8 @@ class ChangeStream implements Iterator
8883
*/
8984
private bool $hasAdvanced = false;
9085

91-
/**
92-
* @see https://php.net/iterator.current
93-
* @return array|object|null
94-
*/
95-
#[ReturnTypeWillChange]
96-
public function current()
86+
/** @see https://php.net/iterator.current */
87+
public function current(): array|object|null
9788
{
9889
$value = $this->iterator->current();
9990

@@ -106,26 +97,9 @@ public function current()
10697
return $this->codec->decode($value);
10798
}
10899

109-
/**
110-
* @return CursorId|Int64
111-
* @psalm-return ($asInt64 is true ? Int64 : CursorId)
112-
*/
113-
#[ReturnTypeWillChange]
114-
public function getCursorId(bool $asInt64 = false)
100+
public function getCursorId(): Int64
115101
{
116-
if (! $asInt64) {
117-
@trigger_error(
118-
sprintf(
119-
'The method "%s" will no longer return a "%s" instance in the future. Pass "true" as argument to change to the new behavior and receive a "%s" instance instead.',
120-
__METHOD__,
121-
CursorId::class,
122-
Int64::class,
123-
),
124-
E_USER_DEPRECATED,
125-
);
126-
}
127-
128-
return $this->iterator->getInnerIterator()->getId($asInt64);
102+
return $this->iterator->getInnerIterator()->getId();
129103
}
130104

131105
/**
@@ -255,7 +229,8 @@ private function onIteration(bool $incrementKey): void
255229
* have been received in the last response. Therefore, we can unset the
256230
* resumeCallable. This will free any reference to Watch as well as the
257231
* only reference to any implicit session created therein. */
258-
if ((string) $this->getCursorId(true) === '0') {
232+
// Use a type-unsafe comparison to compare with Int64 instances
233+
if ($this->getCursorId() == 0) {
259234
$this->resumeCallable = null;
260235
}
261236

src/Model/ChangeStreamIterator.php

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
namespace MongoDB\Model;
1919

20-
use Iterator;
2120
use IteratorIterator;
2221
use MongoDB\BSON\Document;
2322
use MongoDB\BSON\Serializable;
@@ -49,7 +48,7 @@
4948
*
5049
* @internal
5150
* @template TValue of array|object
52-
* @template-extends IteratorIterator<int, TValue, CursorInterface<int, TValue>&Iterator<int, TValue>>
51+
* @template-extends IteratorIterator<int, TValue, CursorInterface<TValue>>
5352
*/
5453
final class ChangeStreamIterator extends IteratorIterator implements CommandSubscriber
5554
{
@@ -77,18 +76,17 @@ public function current()
7776
}
7877

7978
/**
80-
* Necessary to let psalm know that we're always expecting a cursor as inner
81-
* iterator. This could be side-stepped due to the class not being final,
82-
* but it's very much an invalid use-case. This method can be dropped in 2.0
83-
* once the class is final.
79+
* This method is necessary as psalm does not properly set the return type
80+
* of IteratorIterator::getInnerIterator to the templated iterator
8481
*
85-
* @return CursorInterface<int, TValue>&Iterator<int, TValue>
82+
* @see https://github.com/vimeo/psalm/pull/11100.
83+
*
84+
* @return CursorInterface<TValue>
8685
*/
87-
final public function getInnerIterator(): Iterator
86+
public function getInnerIterator(): CursorInterface
8887
{
8988
$cursor = parent::getInnerIterator();
9089
assert($cursor instanceof CursorInterface);
91-
assert($cursor instanceof Iterator);
9290

9391
return $cursor;
9492
}
@@ -173,18 +171,10 @@ public function valid(): bool
173171

174172
/**
175173
* @internal
176-
* @psalm-param CursorInterface<int, TValue>&Iterator<int, TValue> $cursor
174+
* @psalm-param CursorInterface<TValue> $cursor
177175
*/
178176
public function __construct(CursorInterface $cursor, int $firstBatchSize, array|object|null $initialResumeToken, private ?object $postBatchResumeToken = null)
179177
{
180-
if (! $cursor instanceof Iterator) {
181-
throw InvalidArgumentException::invalidType(
182-
'$cursor',
183-
$cursor,
184-
CursorInterface::class . '&' . Iterator::class,
185-
);
186-
}
187-
188178
if (isset($initialResumeToken) && ! is_document($initialResumeToken)) {
189179
throw InvalidArgumentException::expectedDocumentType('$initialResumeToken', $initialResumeToken);
190180
}

src/Model/CodecCursor.php

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,30 +17,24 @@
1717

1818
namespace MongoDB\Model;
1919

20-
use Iterator;
2120
use MongoDB\BSON\Document;
2221
use MongoDB\BSON\Int64;
2322
use MongoDB\Codec\DocumentCodec;
24-
use MongoDB\Driver\Cursor;
25-
use MongoDB\Driver\CursorId;
2623
use MongoDB\Driver\CursorInterface;
2724
use MongoDB\Driver\Server;
28-
use ReturnTypeWillChange;
2925

3026
use function assert;
3127
use function iterator_to_array;
3228
use function sprintf;
3329
use function trigger_error;
3430

35-
use const E_USER_DEPRECATED;
3631
use const E_USER_WARNING;
3732

3833
/**
3934
* @template TValue of object
40-
* @template-implements CursorInterface<int, TValue>
41-
* @template-implements Iterator<int, TValue>
35+
* @template-implements CursorInterface<TValue>
4236
*/
43-
class CodecCursor implements CursorInterface, Iterator
37+
class CodecCursor implements CursorInterface
4438
{
4539
private const TYPEMAP = ['root' => 'bson'];
4640

@@ -64,33 +58,16 @@ public function current(): ?object
6458
* @param DocumentCodec<NativeClass> $codec
6559
* @return self<NativeClass>
6660
*/
67-
public static function fromCursor(Cursor $cursor, DocumentCodec $codec): self
61+
public static function fromCursor(CursorInterface $cursor, DocumentCodec $codec): self
6862
{
6963
$cursor->setTypeMap(self::TYPEMAP);
7064

7165
return new self($cursor, $codec);
7266
}
7367

74-
/**
75-
* @return CursorId|Int64
76-
* @psalm-return ($asInt64 is true ? Int64 : CursorId)
77-
*/
78-
#[ReturnTypeWillChange]
79-
public function getId(bool $asInt64 = false)
68+
public function getId(): Int64
8069
{
81-
if (! $asInt64) {
82-
@trigger_error(
83-
sprintf(
84-
'The method "%s" will no longer return a "%s" instance in the future. Pass "true" as argument to change to the new behavior and receive a "%s" instance instead.',
85-
__METHOD__,
86-
CursorId::class,
87-
Int64::class,
88-
),
89-
E_USER_DEPRECATED,
90-
);
91-
}
92-
93-
return $this->cursor->getId($asInt64);
70+
return $this->cursor->getId();
9471
}
9572

9673
public function getServer(): Server
@@ -138,7 +115,7 @@ public function valid(): bool
138115
}
139116

140117
/** @param DocumentCodec<TValue> $codec */
141-
private function __construct(private Cursor $cursor, private DocumentCodec $codec)
118+
private function __construct(private CursorInterface $cursor, private DocumentCodec $codec)
142119
{
143120
}
144121
}

tests/Model/CodecCursorFunctionalTest.php

Lines changed: 1 addition & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,9 @@
44

55
use MongoDB\BSON\Int64;
66
use MongoDB\Codec\DocumentCodec;
7-
use MongoDB\Driver\CursorId;
87
use MongoDB\Model\CodecCursor;
98
use MongoDB\Tests\FunctionalTestCase;
109

11-
use function restore_error_handler;
12-
use function set_error_handler;
13-
14-
use const E_DEPRECATED;
15-
use const E_USER_DEPRECATED;
16-
1710
class CodecCursorFunctionalTest extends FunctionalTestCase
1811
{
1912
public function setUp(): void
@@ -36,69 +29,13 @@ public function testSetTypeMap(): void
3629
$codecCursor->setTypeMap(['root' => 'array']);
3730
}
3831

39-
public function testGetIdReturnTypeWithoutArgument(): void
40-
{
41-
$collection = self::createTestClient()->selectCollection($this->getDatabaseName(), $this->getCollectionName());
42-
$cursor = $collection->find();
43-
44-
$codecCursor = CodecCursor::fromCursor($cursor, $this->createMock(DocumentCodec::class));
45-
46-
$deprecations = [];
47-
48-
try {
49-
$previousErrorHandler = set_error_handler(
50-
function (...$args) use (&$previousErrorHandler, &$deprecations) {
51-
$deprecations[] = $args;
52-
53-
return true;
54-
},
55-
E_USER_DEPRECATED | E_DEPRECATED,
56-
);
57-
58-
$cursorId = $codecCursor->getId();
59-
} finally {
60-
restore_error_handler();
61-
}
62-
63-
self::assertInstanceOf(CursorId::class, $cursorId);
64-
65-
// Expect 2 deprecations: 1 from CodecCursor, one from Cursor
66-
self::assertCount(2, $deprecations);
67-
self::assertSame(
68-
'The method "MongoDB\Model\CodecCursor::getId" will no longer return a "MongoDB\Driver\CursorId" instance in the future. Pass "true" as argument to change to the new behavior and receive a "MongoDB\BSON\Int64" instance instead.',
69-
$deprecations[0][1],
70-
);
71-
self::assertSame(
72-
'MongoDB\Driver\Cursor::getId(): The method "MongoDB\Driver\Cursor::getId" will no longer return a "MongoDB\Driver\CursorId" instance in the future. Pass "true" as argument to change to the new behavior and receive a "MongoDB\BSON\Int64" instance instead.',
73-
$deprecations[1][1],
74-
);
75-
}
76-
7732
public function testGetIdReturnTypeWithArgument(): void
7833
{
7934
$collection = self::createTestClient()->selectCollection($this->getDatabaseName(), $this->getCollectionName());
8035
$cursor = $collection->find();
8136

8237
$codecCursor = CodecCursor::fromCursor($cursor, $this->createMock(DocumentCodec::class));
8338

84-
$deprecations = [];
85-
86-
try {
87-
$previousErrorHandler = set_error_handler(
88-
function (...$args) use (&$previousErrorHandler, &$deprecations) {
89-
$deprecations[] = $args;
90-
91-
return true;
92-
},
93-
E_USER_DEPRECATED | E_DEPRECATED,
94-
);
95-
96-
$cursorId = $codecCursor->getId(true);
97-
} finally {
98-
restore_error_handler();
99-
}
100-
101-
self::assertInstanceOf(Int64::class, $cursorId);
102-
self::assertCount(0, $deprecations);
39+
self::assertInstanceOf(Int64::class, $codecCursor->getId());
10340
}
10441
}

0 commit comments

Comments
 (0)