Skip to content

Commit 0cfecc2

Browse files
authored
PHPC-2412: Deprecate CursorId class (#1616)
* PHPC-2412: Deprecate CursorId class * Use zend_bool instead of bool * Remove fixed length assertion in test * Correctly set default parameter value * Remove int cast to avoid warnings on 32-bit platforms * Fix wrong expectation for CursorId debug format * Use new cursor ID logic in tests where possible * Remove deprecation comment from CursorId stub * Restore cursorid serialisation test * Restore original class initialisation order
1 parent 17624af commit 0cfecc2

14 files changed

+127
-29
lines changed

src/MongoDB/Cursor.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,22 @@ static PHP_METHOD(MongoDB_Driver_Cursor, toArray)
143143
static PHP_METHOD(MongoDB_Driver_Cursor, getId)
144144
{
145145
php_phongo_cursor_t* intern;
146+
zend_bool asInt64 = false;
146147

147148
intern = Z_CURSOR_OBJ_P(getThis());
148149

149-
PHONGO_PARSE_PARAMETERS_NONE();
150+
PHONGO_PARSE_PARAMETERS_START(0, 1)
151+
Z_PARAM_OPTIONAL
152+
Z_PARAM_BOOL(asInt64)
153+
PHONGO_PARSE_PARAMETERS_END();
150154

151-
php_phongo_cursor_id_new_from_id(return_value, mongoc_cursor_get_id(intern->cursor));
155+
if (asInt64) {
156+
phongo_int64_new(return_value, mongoc_cursor_get_id(intern->cursor));
157+
} else {
158+
php_error_docref(NULL, E_DEPRECATED, "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.");
159+
160+
php_phongo_cursor_id_new_from_id(return_value, mongoc_cursor_get_id(intern->cursor));
161+
}
152162
}
153163

154164
/* Returns the Server object to which this cursor is attached */

src/MongoDB/Cursor.stub.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,13 @@ public function current(): array|object|null {}
1818
public function current() {}
1919
#endif
2020

21-
final public function getId(): CursorId {}
21+
#if PHP_VERSION_ID >= 80000
22+
/** @tentative-return-type */
23+
final public function getId(bool $asInt64 = false): CursorId|\MongoDB\BSON\Int64 {}
24+
#else
25+
/** @return CursorId|\MongoDB\BSON\Int64 */
26+
final public function getId(bool $asInt64 = false) {}
27+
#endif
2228

2329
final public function getServer(): Server {}
2430

src/MongoDB/CursorInterface.stub.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,13 @@
99

1010
interface CursorInterface extends \Traversable
1111
{
12+
#if PHP_VERSION_ID >= 80000
1213
/** @tentative-return-type */
13-
public function getId(): CursorId;
14+
public function getId(): CursorId|\MongoDB\BSON\Int64;
15+
#else
16+
/** @return CursorId|\MongoDB\BSON\Int64 */
17+
public function getId();
18+
#endif
1419

1520
/** @tentative-return-type */
1621
public function getServer(): Server;

src/MongoDB/CursorInterface_arginfo.h

Lines changed: 18 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/MongoDB/Cursor_arginfo.h

Lines changed: 21 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/cursor/cursor-session-001.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ $iterator = new IteratorIterator($cursor);
2626
$iterator->rewind();
2727
$iterator->next();
2828

29-
printf("Cursor ID is zero: %s\n", (string) $cursor->getId() === '0' ? 'yes' : 'no');
29+
printf("Cursor ID is zero: %s\n", $cursor->getId(true) == 0 ? 'yes' : 'no');
3030
var_dump($cursor);
3131

3232
$iterator->next();
@@ -35,7 +35,7 @@ $iterator->next();
3535
* is exhausted. While this is primarily done to ensure implicit sessions for
3636
* command cursors are returned to the pool ASAP, it also applies to explicit
3737
* sessions. */
38-
printf("\nCursor ID is zero: %s\n", (string) $cursor->getId() === '0' ? 'yes' : 'no');
38+
printf("\nCursor ID is zero: %s\n", $cursor->getId(true) == 0 ? 'yes' : 'no');
3939
var_dump($cursor);
4040

4141
?>

tests/cursor/cursor-session-002.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ $iterator->next();
2828
/* Implicit sessions for query cursors are never exposed to PHPC, as they are
2929
* handled internally by libmongoc. Cursor debug ouput should never report such
3030
* sessions. */
31-
printf("Cursor ID is zero: %s\n", (string) $cursor->getId() === '0' ? 'yes' : 'no');
31+
printf("Cursor ID is zero: %s\n", $cursor->getId(true) == 0 ? 'yes' : 'no');
3232
var_dump($cursor);
3333

3434
$iterator->next();
3535

36-
printf("\nCursor ID is zero: %s\n", (string) $cursor->getId() === '0' ? 'yes' : 'no');
36+
printf("\nCursor ID is zero: %s\n", $cursor->getId(true) == 0 ? 'yes' : 'no');
3737
var_dump($cursor);
3838

3939
?>

tests/cursor/cursor-session-003.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ $iterator = new IteratorIterator($cursor);
3030
$iterator->rewind();
3131
$iterator->next();
3232

33-
printf("Cursor ID is zero: %s\n", (string) $cursor->getId() === '0' ? 'yes' : 'no');
33+
printf("Cursor ID is zero: %s\n", $cursor->getId(true) == 0 ? 'yes' : 'no');
3434
var_dump($cursor);
3535

3636
$iterator->next();
@@ -39,7 +39,7 @@ $iterator->next();
3939
* is exhausted. While this is primarily done to ensure implicit sessions for
4040
* command cursors are returned to the pool ASAP, it also applies to explicit
4141
* sessions. */
42-
printf("\nCursor ID is zero: %s\n", (string) $cursor->getId() === '0' ? 'yes' : 'no');
42+
printf("\nCursor ID is zero: %s\n", $cursor->getId(true) == 0 ? 'yes' : 'no');
4343
var_dump($cursor);
4444

4545
?>

tests/cursor/cursor-session-004.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ $iterator = new IteratorIterator($cursor);
2929
$iterator->rewind();
3030
$iterator->next();
3131

32-
printf("Cursor ID is zero: %s\n", (string) $cursor->getId() === '0' ? 'yes' : 'no');
32+
printf("Cursor ID is zero: %s\n", $cursor->getId(true) == 0 ? 'yes' : 'no');
3333
var_dump($cursor);
3434

3535
$iterator->next();
@@ -38,7 +38,7 @@ $iterator->next();
3838
* libmongoc, PHPC-1152 emulates its own implicit sessions for command cursors
3939
* in order to ensure that command cursors always share the same session as the
4040
* originating command. */
41-
printf("\nCursor ID is zero: %s\n", (string) $cursor->getId() === '0' ? 'yes' : 'no');
41+
printf("\nCursor ID is zero: %s\n", $cursor->getId(true) == 0 ? 'yes' : 'no');
4242
var_dump($cursor);
4343

4444
?>

tests/cursor/cursor-tailable_error-002.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ echo throws(function() use ($manager) {
5757
if ($numAwaitAttempts === 5) {
5858
$cursor->getServer()->executeCommand(DATABASE_NAME, new MongoDB\Driver\Command([
5959
'killCursors' => COLLECTION_NAME,
60-
'cursors' => [ $cursor->getId() ],
60+
'cursors' => [ $cursor->getId(true) ],
6161
]));
6262
}
6363

tests/cursor/cursorid-getId-001.phpt

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
--TEST--
2+
MongoDB\Driver\Cursor::getId
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
5+
<?php skip_if_not_live(); ?>
6+
<?php skip_if_not_clean(); ?>
7+
--FILE--
8+
<?php
9+
10+
require_once __DIR__ . "/../utils/basic.inc";
11+
12+
$manager = create_test_manager();
13+
14+
$bulk = new MongoDB\Driver\BulkWrite();
15+
$bulk->insert(['_id' => 1]);
16+
$bulk->insert(['_id' => 2]);
17+
$bulk->insert(['_id' => 3]);
18+
$manager->executeBulkWrite(NS, $bulk);
19+
20+
$cursor = $manager->executeQuery(NS, new MongoDB\Driver\Query([], ['batchSize' => 2]));
21+
22+
var_dump($cursor->getId());
23+
var_dump($cursor->getId(false));
24+
var_dump($cursor->getId(true));
25+
26+
?>
27+
===DONE===
28+
<?php exit(0); ?>
29+
--EXPECTF--
30+
Deprecated: 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. in %s
31+
object(MongoDB\Driver\CursorId)#%d (%d) {
32+
["id"]=>
33+
%rint\(%d\)|string\(%d\) "%d"%r
34+
}
35+
36+
Deprecated: 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. in %s
37+
object(MongoDB\Driver\CursorId)#%d (%d) {
38+
["id"]=>
39+
%rint\(%d\)|string\(%d\) "%d"%r
40+
}
41+
object(MongoDB\BSON\Int64)#%d (%d) {
42+
["integer"]=>
43+
string(%d) "%d"
44+
}
45+
===DONE===

tests/cursorid/cursorid-001.phpt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ hex_dump(fromPHP(['cid' => $cursorId]));
2626
===DONE===
2727
<?php exit(0); ?>
2828
--EXPECTF--
29+
Deprecated: 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. in %s
2930
0 : 12 00 00 00 12 63 69 64 00 %x %x %x %x %x %x %x [.....cid.%s]
3031
10 : %x 00 [%s.]
3132
===DONE===

tests/cursorid/cursorid-002.phpt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ printf("Killed expected cursor: %s\n", (string) $cursorId === (string) $result->
3737
?>
3838
===DONE===
3939
<?php exit(0); ?>
40-
--EXPECT--
40+
--EXPECTF--
41+
Deprecated: 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. in %s
4142
Killed 1 cursor(s)
4243
Killed expected cursor: yes
4344
===DONE===

tests/functional/cursorid-001.phpt

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,17 @@ $query = new MongoDB\Driver\Query(array(), array(
2020

2121
$cursor = $manager->executeQuery(NS, $query);
2222

23-
$cursorid = $cursor->getId();
24-
$s1 = (string)$cursorid;
25-
var_dump(
26-
$cursorid,
27-
$s1
28-
);
29-
var_dump($s1 > 0);
23+
$cursorid = $cursor->getId(true);
24+
var_dump($cursorid);
25+
var_dump($cursorid != 0);
3026

3127
?>
3228
===DONE===
3329
<?php exit(0); ?>
3430
--EXPECTF--
35-
object(MongoDB\Driver\CursorId)#%d (%d) {
36-
["id"]=>
37-
%rint\(\d+\)|string\(\d+\) "\d+"%r
31+
object(MongoDB\BSON\Int64)#%d (%d) {
32+
["integer"]=>
33+
string(%d) "%d"
3834
}
39-
string(%d) "%d"
4035
bool(true)
4136
===DONE===

0 commit comments

Comments
 (0)