Skip to content

Commit 9cce39d

Browse files
authored
PHPC-2444: Remove support for string arguments in UTCDateTime constructor (#1662)
* PHPC-2444: Remove support for string arguments in UTCDateTime constructor * Consolidate tests for invalid values
1 parent 46c6055 commit 9cce39d

7 files changed

+24
-82
lines changed

UPGRADE-2.0.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,6 @@ UPGRADE FROM 1.x to 2.0
2727
removed. Use `--with-mongodb-system-libs` instead.
2828
* All classes that previously implemented the `Serializable` interface no
2929
longer implement this interface.
30+
* The constructor of `MongoDB\BSON\UTCDateTime` no longer accepts a `string`
31+
argument. To pass 64-bit integers on 32-bit platforms, use the
32+
`MongoDB\BSON\Int64` class instead.

src/BSON/UTCDateTime.c

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -218,15 +218,9 @@ static PHP_METHOD(MongoDB_BSON_UTCDateTime, __construct)
218218
case IS_DOUBLE:
219219
php_phongo_utcdatetime_init_from_double(intern, Z_DVAL_P(milliseconds));
220220
return;
221-
222-
case IS_STRING:
223-
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));
224-
225-
php_phongo_utcdatetime_init_from_string(intern, Z_STRVAL_P(milliseconds), Z_STRLEN_P(milliseconds));
226-
return;
227221
}
228222

229-
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT, "Expected integer or string, %s given", PHONGO_ZVAL_CLASS_OR_TYPE_NAME_P(milliseconds));
223+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT, "Expected integer or object, %s given", PHONGO_ZVAL_CLASS_OR_TYPE_NAME_P(milliseconds));
230224
}
231225

232226
static PHP_METHOD(MongoDB_BSON_UTCDateTime, __set_state)

src/BSON/UTCDateTime.stub.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

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

1414
final public function toDateTime(): \DateTime {}
1515

src/BSON/UTCDateTime_arginfo.h

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

tests/bson/bson-utcdatetime-008.phpt

Lines changed: 0 additions & 21 deletions
This file was deleted.

tests/bson/bson-utcdatetime_error-003.phpt

Lines changed: 0 additions & 45 deletions
This file was deleted.

tests/bson/bson-utcdatetime_error-004.phpt

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,31 @@ require_once __DIR__ . '/../utils/basic.inc';
88
/* UTCDateTime::__construct() internally converts floats to integers, so we will
99
* not use a float to test for an invalid value. We also don't test an object,
1010
* since that is used for validating a possible DateTimeInterface argument. */
11-
$invalidValues = [true, []];
11+
$invalidValues = [
12+
true,
13+
[],
14+
// Numeric strings are no longer supported as of 2.0
15+
'1416445411987',
16+
'1234.5678',
17+
];
1218

1319
foreach ($invalidValues as $invalidValue) {
14-
echo throws(function() use ($invalidValue) {
15-
new MongoDB\BSON\UTCDateTime($invalidValue);
16-
}, 'MongoDB\Driver\Exception\InvalidArgumentException'), "\n";
20+
echo throws(
21+
fn () => new MongoDB\BSON\UTCDateTime($invalidValue),
22+
MongoDB\Driver\Exception\InvalidArgumentException::class,
23+
), PHP_EOL;
1724
}
1825

1926
?>
2027
===DONE===
2128
<?php exit(0); ?>
2229
--EXPECT--
2330
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
24-
Expected integer or string, bool given
31+
Expected integer or object, bool given
2532
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
26-
Expected integer or string, array given
33+
Expected integer or object, array given
34+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
35+
Expected integer or object, string given
36+
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
37+
Expected integer or object, string given
2738
===DONE===

0 commit comments

Comments
 (0)