Skip to content

Commit

Permalink
PHPC-2444: Remove support for string arguments in UTCDateTime constru…
Browse files Browse the repository at this point in the history
…ctor
  • Loading branch information
alcaeus committed Sep 20, 2024
1 parent 46c6055 commit 7cbea84
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 33 deletions.
3 changes: 3 additions & 0 deletions UPGRADE-2.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,6 @@ UPGRADE FROM 1.x to 2.0
removed. Use `--with-mongodb-system-libs` instead.
* All classes that previously implemented the `Serializable` interface no
longer implement this interface.
* The constructor of `MongoDB\BSON\UTCDateTime` no longer accepts a `string`
argument. To pass 64-bit integers on 32-bit platforms, use the
`MongoDB\BSON\Int64` class instead.
8 changes: 1 addition & 7 deletions src/BSON/UTCDateTime.c
Original file line number Diff line number Diff line change
Expand Up @@ -218,15 +218,9 @@ static PHP_METHOD(MongoDB_BSON_UTCDateTime, __construct)
case IS_DOUBLE:
php_phongo_utcdatetime_init_from_double(intern, Z_DVAL_P(milliseconds));
return;

case IS_STRING:
php_error_docref(NULL, E_DEPRECATED, "Creating a %s instance with a string is deprecated and will be removed in ext-mongodb 2.0", ZSTR_VAL(php_phongo_utcdatetime_ce->name));

php_phongo_utcdatetime_init_from_string(intern, Z_STRVAL_P(milliseconds), Z_STRLEN_P(milliseconds));
return;
}

phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT, "Expected integer or string, %s given", PHONGO_ZVAL_CLASS_OR_TYPE_NAME_P(milliseconds));
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT, "Expected integer or object, %s given", PHONGO_ZVAL_CLASS_OR_TYPE_NAME_P(milliseconds));
}

static PHP_METHOD(MongoDB_BSON_UTCDateTime, __set_state)
Expand Down
2 changes: 1 addition & 1 deletion src/BSON/UTCDateTime.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

final class UTCDateTime implements UTCDateTimeInterface, \JsonSerializable, Type, \Stringable
{
final public function __construct(int|string|float|\DateTimeInterface|Int64|null $milliseconds = null) {}
final public function __construct(int|float|\DateTimeInterface|Int64|null $milliseconds = null) {}

final public function toDateTime(): \DateTime {}

Expand Down
4 changes: 2 additions & 2 deletions src/BSON/UTCDateTime_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 4 additions & 9 deletions tests/bson/bson-utcdatetime-008.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,12 @@ MongoDB\BSON\UTCDateTime construction from 64-bit integer as string

require_once __DIR__ . '/../utils/basic.inc';

$utcdatetime = new MongoDB\BSON\UTCDateTime('1416445411987');

var_dump($utcdatetime);
echo throws(fn () => new MongoDB\BSON\UTCDateTime('1416445411987'), MongoDB\Driver\Exception\InvalidArgumentException::class), PHP_EOL;

?>
===DONE===
<?php exit(0); ?>
--EXPECTF--
Deprecated: MongoDB\BSON\UTCDateTime::__construct(): Creating a MongoDB\BSON\UTCDateTime instance with a string is deprecated and will be removed in ext-mongodb 2.0 in %s
object(MongoDB\BSON\UTCDateTime)#%d (%d) {
["milliseconds"]=>
string(13) "1416445411987"
}
--EXPECT--
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
Expected integer or object, string given
===DONE===
17 changes: 5 additions & 12 deletions tests/bson/bson-utcdatetime_error-003.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,13 @@ echo throws(function() {
?>
===DONE===
<?php exit(0); ?>
--EXPECTF--
Deprecated: MongoDB\BSON\UTCDateTime::__construct(): Creating a MongoDB\BSON\UTCDateTime instance with a string is deprecated and will be removed in ext-mongodb 2.0 in %s
--EXPECT--
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
Error parsing "1234.5678" as 64-bit integer for MongoDB\BSON\UTCDateTime initialization

Deprecated: MongoDB\BSON\UTCDateTime::__construct(): Creating a MongoDB\BSON\UTCDateTime instance with a string is deprecated and will be removed in ext-mongodb 2.0 in %s
Expected integer or object, string given
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
Error parsing "9223372036854775808" as 64-bit integer for MongoDB\BSON\UTCDateTime initialization

Deprecated: MongoDB\BSON\UTCDateTime::__construct(): Creating a MongoDB\BSON\UTCDateTime instance with a string is deprecated and will be removed in ext-mongodb 2.0 in %s
Expected integer or object, string given
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
Error parsing "-9223372036854775809" as 64-bit integer for MongoDB\BSON\UTCDateTime initialization

Deprecated: MongoDB\BSON\UTCDateTime::__construct(): Creating a MongoDB\BSON\UTCDateTime instance with a string is deprecated and will be removed in ext-mongodb 2.0 in %s
Expected integer or object, string given
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
Error parsing "18446744073709551615" as 64-bit integer for MongoDB\BSON\UTCDateTime initialization
Expected integer or object, string given
===DONE===
4 changes: 2 additions & 2 deletions tests/bson/bson-utcdatetime_error-004.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ foreach ($invalidValues as $invalidValue) {
<?php exit(0); ?>
--EXPECT--
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
Expected integer or string, bool given
Expected integer or object, bool given
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
Expected integer or string, array given
Expected integer or object, array given
===DONE===

0 comments on commit 7cbea84

Please sign in to comment.