Skip to content

Commit 5dbe212

Browse files
committed
fix build back to php 7.3
1 parent e263dc6 commit 5dbe212

File tree

2 files changed

+87
-49
lines changed

2 files changed

+87
-49
lines changed

phper-sys/php_wrapper.c

Lines changed: 53 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,17 @@ typedef ZEND_INI_MH(phper_zend_ini_mh);
2727
typedef zend_class_entry *
2828
phper_init_class_entry_handler(zend_class_entry *class_ce, void *argument);
2929

30+
// ==================================================
31+
// BC for older PHP versions:
32+
// ==================================================
33+
#ifndef IS_MIXED
34+
#define IS_MIXED 0x1A
35+
#endif
36+
37+
#ifndef IS_NEVER
38+
#define IS_NEVER 0x1B
39+
#endif
40+
3041
// ==================================================
3142
// zval apis:
3243
// ==================================================
@@ -467,34 +478,33 @@ phper_zend_begin_arg_with_return_type_info_ex(bool return_reference,
467478
uint32_t typ, bool allow_null) {
468479
#define static
469480
#define const
470-
#if PHP_VERSION_ID >= 70200
471-
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(info, return_reference,
472-
required_num_args, typ, allow_null)
481+
#if PHP_VERSION_ID >= 70400
482+
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(info, return_reference, required_num_args, typ, allow_null)
483+
#elif PHP_VERSION_ID >= 70200
484+
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(info, return_reference, required_num_args, typ, allow_null)
473485
#else
474-
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(
475-
info, return_reference, required_num_args, typ, NULL, allow_null)
486+
ZEND_BEGIN_ARG_INFO_EX(info, 0, return_reference, required_num_args)
476487
#endif
477488
ZEND_END_ARG_INFO()
478489
return info[0];
479490
#undef static
480491
#undef const
481492
}
482493

494+
483495
zend_internal_arg_info
484496
phper_zend_begin_arg_with_return_obj_info_ex(bool return_reference,
485497
uintptr_t required_num_args,
486498
const char* class_name,
487499
bool allow_null) {
488500
#define static
489501
#define const
490-
#if PHP_VERSION_ID >= 70200
502+
#if PHP_VERSION_ID >= 70400
491503
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(info, return_reference,
492-
required_num_args,
493-
class_name, allow_null)
494-
#else
495-
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(
496-
info, return_reference, required_num_args,
497-
class_name, NULL, allow_null)
504+
required_num_args,
505+
class_name, allow_null)
506+
#elif PHP_VERSION_ID >= 70200
507+
ZEND_BEGIN_ARG_INFO_EX(info, 0, return_reference, required_num_args)
498508
#endif
499509
ZEND_END_ARG_INFO()
500510
return info[0];
@@ -513,26 +523,48 @@ zend_internal_arg_info phper_zend_arg_info_with_type(bool pass_by_ref,
513523
uint32_t type_hint,
514524
bool allow_null,
515525
const char *default_value) {
516-
#if PHP_VERSION_ID >= 70200
526+
#if PHP_VERSION_ID >= 70400
527+
zend_internal_arg_info info[] = {
528+
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(pass_by_ref, name, type_hint, allow_null, default_value)
529+
};
530+
#elif PHP_VERSION_ID >= 70200
517531
zend_internal_arg_info info[] = {
518-
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(pass_by_ref, , type_hint, allow_null, default_value)
532+
ZEND_ARG_TYPE_INFO(pass_by_ref, name, type_hint, allow_null)
519533
};
520534
#else
535+
// PHP 7.0 and below: fallback
521536
zend_internal_arg_info info[] = {
522-
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(pass_by_ref, , type_hint, NULL, allow_null, default_value)
537+
ZEND_ARG_INFO(pass_by_ref, name)
523538
};
524539
#endif
525540
info[0].name = name;
526541
return info[0];
527542
}
528543

529544
zend_internal_arg_info phper_zend_arg_obj_info(bool pass_by_ref,
530-
const char *name,
531-
const char *class_name,
532-
bool allow_null) {
545+
const char *name,
546+
const char *class_name,
547+
bool allow_null) {
548+
#if PHP_VERSION_ID >= 70400
533549
zend_internal_arg_info info[] = {
534-
ZEND_ARG_OBJ_INFO(pass_by_ref, , class_name, allow_null)
550+
ZEND_ARG_OBJ_INFO_WITH_DEFAULT_VALUE(pass_by_ref, name, class_name, allow_null, NULL)
535551
};
536-
info[0].name = name;
537552
return info[0];
538-
}
553+
#elif PHP_VERSION_ID >= 70200
554+
zend_internal_arg_info info = {
555+
.name = name,
556+
.type = 0, // can't encode class type
557+
.pass_by_reference = pass_by_ref,
558+
.is_variadic = 0
559+
};
560+
return info;
561+
#else
562+
zend_internal_arg_info info = {
563+
.name = name,
564+
.type = 0,
565+
.pass_by_reference = pass_by_ref,
566+
.is_variadic = 0
567+
};
568+
return info;
569+
#endif
570+
}

tests/integration/tests/php/typehints.php

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
['testArrayOptional', 'array', false, false],
4343
['testArrayNullable', 'array', true, true],
4444

45-
['testMixed', 'mixed', true, true],
45+
['testMixed', 'mixed', true, true, '8.0'],
4646

4747
['testCallable', 'callable', false, true],
4848
['testCallableOptional', 'callable', false, false],
@@ -58,9 +58,9 @@
5858

5959
['testNull', 'null', true, true, '8.2'],
6060

61-
['testClassEntry', 'class_name', false, true],
62-
['testClassEntryOptional', 'class_name', false, false],
63-
['testClassEntryNullable', 'class_name', true, true],
61+
['testClassEntry', 'class_name', false, true, '7.4'],
62+
['testClassEntryOptional', 'class_name', false, false, '7.4'],
63+
['testClassEntryNullable', 'class_name', true, true, '7.4'],
6464
];
6565

6666
// typehints
@@ -105,11 +105,11 @@
105105
['returnCallableNullable', 'callable', true],
106106
['returnIterable', 'iterable', false],
107107
['returnIterableNullable', 'iterable', true],
108-
['returnMixed', 'mixed', true],
108+
['returnMixed', 'mixed', true, '7.4'],
109109
['returnNever', 'never', false, '8.1'],
110110
['returnVoid', 'void', false],
111-
['returnClassEntry', 'class_name', false],
112-
['returnClassEntryNullable', 'class_name', true],
111+
['returnClassEntry', 'class_name', false, '7.4'],
112+
['returnClassEntryNullable', 'class_name', true, '7.4'],
113113
];
114114
echo PHP_EOL . 'Testing return typehints' . PHP_EOL;
115115
$cls = new \IntegrationTest\TypeHints\ReturnTypeHintTest();
@@ -147,23 +147,27 @@ public function setValue($value): void {
147147
assert_eq($foo, $handler->handle($foo));
148148

149149
$argumentDefaultValueProvider = [
150-
// <method>, <expected default value>
151-
['stringDefault', 'foobarbaz', 'string'],
152-
['stringConstantDefault', PHP_VERSION, 'string'],
153-
['boolDefaultTrue', true, 'boolean'],
154-
['boolDefaultFalse', false, 'boolean'],
155-
['intDefault', 42, 'integer'],
156-
['floatDefault', 3.14159, 'double'],
157-
['arrayDefault', ['a' => 'b'], 'array'],
158-
['iterableDefault', [0 => 1], 'array'],
159-
['mixedDefault', 999, 'integer'],
150+
// <method>, <expected default value>, <(optional) min php version>
151+
['stringDefault', 'foobarbaz', 'string', '7.4'],
152+
['stringConstantDefault', PHP_VERSION, 'string', '7.4'],
153+
['boolDefaultTrue', true, 'boolean', '7.4'],
154+
['boolDefaultFalse', false, 'boolean', '7.4'],
155+
['intDefault', 42, 'integer', '7.4'],
156+
['floatDefault', 3.14159, 'double', '7.4'],
157+
['arrayDefault', ['a' => 'b'], 'array', '7.4'],
158+
['iterableDefault', [0 => 1], 'array', '7.4'],
159+
['mixedDefault', 999, 'integer', '7.4'],
160160
];
161161

162162
echo PHP_EOL . 'Testing argument default values' . PHP_EOL;
163163
$cls = new IntegrationTest\TypeHints\ArgumentDefaultValueTest();
164164
$reflection = new ReflectionClass($cls);
165165
foreach ($argumentDefaultValueProvider as $input) {
166166
echo(sprintf("%s..", $input[0]));
167+
if (array_key_exists(3, $input) && !php_at_least($input[3])) {
168+
echo sprintf("SKIPPED requires at least PHP %s", $input[3]) . PHP_EOL;
169+
continue;
170+
}
167171
$reflectionMethod = $reflection->getMethod($input[0]);
168172
$params = $reflectionMethod->getParameters();
169173
$param = $params[0];
@@ -183,14 +187,16 @@ public function setValue($value): void {
183187
['m', 'mixed', 1.23],
184188

185189
];
186-
echo PHP_EOL . 'Testing function typehints' . PHP_EOL;
187-
$reflection = new ReflectionFunction('integration_function_typehints');
188-
$params = $reflection->getParameters();
189-
foreach ($expectedArgs as $i => $input) {
190-
echo(sprintf("argument %d..", $i));
191-
assert_eq($input[0], $params[$i]->getName(), sprintf('argument %d has correct name', $i));
192-
assert_eq($input[1], $params[$i]->getType()->getName(), sprintf('argument %d has correct type', $i));
193-
assert_eq($input[2], $params[$i]->getDefaultValue(), sprintf('argument %d has correct default value', $i));
194-
echo "PASS" . PHP_EOL;
195-
}
196-
assert_eq('void', $reflection->getReturnType()->getName(), 'integration_function_typehints return type is void');
190+
if (PHP_VERSION_ID >= 70400) {
191+
echo PHP_EOL . 'Testing function typehints' . PHP_EOL;
192+
$reflection = new ReflectionFunction('integration_function_typehints');
193+
$params = $reflection->getParameters();
194+
foreach ($expectedArgs as $i => $input) {
195+
echo(sprintf("argument %d..", $i));
196+
assert_eq($input[0], $params[$i]->getName(), sprintf('argument %d has correct name', $i));
197+
assert_eq($input[1], $params[$i]->getType()->getName(), sprintf('argument %d has correct type', $i));
198+
assert_eq($input[2], $params[$i]->getDefaultValue(), sprintf('argument %d has correct default value', $i));
199+
echo "PASS" . PHP_EOL;
200+
}
201+
assert_eq('void', $reflection->getReturnType()->getName(), 'integration_function_typehints return type is void');
202+
}

0 commit comments

Comments
 (0)