Skip to content

Commit 8107a1d

Browse files
committed
Use ZPP instead of custom type checks
We can add these types as a native type declaration to stubs as a side-effect. Closes GH-6068
1 parent e50449b commit 8107a1d

30 files changed

+302
-430
lines changed

ext/intl/formatter/formatter.stub.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,10 @@ public function formatCurrency(float $value, string $currency) {}
4040
public function parseCurrency(string $value, &$currency, &$position = null) {}
4141

4242
/**
43-
* @param int|float $value
4443
* @return bool
4544
* @alias numfmt_set_attribute
4645
*/
47-
public function setAttribute(int $attr, $value) {}
46+
public function setAttribute(int $attr, int|float $value) {}
4847

4948
/**
5049
* @return int|float|false

ext/intl/formatter/formatter_arginfo.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* This is a generated file, edit the .stub.php file instead.
2-
* Stub hash: 041569278b83b65f67fb4848d00d5423e6728165 */
2+
* Stub hash: 60a05cccb434edadeec01cb7d43f919c276ce24d */
33

44
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_NumberFormatter___construct, 0, 0, 2)
55
ZEND_ARG_TYPE_INFO(0, locale, IS_STRING, 0)
@@ -33,7 +33,7 @@ ZEND_END_ARG_INFO()
3333

3434
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_NumberFormatter_setAttribute, 0, 0, 2)
3535
ZEND_ARG_TYPE_INFO(0, attr, IS_LONG, 0)
36-
ZEND_ARG_INFO(0, value)
36+
ZEND_ARG_TYPE_MASK(0, value, MAY_BE_LONG|MAY_BE_DOUBLE, NULL)
3737
ZEND_END_ARG_INFO()
3838

3939
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_NumberFormatter_getAttribute, 0, 0, 1)

ext/intl/formatter/formatter_attr.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ PHP_FUNCTION( numfmt_set_attribute )
129129
FORMATTER_METHOD_INIT_VARS;
130130

131131
/* Parse parameters. */
132-
if( zend_parse_method_parameters( ZEND_NUM_ARGS(), getThis(), "Olz",
132+
if( zend_parse_method_parameters( ZEND_NUM_ARGS(), getThis(), "Oln",
133133
&object, NumberFormatter_ce_ptr, &attribute, &value ) == FAILURE)
134134
{
135135
RETURN_THROWS();

ext/intl/php_intl.stub.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,7 @@ function numfmt_format_currency(NumberFormatter $fmt, float $value, string $curr
223223
*/
224224
function numfmt_parse_currency(NumberFormatter $fmt, string $value, &$currency, &$position = null): float|false {}
225225

226-
/** @param int|float $value */
227-
function numfmt_set_attribute(NumberFormatter $fmt, int $attr, $value): bool {}
226+
function numfmt_set_attribute(NumberFormatter $fmt, int $attr, int|float $value): bool {}
228227

229228
function numfmt_get_attribute(NumberFormatter $fmt, int $attr): int|float|false {}
230229

ext/intl/php_intl_arginfo.h

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

44
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_intlcal_create_instance, 0, 0, IntlCalendar, 1)
55
ZEND_ARG_INFO_WITH_DEFAULT_VALUE(0, timeZone, "null")
@@ -402,7 +402,7 @@ ZEND_END_ARG_INFO()
402402
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_numfmt_set_attribute, 0, 3, _IS_BOOL, 0)
403403
ZEND_ARG_OBJ_INFO(0, fmt, NumberFormatter, 0)
404404
ZEND_ARG_TYPE_INFO(0, attr, IS_LONG, 0)
405-
ZEND_ARG_INFO(0, value)
405+
ZEND_ARG_TYPE_MASK(0, value, MAY_BE_LONG|MAY_BE_DOUBLE, NULL)
406406
ZEND_END_ARG_INFO()
407407

408408
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_numfmt_get_attribute, 0, 2, MAY_BE_LONG|MAY_BE_DOUBLE|MAY_BE_FALSE)

ext/ldap/ldap.c

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1423,7 +1423,9 @@ static void php_set_opts(LDAP *ldap, int sizelimit, int timelimit, int deref, in
14231423
/* {{{ php_ldap_do_search */
14241424
static void php_ldap_do_search(INTERNAL_FUNCTION_PARAMETERS, int scope)
14251425
{
1426-
zval *link, *base_dn, *filter, *attrs = NULL, *attr, *serverctrls = NULL;
1426+
zval *link, *attrs = NULL, *attr, *serverctrls = NULL;
1427+
zend_string *base_dn_str, *filter_str;
1428+
HashTable *base_dn_ht, *filter_ht;
14271429
zend_long attrsonly, sizelimit, timelimit, deref;
14281430
zend_string *ldap_filter = NULL, *ldap_base_dn = NULL;
14291431
char **ldap_attrs = NULL;
@@ -1434,10 +1436,18 @@ static void php_ldap_do_search(INTERNAL_FUNCTION_PARAMETERS, int scope)
14341436
int old_ldap_sizelimit = -1, old_ldap_timelimit = -1, old_ldap_deref = -1;
14351437
int num_attribs = 0, ret = 1, i, errno, argcount = ZEND_NUM_ARGS();
14361438

1437-
if (zend_parse_parameters(argcount, "zzz|a/lllla/", &link, &base_dn, &filter, &attrs, &attrsonly,
1438-
&sizelimit, &timelimit, &deref, &serverctrls) == FAILURE) {
1439-
RETURN_THROWS();
1440-
}
1439+
ZEND_PARSE_PARAMETERS_START(3, 9)
1440+
Z_PARAM_ZVAL(link)
1441+
Z_PARAM_STR_OR_ARRAY_HT(base_dn_str, base_dn_ht)
1442+
Z_PARAM_STR_OR_ARRAY_HT(filter_str, filter_ht)
1443+
Z_PARAM_OPTIONAL
1444+
Z_PARAM_ARRAY_EX(attrs, 0, 1)
1445+
Z_PARAM_LONG(attrsonly)
1446+
Z_PARAM_LONG(sizelimit)
1447+
Z_PARAM_LONG(timelimit)
1448+
Z_PARAM_LONG(deref)
1449+
Z_PARAM_ARRAY_EX(serverctrls, 0, 1)
1450+
ZEND_PARSE_PARAMETERS_END();
14411451

14421452
/* Reverse -> fall through */
14431453
switch (argcount) {
@@ -1486,38 +1496,34 @@ static void php_ldap_do_search(INTERNAL_FUNCTION_PARAMETERS, int scope)
14861496
goto cleanup;
14871497
}
14881498

1489-
if (Z_TYPE_P(base_dn) == IS_ARRAY) {
1490-
nbases = zend_hash_num_elements(Z_ARRVAL_P(base_dn));
1499+
if (base_dn_ht) {
1500+
nbases = zend_hash_num_elements(base_dn_ht);
14911501
if (nbases != nlinks) {
14921502
php_error_docref(NULL, E_WARNING, "Base must either be a string, or an array with the same number of elements as the links array");
14931503
ret = 0;
14941504
goto cleanup;
14951505
}
1496-
zend_hash_internal_pointer_reset(Z_ARRVAL_P(base_dn));
1506+
zend_hash_internal_pointer_reset(base_dn_ht);
14971507
} else {
14981508
nbases = 0; /* this means string, not array */
1499-
ldap_base_dn = zval_get_string(base_dn);
1509+
ldap_base_dn = zend_string_copy(base_dn_str);
15001510
if (EG(exception)) {
15011511
ret = 0;
15021512
goto cleanup;
15031513
}
15041514
}
15051515

1506-
if (Z_TYPE_P(filter) == IS_ARRAY) {
1507-
nfilters = zend_hash_num_elements(Z_ARRVAL_P(filter));
1516+
if (filter_ht) {
1517+
nfilters = zend_hash_num_elements(filter_ht);
15081518
if (nfilters != nlinks) {
15091519
php_error_docref(NULL, E_WARNING, "Filter must either be a string, or an array with the same number of elements as the links array");
15101520
ret = 0;
15111521
goto cleanup;
15121522
}
1513-
zend_hash_internal_pointer_reset(Z_ARRVAL_P(filter));
1523+
zend_hash_internal_pointer_reset(filter_ht);
15141524
} else {
15151525
nfilters = 0; /* this means string, not array */
1516-
ldap_filter = zval_get_string(filter);
1517-
if (EG(exception)) {
1518-
ret = 0;
1519-
goto cleanup;
1520-
}
1526+
ldap_filter = zend_string_copy(filter_str);
15211527
}
15221528

15231529
lds = safe_emalloc(nlinks, sizeof(ldap_linkdata), 0);
@@ -1533,17 +1539,17 @@ static void php_ldap_do_search(INTERNAL_FUNCTION_PARAMETERS, int scope)
15331539
goto cleanup_parallel;
15341540
}
15351541
if (nbases != 0) { /* base_dn an array? */
1536-
entry = zend_hash_get_current_data(Z_ARRVAL_P(base_dn));
1537-
zend_hash_move_forward(Z_ARRVAL_P(base_dn));
1542+
entry = zend_hash_get_current_data(base_dn_ht);
1543+
zend_hash_move_forward(base_dn_ht);
15381544
ldap_base_dn = zval_get_string(entry);
15391545
if (EG(exception)) {
15401546
ret = 0;
15411547
goto cleanup_parallel;
15421548
}
15431549
}
15441550
if (nfilters != 0) { /* filter an array? */
1545-
entry = zend_hash_get_current_data(Z_ARRVAL_P(filter));
1546-
zend_hash_move_forward(Z_ARRVAL_P(filter));
1551+
entry = zend_hash_get_current_data(filter_ht);
1552+
zend_hash_move_forward(filter_ht);
15471553
ldap_filter = zval_get_string(entry);
15481554
if (EG(exception)) {
15491555
ret = 0;
@@ -1588,23 +1594,21 @@ static void php_ldap_do_search(INTERNAL_FUNCTION_PARAMETERS, int scope)
15881594
efree(lds);
15891595
efree(rcs);
15901596
} else {
1591-
ldap_filter = zval_get_string(filter);
1592-
if (EG(exception)) {
1597+
ld = (ldap_linkdata *) zend_fetch_resource_ex(link, "ldap link", le_link);
1598+
if (ld == NULL) {
15931599
ret = 0;
15941600
goto cleanup;
15951601
}
15961602

1597-
ldap_base_dn = zval_get_string(base_dn);
1598-
if (EG(exception)) {
1599-
ret = 0;
1600-
goto cleanup;
1603+
if (!base_dn_str) {
1604+
zend_argument_type_error(2, "must be of type string when argument #1 ($link_identifier) is a resource");
16011605
}
1606+
ldap_base_dn = zend_string_copy(base_dn_str);
16021607

1603-
ld = (ldap_linkdata *) zend_fetch_resource_ex(link, "ldap link", le_link);
1604-
if (ld == NULL) {
1605-
ret = 0;
1606-
goto cleanup;
1608+
if (!filter_str) {
1609+
zend_argument_type_error(3, "must be of type string when argument #1 ($link_identifier) is a resource");
16071610
}
1611+
ldap_filter = zend_string_copy(filter_str);
16081612

16091613
if (argcount > 8) {
16101614
lserverctrls = _php_ldap_controls_from_array(ld->link, serverctrls);

ext/ldap/ldap.stub.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,27 +35,21 @@ function ldap_sasl_bind($link, string $binddn = UNKNOWN, string $password = UNKN
3535

3636
/**
3737
* @param resource|array $link_identifier
38-
* @param string|array $base_dn
39-
* @param string|array $filter
4038
* @return resource|false
4139
*/
42-
function ldap_read($link_identifier, $base_dn, $filter, array $attributes = [], int $attrsonly = 0, int $sizelimit = -1, int $timelimit = -1, int $deref = LDAP_DEREF_NEVER, array $servercontrols = []) {}
40+
function ldap_read($link_identifier, array|string $base_dn, array|string $filter, array $attributes = [], int $attrsonly = 0, int $sizelimit = -1, int $timelimit = -1, int $deref = LDAP_DEREF_NEVER, array $servercontrols = []) {}
4341

4442
/**
4543
* @param resource|array $link_identifier
46-
* @param string|array $base_dn
47-
* @param string|array $filter
4844
* @return resource|false
4945
*/
50-
function ldap_list($link_identifier, $base_dn, $filter, array $attributes = [], int $attrsonly = 0, int $sizelimit = -1, int $timelimit = -1, int $deref = LDAP_DEREF_NEVER, array $servercontrols = []) {}
46+
function ldap_list($link_identifier, array|string $base_dn, array|string $filter, array $attributes = [], int $attrsonly = 0, int $sizelimit = -1, int $timelimit = -1, int $deref = LDAP_DEREF_NEVER, array $servercontrols = []) {}
5147

5248
/**
5349
* @param resource|array $link_identifier
54-
* @param string|array $base_dn
55-
* @param string|array $filter
5650
* @return resource|false
5751
*/
58-
function ldap_search($link_identifier, $base_dn, $filter, array $attributes = [], int $attrsonly = 0, int $sizelimit = -1, int $timelimit = -1, int $deref = LDAP_DEREF_NEVER, array $servercontrols = []) {}
52+
function ldap_search($link_identifier, array|string $base_dn, array|string $filter, array $attributes = [], int $attrsonly = 0, int $sizelimit = -1, int $timelimit = -1, int $deref = LDAP_DEREF_NEVER, array $servercontrols = []) {}
5953

6054
/** @param resource $link_identifier */
6155
function ldap_free_result($link_identifier): bool {}

ext/ldap/ldap_arginfo.h

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

44
#if defined(HAVE_ORALDAP)
55
ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_connect, 0, 0, 0)
@@ -52,8 +52,8 @@ ZEND_END_ARG_INFO()
5252

5353
ZEND_BEGIN_ARG_INFO_EX(arginfo_ldap_read, 0, 0, 3)
5454
ZEND_ARG_INFO(0, link_identifier)
55-
ZEND_ARG_INFO(0, base_dn)
56-
ZEND_ARG_INFO(0, filter)
55+
ZEND_ARG_TYPE_MASK(0, base_dn, MAY_BE_ARRAY|MAY_BE_STRING, NULL)
56+
ZEND_ARG_TYPE_MASK(0, filter, MAY_BE_ARRAY|MAY_BE_STRING, NULL)
5757
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, attributes, IS_ARRAY, 0, "[]")
5858
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, attrsonly, IS_LONG, 0, "0")
5959
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, sizelimit, IS_LONG, 0, "-1")

ext/reflection/php_reflection.c

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3684,38 +3684,35 @@ ZEND_METHOD(ReflectionClassConstant, getAttributes)
36843684
/* {{{ reflection_class_object_ctor */
36853685
static void reflection_class_object_ctor(INTERNAL_FUNCTION_PARAMETERS, int is_object)
36863686
{
3687-
zval *argument;
36883687
zval *object;
3688+
zend_string *arg_class = NULL;
3689+
zend_object *arg_obj;
36893690
reflection_object *intern;
36903691
zend_class_entry *ce;
36913692

36923693
if (is_object) {
36933694
ZEND_PARSE_PARAMETERS_START(1, 1)
3694-
Z_PARAM_OBJECT(argument)
3695+
Z_PARAM_OBJ(arg_obj)
36953696
ZEND_PARSE_PARAMETERS_END();
36963697
} else {
36973698
ZEND_PARSE_PARAMETERS_START(1, 1)
3698-
Z_PARAM_ZVAL(argument)
3699+
Z_PARAM_STR_OR_OBJ(arg_class, arg_obj)
36993700
ZEND_PARSE_PARAMETERS_END();
37003701
}
37013702

37023703
object = ZEND_THIS;
37033704
intern = Z_REFLECTION_P(object);
37043705

3705-
if (Z_TYPE_P(argument) == IS_OBJECT) {
3706-
ZVAL_STR_COPY(reflection_prop_name(object), Z_OBJCE_P(argument)->name);
3707-
intern->ptr = Z_OBJCE_P(argument);
3706+
if (arg_obj) {
3707+
ZVAL_STR_COPY(reflection_prop_name(object), arg_obj->ce->name);
3708+
intern->ptr = arg_obj->ce;
37083709
if (is_object) {
3709-
ZVAL_COPY(&intern->obj, argument);
3710+
ZVAL_OBJ_COPY(&intern->obj, arg_obj);
37103711
}
37113712
} else {
3712-
if (!try_convert_to_string(argument)) {
3713-
RETURN_THROWS();
3714-
}
3715-
3716-
if ((ce = zend_lookup_class(Z_STR_P(argument))) == NULL) {
3713+
if ((ce = zend_lookup_class(arg_class)) == NULL) {
37173714
if (!EG(exception)) {
3718-
zend_throw_exception_ex(reflection_exception_ptr, -1, "Class \"%s\" does not exist", Z_STRVAL_P(argument));
3715+
zend_throw_exception_ex(reflection_exception_ptr, -1, "Class \"%s\" does not exist", ZSTR_VAL(arg_class));
37193716
}
37203717
RETURN_THROWS();
37213718
}

ext/reflection/php_reflection.stub.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,7 @@ class ReflectionClass implements Reflector
202202
{
203203
final private function __clone() {}
204204

205-
/** @param object|string $objectOrClass */
206-
public function __construct($objectOrClass) {}
205+
public function __construct(object|string $objectOrClass) {}
207206

208207
public function __toString(): string {}
209208

ext/reflection/php_reflection_arginfo.h

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

44
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Reflection_getModifierNames, 0, 0, 1)
55
ZEND_ARG_TYPE_INFO(0, modifiers, IS_LONG, 0)
@@ -150,7 +150,7 @@ ZEND_END_ARG_INFO()
150150
#define arginfo_class_ReflectionClass___clone arginfo_class_ReflectionFunctionAbstract___clone
151151

152152
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_ReflectionClass___construct, 0, 0, 1)
153-
ZEND_ARG_INFO(0, objectOrClass)
153+
ZEND_ARG_TYPE_MASK(0, objectOrClass, MAY_BE_OBJECT|MAY_BE_STRING, NULL)
154154
ZEND_END_ARG_INFO()
155155

156156
#define arginfo_class_ReflectionClass___toString arginfo_class_ReflectionFunction___toString

ext/reflection/tests/ReflectionClass_constructor_002.phpt

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ try {
2828

2929
try {
3030
var_dump(new ReflectionClass(array(1,2,3)));
31-
} catch (Exception $e) {
31+
} catch (TypeError $e) {
3232
echo $e->getMessage() . "\n";
3333
}
3434

@@ -45,13 +45,11 @@ try {
4545
}
4646

4747
?>
48-
--EXPECTF--
48+
--EXPECT--
4949
ReflectionClass::__construct() expects exactly 1 parameter, 0 given
5050
Class "" does not exist
5151
Class "1" does not exist
5252
Class "1" does not exist
53-
54-
Warning: Array to string conversion in %s on line %d
55-
Class "Array" does not exist
53+
ReflectionClass::__construct(): Argument #1 ($objectOrClass) must be of type object|string, array given
5654
ReflectionClass::__construct() expects exactly 1 parameter, 2 given
5755
Class "X" does not exist

ext/reflection/tests/ReflectionClass_toString_001.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ Class [ <internal:Reflection> class ReflectionClass implements Reflector, String
3737
Method [ <internal:Reflection, ctor> public method __construct ] {
3838

3939
- Parameters [1] {
40-
Parameter #0 [ <required> $objectOrClass ]
40+
Parameter #0 [ <required> object|string $objectOrClass ]
4141
}
4242
}
4343

0 commit comments

Comments
 (0)