Skip to content

Commit 9df0785

Browse files
authored
PHPC-2101: Default Binary constructor parameter to TYPE_GENERIC (#1362)
1 parent 584766c commit 9df0785

File tree

5 files changed

+25
-8
lines changed

5 files changed

+25
-8
lines changed

src/BSON/Binary.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,13 @@ static PHP_METHOD(MongoDB_BSON_Binary, __construct)
9797
php_phongo_binary_t* intern;
9898
char* data;
9999
size_t data_len;
100-
zend_long type;
100+
zend_long type = BSON_SUBTYPE_BINARY;
101101

102102
intern = Z_BINARY_OBJ_P(getThis());
103103

104-
PHONGO_PARSE_PARAMETERS_START(2, 2)
104+
PHONGO_PARSE_PARAMETERS_START(1, 2)
105105
Z_PARAM_STRING(data, data_len)
106+
Z_PARAM_OPTIONAL
106107
Z_PARAM_LONG(type)
107108
PHONGO_PARSE_PARAMETERS_END();
108109

src/BSON/Binary.stub.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ final class Binary implements BinaryInterface, \JsonSerializable, Type, \Seriali
6363
*/
6464
public const TYPE_USER_DEFINED = UNKNOWN;
6565

66-
public final function __construct(string $data, int $type) {}
66+
public final function __construct(string $data, int $type = Binary::TYPE_GENERIC) {}
6767

6868
final public function getData(): string {}
6969

src/BSON/Binary_arginfo.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/* This is a generated file, edit the .stub.php file instead.
2-
* Stub hash: 7b9fd21a206d97f242415b2b2f0fc1ca847524ca */
2+
* Stub hash: 5a6e8f17139479d3183b22dc9e13fd81939b8df9 */
33

4-
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_MongoDB_BSON_Binary___construct, 0, 0, 2)
4+
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_MongoDB_BSON_Binary___construct, 0, 0, 1)
55
ZEND_ARG_TYPE_INFO(0, data, IS_STRING, 0)
6-
ZEND_ARG_TYPE_INFO(0, type, IS_LONG, 0)
6+
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, type, IS_LONG, 0, "MongoDB\\BSON\\Binary::TYPE_GENERIC")
77
ZEND_END_ARG_INFO()
88

99
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_MongoDB_BSON_Binary_getData, 0, 0, IS_STRING, 0)

tests/bson/bson-binary-002.phpt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
--TEST--
2+
MongoDB\BSON\Binary::__construct() defaults to TYPE_GENERIC
3+
--FILE--
4+
<?php
5+
6+
$binaryWithDefaultType = new MongoDB\BSON\Binary('randomBinaryString');
7+
var_dump($binaryWithDefaultType->getData() === 'randomBinaryString');
8+
var_dump($binaryWithDefaultType->getType() === MongoDB\BSON\Binary::TYPE_GENERIC);
9+
10+
?>
11+
===DONE===
12+
<?php exit(0); ?>
13+
--EXPECT--
14+
bool(true)
15+
bool(true)
16+
===DONE===

tests/bson/bson-binary_error-001.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ echo throws(function() use ($binary) {
1919
}, MongoDB\Driver\Exception\InvalidArgumentException::class), "\n";
2020

2121
echo throws(function() {
22-
new MongoDB\BSON\Binary("random binary data without type");
22+
new MongoDB\BSON\Binary();
2323
}, MongoDB\Driver\Exception\InvalidArgumentException::class), "\n";
2424

2525
?>
@@ -31,6 +31,6 @@ MongoDB\BSON\Binary::getData() expects exactly 0 %r(argument|parameter)%rs, 1 gi
3131
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
3232
MongoDB\BSON\Binary::getType() expects exactly 0 %r(argument|parameter)%rs, 1 given
3333
OK: Got MongoDB\Driver\Exception\InvalidArgumentException
34-
MongoDB\BSON\Binary::__construct() expects exactly 2 %r(argument|parameter)%rs, 1 given
34+
MongoDB\BSON\Binary::__construct() expects at least 1 %r(argument|parameter)%r, 0 given
3535
===DONE===
3636

0 commit comments

Comments
 (0)