Skip to content

Commit d20de7e

Browse files
authored
PHPC-1021: Remove support for ReadPreference integer modes (#1666)
* PHPC-1021: Remove support for ReadPreference integer modes This removes the deprecated integer constants, constructor support, and getMode(). Error-checking in php_phongo_readpreference_get_mode_string() has also been removed. ReadPreference only uses mongoc_read_prefs_new() internally, and we should not need to worry about libmongoc feeding us mongoc_read_prefs_t with an invalid mode. * Add upgrade note * Remove obsolete test
1 parent 2566ca1 commit d20de7e

10 files changed

+30
-409
lines changed

UPGRADE-2.0.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ UPGRADE FROM 1.x to 2.0
99
* The constants `MongoDB\Driver\ClientEncryption::ALGORITHM_RANGE_PREVIEW` and
1010
`MongoDB\Driver\ClientEncryption::QUERY_TYPE_RANGE_PREVIEW` have been
1111
removed. Use the `ALGORITHM_RANGE` and `QUERY_TYPE_RANGE` instead.
12+
* The `MongoDB\Driver\ReadPreference` class now requires a string value for its
13+
constructor's `$mode` parameter. The integer constants for modes have been
14+
removed along with the `getMode()` method. Use the string constants and
15+
`getModeString()` instead.
1216
* All tentative return types defined in interface and non-final classes are now
1317
fixed and are required in implementing or extending classes.
1418
* `MongoDB\Driver\CursorInterface` now extends `Iterator`, requiring

src/MongoDB/ReadPreference.c

Lines changed: 23 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,9 @@ static bool php_phongo_readpreference_init_from_hash(php_phongo_readpreference_t
153153
return false;
154154
}
155155

156-
static const char* php_phongo_readpreference_get_mode_string(mongoc_read_mode_t mode)
156+
static const char* php_phongo_readpreference_get_mode_string(const mongoc_read_prefs_t* read_prefs)
157157
{
158-
switch (mode) {
158+
switch (mongoc_read_prefs_get_mode(read_prefs)) {
159159
case MONGOC_READ_PRIMARY:
160160
return PHONGO_READ_PRIMARY;
161161
case MONGOC_READ_PRIMARY_PREFERRED:
@@ -167,19 +167,15 @@ static const char* php_phongo_readpreference_get_mode_string(mongoc_read_mode_t
167167
case MONGOC_READ_NEAREST:
168168
return PHONGO_READ_NEAREST;
169169
default:
170-
/* Should never happen, but if it does: exception */
171-
phongo_throw_exception(PHONGO_ERROR_LOGIC, "Mode '%d' should never have been passed to php_phongo_readpreference_get_mode_string, please file a bug report", mode);
172-
break;
170+
return "";
173171
}
174-
175-
return NULL;
176172
}
177173

178174
/* Constructs a new ReadPreference */
179175
static PHP_METHOD(MongoDB_Driver_ReadPreference, __construct)
180176
{
181177
php_phongo_readpreference_t* intern;
182-
zval* mode;
178+
zend_string* mode;
183179
zval* tagSets = NULL;
184180
zval* options = NULL;
185181

@@ -188,44 +184,24 @@ static PHP_METHOD(MongoDB_Driver_ReadPreference, __construct)
188184
/* Separate the tagSets zval, since we may end up modifying it in
189185
* php_phongo_read_preference_prep_tagsets() below. */
190186
PHONGO_PARSE_PARAMETERS_START(1, 3)
191-
Z_PARAM_ZVAL(mode)
187+
Z_PARAM_STR(mode)
192188
Z_PARAM_OPTIONAL
193189
Z_PARAM_ARRAY_EX(tagSets, 1, 1)
194190
Z_PARAM_ARRAY_OR_NULL(options)
195191
PHONGO_PARSE_PARAMETERS_END();
196192

197-
if (Z_TYPE_P(mode) == IS_LONG) {
198-
php_error_docref(NULL, E_DEPRECATED, "Passing an integer mode to \"MongoDB\\Driver\\ReadPreference::__construct\" is deprecated and will be removed in a future release.");
199-
200-
switch (Z_LVAL_P(mode)) {
201-
case MONGOC_READ_PRIMARY:
202-
case MONGOC_READ_SECONDARY:
203-
case MONGOC_READ_PRIMARY_PREFERRED:
204-
case MONGOC_READ_SECONDARY_PREFERRED:
205-
case MONGOC_READ_NEAREST:
206-
intern->read_preference = mongoc_read_prefs_new(Z_LVAL_P(mode));
207-
break;
208-
default:
209-
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT, "Invalid mode: %" PHONGO_LONG_FORMAT, Z_LVAL_P(mode));
210-
return;
211-
}
212-
} else if (Z_TYPE_P(mode) == IS_STRING) {
213-
if (strcasecmp(Z_STRVAL_P(mode), PHONGO_READ_PRIMARY) == 0) {
214-
intern->read_preference = mongoc_read_prefs_new(MONGOC_READ_PRIMARY);
215-
} else if (strcasecmp(Z_STRVAL_P(mode), PHONGO_READ_PRIMARY_PREFERRED) == 0) {
216-
intern->read_preference = mongoc_read_prefs_new(MONGOC_READ_PRIMARY_PREFERRED);
217-
} else if (strcasecmp(Z_STRVAL_P(mode), PHONGO_READ_SECONDARY) == 0) {
218-
intern->read_preference = mongoc_read_prefs_new(MONGOC_READ_SECONDARY);
219-
} else if (strcasecmp(Z_STRVAL_P(mode), PHONGO_READ_SECONDARY_PREFERRED) == 0) {
220-
intern->read_preference = mongoc_read_prefs_new(MONGOC_READ_SECONDARY_PREFERRED);
221-
} else if (strcasecmp(Z_STRVAL_P(mode), PHONGO_READ_NEAREST) == 0) {
222-
intern->read_preference = mongoc_read_prefs_new(MONGOC_READ_NEAREST);
223-
} else {
224-
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT, "Invalid mode: '%s'", Z_STRVAL_P(mode));
225-
return;
226-
}
193+
if (zend_string_equals_literal_ci(mode, PHONGO_READ_PRIMARY)) {
194+
intern->read_preference = mongoc_read_prefs_new(MONGOC_READ_PRIMARY);
195+
} else if (zend_string_equals_literal_ci(mode, PHONGO_READ_PRIMARY_PREFERRED)) {
196+
intern->read_preference = mongoc_read_prefs_new(MONGOC_READ_PRIMARY_PREFERRED);
197+
} else if (zend_string_equals_literal_ci(mode, PHONGO_READ_SECONDARY)) {
198+
intern->read_preference = mongoc_read_prefs_new(MONGOC_READ_SECONDARY);
199+
} else if (zend_string_equals_literal_ci(mode, PHONGO_READ_SECONDARY_PREFERRED)) {
200+
intern->read_preference = mongoc_read_prefs_new(MONGOC_READ_SECONDARY_PREFERRED);
201+
} else if (zend_string_equals_literal_ci(mode, PHONGO_READ_NEAREST)) {
202+
intern->read_preference = mongoc_read_prefs_new(MONGOC_READ_NEAREST);
227203
} else {
228-
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT, "Expected mode to be integer or string, %s given", PHONGO_ZVAL_CLASS_OR_TYPE_NAME_P(mode));
204+
phongo_throw_exception(PHONGO_ERROR_INVALID_ARGUMENT, "Invalid mode: '%s'", ZSTR_VAL(mode));
229205
return;
230206
}
231207

@@ -366,35 +342,16 @@ static PHP_METHOD(MongoDB_Driver_ReadPreference, getMaxStalenessSeconds)
366342
RETURN_LONG(mongoc_read_prefs_get_max_staleness_seconds(intern->read_preference));
367343
}
368344

369-
/* Returns the ReadPreference mode */
370-
static PHP_METHOD(MongoDB_Driver_ReadPreference, getMode)
371-
{
372-
php_phongo_readpreference_t* intern;
373-
374-
intern = Z_READPREFERENCE_OBJ_P(getThis());
375-
376-
PHONGO_PARSE_PARAMETERS_NONE();
377-
378-
RETURN_LONG(mongoc_read_prefs_get_mode(intern->read_preference));
379-
}
380-
381345
/* Returns the ReadPreference mode as string */
382346
static PHP_METHOD(MongoDB_Driver_ReadPreference, getModeString)
383347
{
384348
php_phongo_readpreference_t* intern;
385-
const char* mode_string;
386349

387350
intern = Z_READPREFERENCE_OBJ_P(getThis());
388351

389352
PHONGO_PARSE_PARAMETERS_NONE();
390353

391-
mode_string = php_phongo_readpreference_get_mode_string(mongoc_read_prefs_get_mode(intern->read_preference));
392-
if (!mode_string) {
393-
/* Exception already thrown */
394-
return;
395-
}
396-
397-
RETURN_STRING(mode_string);
354+
RETURN_STRING(php_phongo_readpreference_get_mode_string(intern->read_preference));
398355
}
399356

400357
/* Returns the ReadPreference tag sets */
@@ -429,10 +386,8 @@ static HashTable* php_phongo_readpreference_get_properties_hash(zend_object* obj
429386
{
430387
php_phongo_readpreference_t* intern;
431388
HashTable* props;
432-
const char* modeString = NULL;
433389
const bson_t* tags;
434390
const bson_t* hedge;
435-
mongoc_read_mode_t mode;
436391

437392
intern = Z_OBJ_READPREFERENCE(object);
438393

@@ -442,15 +397,13 @@ static HashTable* php_phongo_readpreference_get_properties_hash(zend_object* obj
442397
return props;
443398
}
444399

445-
tags = mongoc_read_prefs_get_tags(intern->read_preference);
446-
mode = mongoc_read_prefs_get_mode(intern->read_preference);
447-
modeString = php_phongo_readpreference_get_mode_string(mode);
448-
hedge = mongoc_read_prefs_get_hedge(intern->read_preference);
400+
tags = mongoc_read_prefs_get_tags(intern->read_preference);
401+
hedge = mongoc_read_prefs_get_hedge(intern->read_preference);
449402

450-
if (modeString) {
403+
{
451404
zval z_mode;
452405

453-
ZVAL_STRING(&z_mode, modeString);
406+
ZVAL_STRING(&z_mode, php_phongo_readpreference_get_mode_string(intern->read_preference));
454407
zend_hash_str_update(props, "mode", sizeof("mode") - 1, &z_mode);
455408
}
456409

@@ -510,12 +463,10 @@ static PHP_METHOD(MongoDB_Driver_ReadPreference, serialize)
510463
php_phongo_readpreference_t* intern;
511464
zval retval;
512465
php_serialize_data_t var_hash;
513-
smart_str buf = { 0 };
514-
const char* modeString = NULL;
466+
smart_str buf = { 0 };
515467
const bson_t* tags;
516468
const bson_t* hedge;
517469
int64_t maxStalenessSeconds;
518-
mongoc_read_mode_t mode;
519470

520471
intern = Z_READPREFERENCE_OBJ_P(getThis());
521472

@@ -526,16 +477,12 @@ static PHP_METHOD(MongoDB_Driver_ReadPreference, serialize)
526477
}
527478

528479
tags = mongoc_read_prefs_get_tags(intern->read_preference);
529-
mode = mongoc_read_prefs_get_mode(intern->read_preference);
530-
modeString = php_phongo_readpreference_get_mode_string(mode);
531480
maxStalenessSeconds = mongoc_read_prefs_get_max_staleness_seconds(intern->read_preference);
532481
hedge = mongoc_read_prefs_get_hedge(intern->read_preference);
533482

534483
array_init_size(&retval, 4);
535484

536-
if (modeString) {
537-
ADD_ASSOC_STRING(&retval, "mode", modeString);
538-
}
485+
ADD_ASSOC_STRING(&retval, "mode", php_phongo_readpreference_get_mode_string(intern->read_preference));
539486

540487
if (!bson_empty0(tags)) {
541488
php_phongo_bson_state state;

src/MongoDB/ReadPreference.stub.php

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -9,41 +9,6 @@
99

1010
final class ReadPreference implements \MongoDB\BSON\Serializable, \Serializable
1111
{
12-
/**
13-
* @var int
14-
* @cvalue MONGOC_READ_PRIMARY
15-
* @deprecated
16-
*/
17-
public const RP_PRIMARY = UNKNOWN;
18-
19-
/**
20-
* @var int
21-
* @cvalue MONGOC_READ_PRIMARY_PREFERRED
22-
* @deprecated
23-
*/
24-
public const RP_PRIMARY_PREFERRED = UNKNOWN;
25-
26-
/**
27-
* @var int
28-
* @cvalue MONGOC_READ_SECONDARY
29-
* @deprecated
30-
*/
31-
public const RP_SECONDARY = UNKNOWN;
32-
33-
/**
34-
* @var int
35-
* @cvalue MONGOC_READ_SECONDARY_PREFERRED
36-
* @deprecated
37-
*/
38-
public const RP_SECONDARY_PREFERRED = UNKNOWN;
39-
40-
/**
41-
* @var int
42-
* @cvalue MONGOC_READ_NEAREST
43-
* @deprecated
44-
*/
45-
public const RP_NEAREST = UNKNOWN;
46-
4712
/**
4813
* @var string
4914
* @cvalue PHONGO_READ_PRIMARY
@@ -86,15 +51,12 @@ final class ReadPreference implements \MongoDB\BSON\Serializable, \Serializable
8651
*/
8752
public const SMALLEST_MAX_STALENESS_SECONDS = UNKNOWN;
8853

89-
final public function __construct(string|int $mode, ?array $tagSets = null, ?array $options = null) {}
54+
final public function __construct(string $mode, ?array $tagSets = null, ?array $options = null) {}
9055

9156
final public function getHedge(): ?object {}
9257

9358
final public function getMaxStalenessSeconds(): int {}
9459

95-
/** @deprecated */
96-
final public function getMode(): int {}
97-
9860
final public function getModeString(): string {}
9961

10062
final public function getTagSets(): array {}

src/MongoDB/ReadPreference_arginfo.h

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

tests/readPreference/readpreference-constants-002.phpt

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

0 commit comments

Comments
 (0)