From 0cfecc251d8c1a3dfb4794dc01cb229ecf356e7e Mon Sep 17 00:00:00 2001 From: Andreas Braun Date: Thu, 5 Sep 2024 16:21:04 +0200 Subject: [PATCH] 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 --- src/MongoDB/Cursor.c | 14 ++++++- src/MongoDB/Cursor.stub.php | 8 +++- src/MongoDB/CursorInterface.stub.php | 7 +++- src/MongoDB/CursorInterface_arginfo.h | 20 ++++++++- src/MongoDB/Cursor_arginfo.h | 23 ++++++++++- tests/cursor/cursor-session-001.phpt | 4 +- tests/cursor/cursor-session-002.phpt | 4 +- tests/cursor/cursor-session-003.phpt | 4 +- tests/cursor/cursor-session-004.phpt | 4 +- tests/cursor/cursor-tailable_error-002.phpt | 2 +- tests/cursor/cursorid-getId-001.phpt | 45 +++++++++++++++++++++ tests/cursorid/cursorid-001.phpt | 1 + tests/cursorid/cursorid-002.phpt | 3 +- tests/functional/cursorid-001.phpt | 17 +++----- 14 files changed, 127 insertions(+), 29 deletions(-) create mode 100644 tests/cursor/cursorid-getId-001.phpt diff --git a/src/MongoDB/Cursor.c b/src/MongoDB/Cursor.c index 96bf61fea..a48e27ce7 100644 --- a/src/MongoDB/Cursor.c +++ b/src/MongoDB/Cursor.c @@ -143,12 +143,22 @@ static PHP_METHOD(MongoDB_Driver_Cursor, toArray) static PHP_METHOD(MongoDB_Driver_Cursor, getId) { php_phongo_cursor_t* intern; + zend_bool asInt64 = false; intern = Z_CURSOR_OBJ_P(getThis()); - PHONGO_PARSE_PARAMETERS_NONE(); + PHONGO_PARSE_PARAMETERS_START(0, 1) + Z_PARAM_OPTIONAL + Z_PARAM_BOOL(asInt64) + PHONGO_PARSE_PARAMETERS_END(); - php_phongo_cursor_id_new_from_id(return_value, mongoc_cursor_get_id(intern->cursor)); + if (asInt64) { + phongo_int64_new(return_value, mongoc_cursor_get_id(intern->cursor)); + } else { + 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."); + + php_phongo_cursor_id_new_from_id(return_value, mongoc_cursor_get_id(intern->cursor)); + } } /* Returns the Server object to which this cursor is attached */ diff --git a/src/MongoDB/Cursor.stub.php b/src/MongoDB/Cursor.stub.php index b0007046c..252ab65b8 100644 --- a/src/MongoDB/Cursor.stub.php +++ b/src/MongoDB/Cursor.stub.php @@ -18,7 +18,13 @@ public function current(): array|object|null {} public function current() {} #endif - final public function getId(): CursorId {} +#if PHP_VERSION_ID >= 80000 + /** @tentative-return-type */ + final public function getId(bool $asInt64 = false): CursorId|\MongoDB\BSON\Int64 {} +#else + /** @return CursorId|\MongoDB\BSON\Int64 */ + final public function getId(bool $asInt64 = false) {} +#endif final public function getServer(): Server {} diff --git a/src/MongoDB/CursorInterface.stub.php b/src/MongoDB/CursorInterface.stub.php index 112555e8b..1501659f3 100644 --- a/src/MongoDB/CursorInterface.stub.php +++ b/src/MongoDB/CursorInterface.stub.php @@ -9,8 +9,13 @@ interface CursorInterface extends \Traversable { +#if PHP_VERSION_ID >= 80000 /** @tentative-return-type */ - public function getId(): CursorId; + public function getId(): CursorId|\MongoDB\BSON\Int64; +#else + /** @return CursorId|\MongoDB\BSON\Int64 */ + public function getId(); +#endif /** @tentative-return-type */ public function getServer(): Server; diff --git a/src/MongoDB/CursorInterface_arginfo.h b/src/MongoDB/CursorInterface_arginfo.h index 5ebecdae3..2bf1cb51c 100644 --- a/src/MongoDB/CursorInterface_arginfo.h +++ b/src/MongoDB/CursorInterface_arginfo.h @@ -1,8 +1,15 @@ /* This is a generated file, edit the .stub.php file instead. - * Stub hash: 001f88edb710125420178d78e291fffc10bbfa1b */ + * Stub hash: 191d790b04cda72e9949774fad866f7750de5dfa */ -ZEND_BEGIN_ARG_WITH_TENTATIVE_RETURN_OBJ_INFO_EX(arginfo_class_MongoDB_Driver_CursorInterface_getId, 0, 0, MongoDB\\Driver\\CursorId, 0) +#if PHP_VERSION_ID >= 80000 +ZEND_BEGIN_ARG_WITH_TENTATIVE_RETURN_OBJ_TYPE_MASK_EX(arginfo_class_MongoDB_Driver_CursorInterface_getId, 0, 0, MongoDB\\Driver\\CursorId|MongoDB\\BSON\\Int64, 0) ZEND_END_ARG_INFO() +#endif + +#if !(PHP_VERSION_ID >= 80000) +ZEND_BEGIN_ARG_INFO_EX(arginfo_class_MongoDB_Driver_CursorInterface_getId, 0, 0, 0) +ZEND_END_ARG_INFO() +#endif ZEND_BEGIN_ARG_WITH_TENTATIVE_RETURN_OBJ_INFO_EX(arginfo_class_MongoDB_Driver_CursorInterface_getServer, 0, 0, MongoDB\\Driver\\Server, 0) ZEND_END_ARG_INFO() @@ -18,10 +25,19 @@ ZEND_BEGIN_ARG_WITH_TENTATIVE_RETURN_TYPE_INFO_EX(arginfo_class_MongoDB_Driver_C ZEND_END_ARG_INFO() +#if PHP_VERSION_ID >= 80000 +#endif +#if !(PHP_VERSION_ID >= 80000) +#endif static const zend_function_entry class_MongoDB_Driver_CursorInterface_methods[] = { +#if PHP_VERSION_ID >= 80000 + ZEND_ABSTRACT_ME_WITH_FLAGS(MongoDB_Driver_CursorInterface, getId, arginfo_class_MongoDB_Driver_CursorInterface_getId, ZEND_ACC_PUBLIC|ZEND_ACC_ABSTRACT) +#endif +#if !(PHP_VERSION_ID >= 80000) ZEND_ABSTRACT_ME_WITH_FLAGS(MongoDB_Driver_CursorInterface, getId, arginfo_class_MongoDB_Driver_CursorInterface_getId, ZEND_ACC_PUBLIC|ZEND_ACC_ABSTRACT) +#endif ZEND_ABSTRACT_ME_WITH_FLAGS(MongoDB_Driver_CursorInterface, getServer, arginfo_class_MongoDB_Driver_CursorInterface_getServer, ZEND_ACC_PUBLIC|ZEND_ACC_ABSTRACT) ZEND_ABSTRACT_ME_WITH_FLAGS(MongoDB_Driver_CursorInterface, isDead, arginfo_class_MongoDB_Driver_CursorInterface_isDead, ZEND_ACC_PUBLIC|ZEND_ACC_ABSTRACT) ZEND_ABSTRACT_ME_WITH_FLAGS(MongoDB_Driver_CursorInterface, setTypeMap, arginfo_class_MongoDB_Driver_CursorInterface_setTypeMap, ZEND_ACC_PUBLIC|ZEND_ACC_ABSTRACT) diff --git a/src/MongoDB/Cursor_arginfo.h b/src/MongoDB/Cursor_arginfo.h index f8c14b9c9..1874a4592 100644 --- a/src/MongoDB/Cursor_arginfo.h +++ b/src/MongoDB/Cursor_arginfo.h @@ -1,5 +1,5 @@ /* This is a generated file, edit the .stub.php file instead. - * Stub hash: b94c509926df512855559d2c0ab4263b9d7dec63 */ + * Stub hash: e5e70f4036534c5aff1d55106e776e0230de53a6 */ ZEND_BEGIN_ARG_INFO_EX(arginfo_class_MongoDB_Driver_Cursor___construct, 0, 0, 0) ZEND_END_ARG_INFO() @@ -14,8 +14,17 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_class_MongoDB_Driver_Cursor_current, 0, 0, 0) ZEND_END_ARG_INFO() #endif -ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_class_MongoDB_Driver_Cursor_getId, 0, 0, MongoDB\\Driver\\CursorId, 0) +#if PHP_VERSION_ID >= 80000 +ZEND_BEGIN_ARG_WITH_TENTATIVE_RETURN_OBJ_TYPE_MASK_EX(arginfo_class_MongoDB_Driver_Cursor_getId, 0, 0, MongoDB\\Driver\\CursorId|MongoDB\\BSON\\Int64, 0) + ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, asInt64, _IS_BOOL, 0, "false") +ZEND_END_ARG_INFO() +#endif + +#if !(PHP_VERSION_ID >= 80000) +ZEND_BEGIN_ARG_INFO_EX(arginfo_class_MongoDB_Driver_Cursor_getId, 0, 0, 0) + ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, asInt64, _IS_BOOL, 0, "false") ZEND_END_ARG_INFO() +#endif ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_class_MongoDB_Driver_Cursor_getServer, 0, 0, MongoDB\\Driver\\Server, 0) ZEND_END_ARG_INFO() @@ -48,7 +57,12 @@ static ZEND_METHOD(MongoDB_Driver_Cursor, current); #if !(PHP_VERSION_ID >= 80000) static ZEND_METHOD(MongoDB_Driver_Cursor, current); #endif +#if PHP_VERSION_ID >= 80000 +static ZEND_METHOD(MongoDB_Driver_Cursor, getId); +#endif +#if !(PHP_VERSION_ID >= 80000) static ZEND_METHOD(MongoDB_Driver_Cursor, getId); +#endif static ZEND_METHOD(MongoDB_Driver_Cursor, getServer); static ZEND_METHOD(MongoDB_Driver_Cursor, isDead); static ZEND_METHOD(MongoDB_Driver_Cursor, key); @@ -67,7 +81,12 @@ static const zend_function_entry class_MongoDB_Driver_Cursor_methods[] = { #if !(PHP_VERSION_ID >= 80000) ZEND_ME(MongoDB_Driver_Cursor, current, arginfo_class_MongoDB_Driver_Cursor_current, ZEND_ACC_PUBLIC) #endif +#if PHP_VERSION_ID >= 80000 + ZEND_ME(MongoDB_Driver_Cursor, getId, arginfo_class_MongoDB_Driver_Cursor_getId, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL) +#endif +#if !(PHP_VERSION_ID >= 80000) ZEND_ME(MongoDB_Driver_Cursor, getId, arginfo_class_MongoDB_Driver_Cursor_getId, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL) +#endif ZEND_ME(MongoDB_Driver_Cursor, getServer, arginfo_class_MongoDB_Driver_Cursor_getServer, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL) ZEND_ME(MongoDB_Driver_Cursor, isDead, arginfo_class_MongoDB_Driver_Cursor_isDead, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL) ZEND_ME(MongoDB_Driver_Cursor, key, arginfo_class_MongoDB_Driver_Cursor_key, ZEND_ACC_PUBLIC) diff --git a/tests/cursor/cursor-session-001.phpt b/tests/cursor/cursor-session-001.phpt index ade67b842..e5f0ef66f 100644 --- a/tests/cursor/cursor-session-001.phpt +++ b/tests/cursor/cursor-session-001.phpt @@ -26,7 +26,7 @@ $iterator = new IteratorIterator($cursor); $iterator->rewind(); $iterator->next(); -printf("Cursor ID is zero: %s\n", (string) $cursor->getId() === '0' ? 'yes' : 'no'); +printf("Cursor ID is zero: %s\n", $cursor->getId(true) == 0 ? 'yes' : 'no'); var_dump($cursor); $iterator->next(); @@ -35,7 +35,7 @@ $iterator->next(); * is exhausted. While this is primarily done to ensure implicit sessions for * command cursors are returned to the pool ASAP, it also applies to explicit * sessions. */ -printf("\nCursor ID is zero: %s\n", (string) $cursor->getId() === '0' ? 'yes' : 'no'); +printf("\nCursor ID is zero: %s\n", $cursor->getId(true) == 0 ? 'yes' : 'no'); var_dump($cursor); ?> diff --git a/tests/cursor/cursor-session-002.phpt b/tests/cursor/cursor-session-002.phpt index 4332f7c12..b8827c2c4 100644 --- a/tests/cursor/cursor-session-002.phpt +++ b/tests/cursor/cursor-session-002.phpt @@ -28,12 +28,12 @@ $iterator->next(); /* Implicit sessions for query cursors are never exposed to PHPC, as they are * handled internally by libmongoc. Cursor debug ouput should never report such * sessions. */ -printf("Cursor ID is zero: %s\n", (string) $cursor->getId() === '0' ? 'yes' : 'no'); +printf("Cursor ID is zero: %s\n", $cursor->getId(true) == 0 ? 'yes' : 'no'); var_dump($cursor); $iterator->next(); -printf("\nCursor ID is zero: %s\n", (string) $cursor->getId() === '0' ? 'yes' : 'no'); +printf("\nCursor ID is zero: %s\n", $cursor->getId(true) == 0 ? 'yes' : 'no'); var_dump($cursor); ?> diff --git a/tests/cursor/cursor-session-003.phpt b/tests/cursor/cursor-session-003.phpt index df9f7d46e..2eabbe616 100644 --- a/tests/cursor/cursor-session-003.phpt +++ b/tests/cursor/cursor-session-003.phpt @@ -30,7 +30,7 @@ $iterator = new IteratorIterator($cursor); $iterator->rewind(); $iterator->next(); -printf("Cursor ID is zero: %s\n", (string) $cursor->getId() === '0' ? 'yes' : 'no'); +printf("Cursor ID is zero: %s\n", $cursor->getId(true) == 0 ? 'yes' : 'no'); var_dump($cursor); $iterator->next(); @@ -39,7 +39,7 @@ $iterator->next(); * is exhausted. While this is primarily done to ensure implicit sessions for * command cursors are returned to the pool ASAP, it also applies to explicit * sessions. */ -printf("\nCursor ID is zero: %s\n", (string) $cursor->getId() === '0' ? 'yes' : 'no'); +printf("\nCursor ID is zero: %s\n", $cursor->getId(true) == 0 ? 'yes' : 'no'); var_dump($cursor); ?> diff --git a/tests/cursor/cursor-session-004.phpt b/tests/cursor/cursor-session-004.phpt index b93bbbf08..1fc23ffef 100644 --- a/tests/cursor/cursor-session-004.phpt +++ b/tests/cursor/cursor-session-004.phpt @@ -29,7 +29,7 @@ $iterator = new IteratorIterator($cursor); $iterator->rewind(); $iterator->next(); -printf("Cursor ID is zero: %s\n", (string) $cursor->getId() === '0' ? 'yes' : 'no'); +printf("Cursor ID is zero: %s\n", $cursor->getId(true) == 0 ? 'yes' : 'no'); var_dump($cursor); $iterator->next(); @@ -38,7 +38,7 @@ $iterator->next(); * libmongoc, PHPC-1152 emulates its own implicit sessions for command cursors * in order to ensure that command cursors always share the same session as the * originating command. */ -printf("\nCursor ID is zero: %s\n", (string) $cursor->getId() === '0' ? 'yes' : 'no'); +printf("\nCursor ID is zero: %s\n", $cursor->getId(true) == 0 ? 'yes' : 'no'); var_dump($cursor); ?> diff --git a/tests/cursor/cursor-tailable_error-002.phpt b/tests/cursor/cursor-tailable_error-002.phpt index 9157c5e2d..dec2d52c6 100644 --- a/tests/cursor/cursor-tailable_error-002.phpt +++ b/tests/cursor/cursor-tailable_error-002.phpt @@ -57,7 +57,7 @@ echo throws(function() use ($manager) { if ($numAwaitAttempts === 5) { $cursor->getServer()->executeCommand(DATABASE_NAME, new MongoDB\Driver\Command([ 'killCursors' => COLLECTION_NAME, - 'cursors' => [ $cursor->getId() ], + 'cursors' => [ $cursor->getId(true) ], ])); } diff --git a/tests/cursor/cursorid-getId-001.phpt b/tests/cursor/cursorid-getId-001.phpt new file mode 100644 index 000000000..fd168d8ee --- /dev/null +++ b/tests/cursor/cursorid-getId-001.phpt @@ -0,0 +1,45 @@ +--TEST-- +MongoDB\Driver\Cursor::getId +--SKIPIF-- + + + +--FILE-- +insert(['_id' => 1]); +$bulk->insert(['_id' => 2]); +$bulk->insert(['_id' => 3]); +$manager->executeBulkWrite(NS, $bulk); + +$cursor = $manager->executeQuery(NS, new MongoDB\Driver\Query([], ['batchSize' => 2])); + +var_dump($cursor->getId()); +var_dump($cursor->getId(false)); +var_dump($cursor->getId(true)); + +?> +===DONE=== + +--EXPECTF-- +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 +object(MongoDB\Driver\CursorId)#%d (%d) { + ["id"]=> + %rint\(%d\)|string\(%d\) "%d"%r +} + +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 +object(MongoDB\Driver\CursorId)#%d (%d) { + ["id"]=> + %rint\(%d\)|string\(%d\) "%d"%r +} +object(MongoDB\BSON\Int64)#%d (%d) { + ["integer"]=> + string(%d) "%d" +} +===DONE=== diff --git a/tests/cursorid/cursorid-001.phpt b/tests/cursorid/cursorid-001.phpt index 34cfbbc3e..6cf970223 100644 --- a/tests/cursorid/cursorid-001.phpt +++ b/tests/cursorid/cursorid-001.phpt @@ -26,6 +26,7 @@ hex_dump(fromPHP(['cid' => $cursorId])); ===DONE=== --EXPECTF-- +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 0 : 12 00 00 00 12 63 69 64 00 %x %x %x %x %x %x %x [.....cid.%s] 10 : %x 00 [%s.] ===DONE=== diff --git a/tests/cursorid/cursorid-002.phpt b/tests/cursorid/cursorid-002.phpt index 23c053894..5809fd218 100644 --- a/tests/cursorid/cursorid-002.phpt +++ b/tests/cursorid/cursorid-002.phpt @@ -37,7 +37,8 @@ printf("Killed expected cursor: %s\n", (string) $cursorId === (string) $result-> ?> ===DONE=== ---EXPECT-- +--EXPECTF-- +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 Killed 1 cursor(s) Killed expected cursor: yes ===DONE=== diff --git a/tests/functional/cursorid-001.phpt b/tests/functional/cursorid-001.phpt index 026b20c2a..ec9742437 100644 --- a/tests/functional/cursorid-001.phpt +++ b/tests/functional/cursorid-001.phpt @@ -20,22 +20,17 @@ $query = new MongoDB\Driver\Query(array(), array( $cursor = $manager->executeQuery(NS, $query); -$cursorid = $cursor->getId(); -$s1 = (string)$cursorid; -var_dump( - $cursorid, - $s1 -); -var_dump($s1 > 0); +$cursorid = $cursor->getId(true); +var_dump($cursorid); +var_dump($cursorid != 0); ?> ===DONE=== --EXPECTF-- -object(MongoDB\Driver\CursorId)#%d (%d) { - ["id"]=> - %rint\(\d+\)|string\(\d+\) "\d+"%r +object(MongoDB\BSON\Int64)#%d (%d) { + ["integer"]=> + string(%d) "%d" } -string(%d) "%d" bool(true) ===DONE===