Skip to content

Commit 6887226

Browse files
authored
PHPC-1709: Add typing information to arginfo (#1337)
* Add missing macros for declaring arguments * Add macros to define disabled constructor and __wakeup * Define arginfo via stubs for BSON classes * Declare tentative return types in interfaces * Add correct return type for __set_state methods * Generate class entries for BSON classes * Declare Binary class constants in stub file * Use stubs in exception classes * Add stubs for monitoring classes * Fix wrong JsonSerializable class in BSON stubs * Disable declaration-after-statement This is necessary to let the generated arginfo files compile * Use stubs for driver classes * Fix deprecation messages in tests * Add missing macro * Fix tests relying on value injection * Parse parameters in disabled constructor/wakup function * Add note about arginfo files to contribution docs * Add GitHub action to check generated arginfo files * PHPC-2115: Use DateTimeInterface in UTCDateTime constructor signature * Don't install mongodb extension in GitHub actions workflows * Change indentation in stub files * Use individual #if conditions for each stub method * Make disabled constructor/wakeup methods static * Fix bulkWrite parameter name * Use %d to match property count in tests * Ensure all classes with disabled serialisation declare __wakeup
1 parent e2f9144 commit 6887226

File tree

390 files changed

+8164
-3086
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

390 files changed

+8164
-3086
lines changed

.github/workflows/arginfo-files.yml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
name: "Check generated arginfo files"
2+
3+
on:
4+
pull_request:
5+
branches:
6+
- "v*.*"
7+
- "master"
8+
- "feature/*"
9+
push:
10+
branches:
11+
- "v*.*"
12+
- "master"
13+
- "feature/*"
14+
15+
jobs:
16+
check-arginfo:
17+
name: "Check generated arginfo files"
18+
runs-on: "ubuntu-20.04"
19+
20+
strategy:
21+
matrix:
22+
php-version:
23+
- "8.2"
24+
25+
steps:
26+
- name: "Checkout"
27+
uses: "actions/checkout@v2"
28+
with:
29+
submodules: true
30+
31+
- name: "Install PHP"
32+
uses: "shivammathur/setup-php@v2"
33+
with:
34+
php-version: "${{ matrix.php-version }}"
35+
tools: "phpize"
36+
37+
- name: "Run phpize"
38+
run: phpize
39+
40+
- name: "Rebuild arginfo files from stubs"
41+
run: "php ./build/gen_stub.php"
42+
43+
- name: "Check arginfo file diff"
44+
run: git add . -N && git diff --exit-code

.github/workflows/clang-format.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ jobs:
3232
uses: "shivammathur/setup-php@v2"
3333
with:
3434
php-version: "${{ matrix.php-version }}"
35-
extensions: ":mongodb"
3635
tools: "phpize"
3736

3837
- name: "Configure driver"

.github/workflows/tests.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ jobs:
6565
uses: "shivammathur/setup-php@v2"
6666
with:
6767
php-version: "${{ matrix.php-version }}"
68-
extensions: ":mongodb"
6968
tools: "phpize"
7069

7170
- name: "Configure driver"

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ mongocryptd.pid
4141
*.out
4242
*.mem
4343
*.php
44+
!*.stub.php
4445
tests/*/*.sh
4546
tests/*/*/*/*.sh
4647
tests/*/*/*/*/*.sh

CONTRIBUTING.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ phpize > /dev/null && \
2121
make clean > /dev/null && make all > /dev/null && make install
2222
```
2323

24+
## Generating arginfo from stub files
25+
26+
Arginfo structures are generated from stub files using the `gen_stub.php`
27+
file. Note that this requires `phpize` to be run for PHP 8.2 to make use
28+
of all features. After changing a stub file, run `./build/gen_stub.php`
29+
to regenerate the corresponding arginfo files and commit the results.
30+
2431
## Testing
2532

2633
The extension's test use the PHPT format from PHP internals. This format is

config.m4

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ if test "$PHP_MONGODB" != "no"; then
6666
AX_CHECK_COMPILE_FLAG(-Wenum-compare, _MAINTAINER_CFLAGS="$_MAINTAINER_CFLAGS -Wenum-compare" ,, -Werror)
6767
AX_CHECK_COMPILE_FLAG(-Wempty-body, _MAINTAINER_CFLAGS="$_MAINTAINER_CFLAGS -Wempty-body" ,, -Werror)
6868
AX_CHECK_COMPILE_FLAG(-Wparentheses, _MAINTAINER_CFLAGS="$_MAINTAINER_CFLAGS -Wparentheses" ,, -Werror)
69-
AX_CHECK_COMPILE_FLAG(-Wdeclaration-after-statement, _MAINTAINER_CFLAGS="$_MAINTAINER_CFLAGS -Wdeclaration-after-statement" ,, -Werror)
7069
AX_CHECK_COMPILE_FLAG(-Wmaybe-uninitialized, _MAINTAINER_CFLAGS="$_MAINTAINER_CFLAGS -Wmaybe-uninitialized" ,, -Werror)
7170
AX_CHECK_COMPILE_FLAG(-Wimplicit-fallthrough, _MAINTAINER_CFLAGS="$_MAINTAINER_CFLAGS -Wimplicit-fallthrough" ,, -Werror)
7271
AX_CHECK_COMPILE_FLAG(-Werror, _MAINTAINER_CFLAGS="$_MAINTAINER_CFLAGS -Werror" ,, -Werror)

php_phongo.c

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ ZEND_TSRMLS_CACHE_DEFINE();
4444
static int32_t phongo_num_threads = 0;
4545

4646
/* Declare zend_class_entry dependencies, which are initialized in MINIT */
47-
zend_class_entry* php_phongo_date_immutable_ce;
4847
zend_class_entry* php_phongo_json_serializable_ce;
4948

5049
/* {{{ phongo_std_object_handlers */
@@ -181,17 +180,12 @@ PHP_MINIT_FUNCTION(mongodb) /* {{{ */
181180
phongo_std_object_handlers.get_gc = php_phongo_std_get_gc;
182181

183182
/* Initialize zend_class_entry dependencies.
184-
*
185-
* Although DateTimeImmutable was introduced in PHP 5.5.0,
186-
* php_date_get_immutable_ce() is not available in PHP versions before
187-
* 5.5.24 and 5.6.8.
188183
*
189184
* Although JsonSerializable was introduced in PHP 5.4.0,
190185
* php_json_serializable_ce is not exported in PHP versions before 5.4.26
191186
* and 5.5.10. For later PHP versions, looking up the class manually also
192187
* helps with distros that disable LTDL_LAZY for dlopen() (e.g. Fedora).
193188
*/
194-
php_phongo_date_immutable_ce = php_phongo_fetch_internal_class(ZEND_STRL("datetimeimmutable"));
195189
php_phongo_json_serializable_ce = php_phongo_fetch_internal_class(ZEND_STRL("jsonserializable"));
196190

197191
if (php_phongo_json_serializable_ce == NULL) {

php_phongo.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,20 @@ zend_object_handlers* phongo_get_std_object_handlers(void);
109109
(intern)->created_by_pid = (int) getpid(); \
110110
} while (0)
111111

112+
#define PHONGO_DISABLED_CONSTRUCTOR(classname) \
113+
static PHP_METHOD(classname, __construct) \
114+
{ \
115+
PHONGO_PARSE_PARAMETERS_NONE(); \
116+
phongo_throw_exception(PHONGO_ERROR_RUNTIME, "Accessing private constructor"); \
117+
}
118+
119+
#define PHONGO_DISABLED_WAKEUP(classname) \
120+
static PHP_METHOD(classname, __wakeup) \
121+
{ \
122+
PHONGO_PARSE_PARAMETERS_NONE(); \
123+
phongo_throw_exception(PHONGO_ERROR_RUNTIME, "MongoDB\\Driver objects cannot be serialized"); \
124+
}
125+
112126
/* Shared function entries for disabling constructors and unserialize() */
113127
PHP_FUNCTION(MongoDB_disabled___construct);
114128
PHP_FUNCTION(MongoDB_disabled___wakeup);

scripts/clang-format.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
CLANG_ARGS="-Werror"
44

55
if test x"$1" = xchanged; then
6-
FILES1=`git ls-files | grep -v "src/contrib" | grep '\.[ch]$'`
7-
FILES2=`git ls-files --others --exclude-standard | grep -v "src/contrib" | grep '\.[ch]$'`
6+
FILES1=`git ls-files | grep -v "src/contrib" | grep -v "_arginfo.h" | grep '\.[ch]$'`
7+
FILES2=`git ls-files --others --exclude-standard | grep -v "src/contrib" | grep -v "_arginfo.h" | grep '\.[ch]$'`
88
FILES="$FILES1 $FILES2"
99
else
10-
FILES1=`git ls-files | grep -v "src/contrib" | grep '\.[ch]$'`
11-
FILES2=`git ls-files --others --exclude-standard | grep -v "src/contrib" | grep '\.[ch]$'`
10+
FILES1=`git ls-files | grep -v "src/contrib" | grep -v "_arginfo.h" | grep '\.[ch]$'`
11+
FILES2=`git ls-files --others --exclude-standard | grep -v "src/contrib" | grep -v "_arginfo.h" | grep '\.[ch]$'`
1212
FILES="$FILES1 $FILES2"
1313
fi
1414

src/BSON/Binary.c

Lines changed: 12 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
#include "php_phongo.h"
2424
#include "phongo_error.h"
25+
#include "Binary_arginfo.h"
2526

2627
#define PHONGO_BINARY_UUID_SIZE 16
2728

@@ -92,7 +93,7 @@ static HashTable* php_phongo_binary_get_properties_hash(phongo_compat_object_han
9293

9394
/* {{{ proto void MongoDB\BSON\Binary::__construct(string $data, int $type)
9495
Construct a new BSON binary type */
95-
static PHP_METHOD(Binary, __construct)
96+
static PHP_METHOD(MongoDB_BSON_Binary, __construct)
9697
{
9798
php_phongo_binary_t* intern;
9899
char* data;
@@ -111,7 +112,7 @@ static PHP_METHOD(Binary, __construct)
111112

112113
/* {{{ proto MongoDB\BSON\Binary MongoDB\BSON\Binary::__set_state(array $properties)
113114
*/
114-
static PHP_METHOD(Binary, __set_state)
115+
static PHP_METHOD(MongoDB_BSON_Binary, __set_state)
115116
{
116117
php_phongo_binary_t* intern;
117118
HashTable* props;
@@ -131,7 +132,7 @@ static PHP_METHOD(Binary, __set_state)
131132

132133
/* {{{ proto string MongoDB\BSON\Binary::__toString()
133134
Return the Binary's data string. */
134-
static PHP_METHOD(Binary, __toString)
135+
static PHP_METHOD(MongoDB_BSON_Binary, __toString)
135136
{
136137
php_phongo_binary_t* intern;
137138

@@ -144,7 +145,7 @@ static PHP_METHOD(Binary, __toString)
144145

145146
/* {{{ proto string MongoDB\BSON\Binary::getData()
146147
*/
147-
static PHP_METHOD(Binary, getData)
148+
static PHP_METHOD(MongoDB_BSON_Binary, getData)
148149
{
149150
php_phongo_binary_t* intern;
150151

@@ -157,7 +158,7 @@ static PHP_METHOD(Binary, getData)
157158

158159
/* {{{ proto integer MongoDB\BSON\Binary::getType()
159160
*/
160-
static PHP_METHOD(Binary, getType)
161+
static PHP_METHOD(MongoDB_BSON_Binary, getType)
161162
{
162163
php_phongo_binary_t* intern;
163164

@@ -170,7 +171,7 @@ static PHP_METHOD(Binary, getType)
170171

171172
/* {{{ proto array MongoDB\BSON\Binary::jsonSerialize()
172173
*/
173-
static PHP_METHOD(Binary, jsonSerialize)
174+
static PHP_METHOD(MongoDB_BSON_Binary, jsonSerialize)
174175
{
175176
php_phongo_binary_t* intern;
176177
char type[3];
@@ -194,7 +195,7 @@ static PHP_METHOD(Binary, jsonSerialize)
194195

195196
/* {{{ proto string MongoDB\BSON\Binary::serialize()
196197
*/
197-
static PHP_METHOD(Binary, serialize)
198+
static PHP_METHOD(MongoDB_BSON_Binary, serialize)
198199
{
199200
php_phongo_binary_t* intern;
200201
zval retval;
@@ -222,7 +223,7 @@ static PHP_METHOD(Binary, serialize)
222223

223224
/* {{{ proto void MongoDB\BSON\Binary::unserialize(string $serialized)
224225
*/
225-
static PHP_METHOD(Binary, unserialize)
226+
static PHP_METHOD(MongoDB_BSON_Binary, unserialize)
226227
{
227228
php_phongo_binary_t* intern;
228229
char* serialized;
@@ -252,7 +253,7 @@ static PHP_METHOD(Binary, unserialize)
252253

253254
/* {{{ proto array MongoDB\Driver\Binary::__serialize()
254255
*/
255-
static PHP_METHOD(Binary, __serialize)
256+
static PHP_METHOD(MongoDB_BSON_Binary, __serialize)
256257
{
257258
PHONGO_PARSE_PARAMETERS_NONE();
258259

@@ -261,7 +262,7 @@ static PHP_METHOD(Binary, __serialize)
261262

262263
/* {{{ proto void MongoDB\Driver\Binary::__unserialize(array $data)
263264
*/
264-
static PHP_METHOD(Binary, __unserialize)
265+
static PHP_METHOD(MongoDB_BSON_Binary, __unserialize)
265266
{
266267
zval* data;
267268

@@ -272,50 +273,6 @@ static PHP_METHOD(Binary, __unserialize)
272273
php_phongo_binary_init_from_hash(Z_BINARY_OBJ_P(getThis()), Z_ARRVAL_P(data));
273274
} /* }}} */
274275

275-
/* {{{ MongoDB\BSON\Binary function entries */
276-
/* clang-format off */
277-
ZEND_BEGIN_ARG_INFO_EX(ai_Binary___construct, 0, 0, 2)
278-
ZEND_ARG_INFO(0, data)
279-
ZEND_ARG_INFO(0, type)
280-
ZEND_END_ARG_INFO()
281-
282-
ZEND_BEGIN_ARG_INFO_EX(ai_Binary___set_state, 0, 0, 1)
283-
ZEND_ARG_ARRAY_INFO(0, properties, 0)
284-
ZEND_END_ARG_INFO()
285-
286-
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(ai_Binary___toString, 0, 0, IS_STRING, 0)
287-
ZEND_END_ARG_INFO()
288-
289-
ZEND_BEGIN_ARG_INFO_EX(ai_Binary___unserialize, 0, 0, 1)
290-
ZEND_ARG_ARRAY_INFO(0, data, 0)
291-
ZEND_END_ARG_INFO()
292-
293-
ZEND_BEGIN_ARG_WITH_TENTATIVE_RETURN_TYPE_INFO_EX(ai_Binary_jsonSerialize, 0, 0, IS_ARRAY, 0)
294-
ZEND_END_ARG_INFO()
295-
296-
ZEND_BEGIN_ARG_INFO_EX(ai_Binary_unserialize, 0, 0, 1)
297-
ZEND_ARG_INFO(0, serialized)
298-
ZEND_END_ARG_INFO()
299-
300-
ZEND_BEGIN_ARG_INFO_EX(ai_Binary_void, 0, 0, 0)
301-
ZEND_END_ARG_INFO()
302-
303-
static zend_function_entry php_phongo_binary_me[] = {
304-
PHP_ME(Binary, __construct, ai_Binary___construct, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
305-
PHP_ME(Binary, __serialize, ai_Binary_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
306-
PHP_ME(Binary, __set_state, ai_Binary___set_state, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
307-
PHP_ME(Binary, __toString, ai_Binary___toString, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
308-
PHP_ME(Binary, __unserialize, ai_Binary___unserialize, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
309-
PHP_ME(Binary, jsonSerialize, ai_Binary_jsonSerialize, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
310-
PHP_ME(Binary, serialize, ai_Binary_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
311-
PHP_ME(Binary, unserialize, ai_Binary_unserialize, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
312-
PHP_ME(Binary, getData, ai_Binary_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
313-
PHP_ME(Binary, getType, ai_Binary_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
314-
PHP_FE_END
315-
};
316-
/* clang-format on */
317-
/* }}} */
318-
319276
/* {{{ MongoDB\BSON\Binary object handlers */
320277
static zend_object_handlers php_phongo_handler_binary;
321278

@@ -400,17 +357,8 @@ static HashTable* php_phongo_binary_get_properties(phongo_compat_object_handler_
400357

401358
void php_phongo_binary_init_ce(INIT_FUNC_ARGS) /* {{{ */
402359
{
403-
zend_class_entry ce;
404-
405-
INIT_NS_CLASS_ENTRY(ce, "MongoDB\\BSON", "Binary", php_phongo_binary_me);
406-
php_phongo_binary_ce = zend_register_internal_class(&ce);
360+
php_phongo_binary_ce = register_class_MongoDB_BSON_Binary(php_phongo_binary_interface_ce, php_phongo_json_serializable_ce, php_phongo_type_ce, zend_ce_serializable);
407361
php_phongo_binary_ce->create_object = php_phongo_binary_create_object;
408-
PHONGO_CE_FINAL(php_phongo_binary_ce);
409-
410-
zend_class_implements(php_phongo_binary_ce, 1, php_phongo_binary_interface_ce);
411-
zend_class_implements(php_phongo_binary_ce, 1, php_phongo_json_serializable_ce);
412-
zend_class_implements(php_phongo_binary_ce, 1, php_phongo_type_ce);
413-
zend_class_implements(php_phongo_binary_ce, 1, zend_ce_serializable);
414362

415363
#if PHP_VERSION_ID >= 80000
416364
zend_class_implements(php_phongo_binary_ce, 1, zend_ce_stringable);
@@ -423,14 +371,4 @@ void php_phongo_binary_init_ce(INIT_FUNC_ARGS) /* {{{ */
423371
php_phongo_handler_binary.get_properties = php_phongo_binary_get_properties;
424372
php_phongo_handler_binary.free_obj = php_phongo_binary_free_object;
425373
php_phongo_handler_binary.offset = XtOffsetOf(php_phongo_binary_t, std);
426-
427-
zend_declare_class_constant_long(php_phongo_binary_ce, ZEND_STRL("TYPE_GENERIC"), BSON_SUBTYPE_BINARY);
428-
zend_declare_class_constant_long(php_phongo_binary_ce, ZEND_STRL("TYPE_FUNCTION"), BSON_SUBTYPE_FUNCTION);
429-
zend_declare_class_constant_long(php_phongo_binary_ce, ZEND_STRL("TYPE_OLD_BINARY"), BSON_SUBTYPE_BINARY_DEPRECATED);
430-
zend_declare_class_constant_long(php_phongo_binary_ce, ZEND_STRL("TYPE_OLD_UUID"), BSON_SUBTYPE_UUID_DEPRECATED);
431-
zend_declare_class_constant_long(php_phongo_binary_ce, ZEND_STRL("TYPE_UUID"), BSON_SUBTYPE_UUID);
432-
zend_declare_class_constant_long(php_phongo_binary_ce, ZEND_STRL("TYPE_MD5"), BSON_SUBTYPE_MD5);
433-
zend_declare_class_constant_long(php_phongo_binary_ce, ZEND_STRL("TYPE_ENCRYPTED"), BSON_SUBTYPE_ENCRYPTED);
434-
zend_declare_class_constant_long(php_phongo_binary_ce, ZEND_STRL("TYPE_COLUMN"), BSON_SUBTYPE_COLUMN);
435-
zend_declare_class_constant_long(php_phongo_binary_ce, ZEND_STRL("TYPE_USER_DEFINED"), BSON_SUBTYPE_USER);
436374
} /* }}} */

0 commit comments

Comments
 (0)