Skip to content

PHPC-2459: Remove support for float arg in UTCDateTime ctor #1709

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions UPGRADE-2.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ 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
* The constructor of `MongoDB\BSON\UTCDateTime` no longer accepts a `string` or
`float` argument. To pass 64-bit integers on 32-bit platforms, use the
`MongoDB\BSON\Int64` class instead.
* The `--with-openssl-dir` configure option has been removed. If using OpenSSL,
ensure that it is detected by `pkg-config`.
Expand Down
16 changes: 0 additions & 16 deletions src/BSON/UTCDateTime.c
Original file line number Diff line number Diff line change
Expand Up @@ -121,16 +121,6 @@ static bool php_phongo_utcdatetime_init_from_object(php_phongo_utcdatetime_t* in
return false;
}

static bool php_phongo_utcdatetime_init_from_double(php_phongo_utcdatetime_t* intern, double milliseconds)
{
char tmp[24];
int tmp_len;

tmp_len = snprintf(tmp, sizeof(tmp), "%.0f", milliseconds > 0 ? floor(milliseconds) : ceil(milliseconds));

return php_phongo_utcdatetime_init_from_string(intern, tmp, tmp_len);
}

static HashTable* php_phongo_utcdatetime_get_properties_hash(zend_object* object, bool is_temp)
{
php_phongo_utcdatetime_t* intern;
Expand Down Expand Up @@ -214,12 +204,6 @@ static PHP_METHOD(MongoDB_BSON_UTCDateTime, __construct)
case IS_LONG:
php_phongo_utcdatetime_init(intern, Z_LVAL_P(milliseconds));
return;

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

php_phongo_utcdatetime_init_from_double(intern, Z_DVAL_P(milliseconds));
return;
}

phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT, "Expected integer or object, %s given", PHONGO_ZVAL_CLASS_OR_TYPE_NAME_P(milliseconds));
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|float|\DateTimeInterface|Int64|null $milliseconds = null) {}
final public function __construct(int|\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.

37 changes: 0 additions & 37 deletions tests/bson/bson-utcdatetime-007.phpt

This file was deleted.

18 changes: 0 additions & 18 deletions tests/bson/bson-utcdatetime_error-001.phpt

This file was deleted.

13 changes: 9 additions & 4 deletions tests/bson/bson-utcdatetime_error-004.phpt
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
--TEST--
MongoDB\BSON\UTCDateTime constructor requires integer or string argument
MongoDB\BSON\UTCDateTime constructor type validation
--FILE--
<?php

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

/* UTCDateTime::__construct() internally converts floats to integers, so we will
* not use a float to test for an invalid value. We also don't test an object,
* since that is used for validating a possible DateTimeInterface argument. */
$invalidValues = [
true,
[],
// Numeric strings are no longer supported as of 2.0
'1416445411987',
'1234.5678',
// Floats are no longer supported as of 2.0
1234.5678,
// Only DateTimeInterface or MongoDB\BSON\Int64 is accepted
new stdClass,
];

foreach ($invalidValues as $invalidValue) {
Expand All @@ -35,4 +36,8 @@ OK: Got MongoDB\Driver\Exception\InvalidArgumentException
Expected integer or object, string given
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
Expected integer or object, string given
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
Expected integer or object, float given
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
Expected instance of DateTimeInterface or MongoDB\BSON\Int64, stdClass given
===DONE===