Skip to content

Commit 6e723f2

Browse files
committed
zend_test: add Z_PARAM_OBJ_OF_CLASS_OR_LONG ZPP specifier test
1 parent efddc23 commit 6e723f2

7 files changed

Lines changed: 214 additions & 6 deletions

File tree

‎ext/zend_test/test.c‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -691,6 +691,41 @@ static ZEND_FUNCTION(zend_obj_stdclass_or_string_or_null)
691691
}
692692
}
693693

694+
static ZEND_FUNCTION(zend_obj_stdclass_or_int)
695+
{
696+
zend_long l;
697+
zend_object *object;
698+
699+
ZEND_PARSE_PARAMETERS_START(1, 1)
700+
Z_PARAM_OBJ_OF_CLASS_OR_LONG(object, zend_standard_class_def, l)
701+
ZEND_PARSE_PARAMETERS_END();
702+
703+
if (object) {
704+
RETURN_OBJ_COPY(object);
705+
} else {
706+
RETURN_LONG(l);
707+
}
708+
}
709+
710+
static ZEND_FUNCTION(zend_obj_stdclass_or_int_or_null)
711+
{
712+
zend_long l;
713+
zend_object *object;
714+
bool is_null;
715+
716+
ZEND_PARSE_PARAMETERS_START(1, 1)
717+
Z_PARAM_OBJ_OF_CLASS_OR_LONG_OR_NULL(object, zend_standard_class_def, l, is_null)
718+
ZEND_PARSE_PARAMETERS_END();
719+
720+
if (is_null) {
721+
RETURN_NULL();
722+
} else if (object) {
723+
RETURN_OBJ_COPY(object);
724+
} else {
725+
RETURN_LONG(l);
726+
}
727+
}
728+
694729
static ZEND_FUNCTION(zend_resource)
695730
{
696731
zval *v;

‎ext/zend_test/test.stub.php‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,9 @@ function zend_string_or_object_or_null(object|string|null $param): object|string
371371
function zend_obj_stdclass_or_string(stdClass|string|null $param): stdClass|string {}
372372
function zend_obj_stdclass_or_string_or_null(stdClass|string|null $param): stdClass|string|null {}
373373

374+
function zend_obj_stdclass_or_int(stdClass|int|null $param): stdClass|int {}
375+
function zend_obj_stdclass_or_int_or_null(stdClass|int|null $param): stdClass|int|null {}
376+
374377
function zend_number_or_string(string|int|float $param): string|int|float {}
375378

376379
function zend_number_or_string_or_null(string|int|float|null $param): string|int|float|null {}

‎ext/zend_test/test_arginfo.h‎

Lines changed: 13 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎ext/zend_test/test_decl.h‎

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

‎ext/zend_test/test_legacy_arginfo.h‎

Lines changed: 9 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
--TEST--
2+
Test Z_PARAM_OBJ_OF_CLASS_OR_LONG() and Z_PARAM_OBJ_OF_CLASS_OR_LONG_OR_NULL() (strict_mode)
3+
--EXTENSIONS--
4+
zend_test
5+
--FILE--
6+
<?php
7+
8+
declare(strict_types=1);
9+
10+
$types = require 'types.inc';
11+
12+
foreach ($types as $name => $type) {
13+
echo "Using $name:\n";
14+
try {
15+
var_dump(zend_obj_stdclass_or_int($type));
16+
} catch (Throwable $e) {
17+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
18+
}
19+
try {
20+
var_dump(zend_obj_stdclass_or_int_or_null($type));
21+
} catch (Throwable $e) {
22+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
23+
}
24+
}
25+
26+
?>
27+
--EXPECT--
28+
Using null:
29+
TypeError: zend_obj_stdclass_or_int(): Argument #1 ($param) must be of type stdClass|int, null given
30+
NULL
31+
Using false:
32+
TypeError: zend_obj_stdclass_or_int(): Argument #1 ($param) must be of type stdClass|int, false given
33+
TypeError: zend_obj_stdclass_or_int_or_null(): Argument #1 ($param) must be of type stdClass|int|null, false given
34+
Using true:
35+
TypeError: zend_obj_stdclass_or_int(): Argument #1 ($param) must be of type stdClass|int, true given
36+
TypeError: zend_obj_stdclass_or_int_or_null(): Argument #1 ($param) must be of type stdClass|int|null, true given
37+
Using 42:
38+
int(42)
39+
int(42)
40+
Using 73.5:
41+
TypeError: zend_obj_stdclass_or_int(): Argument #1 ($param) must be of type stdClass|int, float given
42+
TypeError: zend_obj_stdclass_or_int_or_null(): Argument #1 ($param) must be of type stdClass|int|null, float given
43+
Using 'string':
44+
TypeError: zend_obj_stdclass_or_int(): Argument #1 ($param) must be of type stdClass|int, string given
45+
TypeError: zend_obj_stdclass_or_int_or_null(): Argument #1 ($param) must be of type stdClass|int|null, string given
46+
Using '15':
47+
TypeError: zend_obj_stdclass_or_int(): Argument #1 ($param) must be of type stdClass|int, string given
48+
TypeError: zend_obj_stdclass_or_int_or_null(): Argument #1 ($param) must be of type stdClass|int|null, string given
49+
Using '56.7':
50+
TypeError: zend_obj_stdclass_or_int(): Argument #1 ($param) must be of type stdClass|int, string given
51+
TypeError: zend_obj_stdclass_or_int_or_null(): Argument #1 ($param) must be of type stdClass|int|null, string given
52+
Using 'stdClass':
53+
TypeError: zend_obj_stdclass_or_int(): Argument #1 ($param) must be of type stdClass|int, string given
54+
TypeError: zend_obj_stdclass_or_int_or_null(): Argument #1 ($param) must be of type stdClass|int|null, string given
55+
Using anon class name:
56+
TypeError: zend_obj_stdclass_or_int(): Argument #1 ($param) must be of type stdClass|int, string given
57+
TypeError: zend_obj_stdclass_or_int_or_null(): Argument #1 ($param) must be of type stdClass|int|null, string given
58+
Using []:
59+
TypeError: zend_obj_stdclass_or_int(): Argument #1 ($param) must be of type stdClass|int, array given
60+
TypeError: zend_obj_stdclass_or_int_or_null(): Argument #1 ($param) must be of type stdClass|int|null, array given
61+
Using new stdClass():
62+
object(stdClass)#2 (0) {
63+
}
64+
object(stdClass)#2 (0) {
65+
}
66+
Using new S():
67+
TypeError: zend_obj_stdclass_or_int(): Argument #1 ($param) must be of type stdClass|int, S given
68+
TypeError: zend_obj_stdclass_or_int_or_null(): Argument #1 ($param) must be of type stdClass|int|null, S given
69+
Using STDOUT:
70+
TypeError: zend_obj_stdclass_or_int(): Argument #1 ($param) must be of type stdClass|int, resource given
71+
TypeError: zend_obj_stdclass_or_int_or_null(): Argument #1 ($param) must be of type stdClass|int|null, resource given
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
--TEST--
2+
Test Z_PARAM_OBJ_OF_CLASS_OR_LONG() and Z_PARAM_OBJ_OF_CLASS_OR_LONG_OR_NULL() (weak_mode)
3+
--EXTENSIONS--
4+
zend_test
5+
--FILE--
6+
<?php
7+
8+
$types = require 'types.inc';
9+
10+
foreach ($types as $name => $type) {
11+
echo "Using $name:\n";
12+
try {
13+
var_dump(zend_obj_stdclass_or_int($type));
14+
} catch (Throwable $e) {
15+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
16+
}
17+
try {
18+
var_dump(zend_obj_stdclass_or_int_or_null($type));
19+
} catch (Throwable $e) {
20+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
21+
}
22+
}
23+
24+
?>
25+
--EXPECTF--
26+
Using null:
27+
28+
Deprecated: zend_obj_stdclass_or_int(): Passing null to parameter #1 ($param) of type stdClass|int|null is deprecated in %s on line %d
29+
int(0)
30+
NULL
31+
Using false:
32+
int(0)
33+
int(0)
34+
Using true:
35+
int(1)
36+
int(1)
37+
Using 42:
38+
int(42)
39+
int(42)
40+
Using 73.5:
41+
42+
Deprecated: Implicit conversion from float 73.5 to int loses precision in %s on line %d
43+
int(73)
44+
45+
Deprecated: Implicit conversion from float 73.5 to int loses precision in %s on line %d
46+
int(73)
47+
Using 'string':
48+
TypeError: zend_obj_stdclass_or_int(): Argument #1 ($param) must be of type stdClass|int, string given
49+
TypeError: zend_obj_stdclass_or_int_or_null(): Argument #1 ($param) must be of type stdClass|int|null, string given
50+
Using '15':
51+
int(15)
52+
int(15)
53+
Using '56.7':
54+
55+
Deprecated: Implicit conversion from float-string "56.7" to int loses precision in %s on line %d
56+
int(56)
57+
58+
Deprecated: Implicit conversion from float-string "56.7" to int loses precision in %s on line %d
59+
int(56)
60+
Using 'stdClass':
61+
TypeError: zend_obj_stdclass_or_int(): Argument #1 ($param) must be of type stdClass|int, string given
62+
TypeError: zend_obj_stdclass_or_int_or_null(): Argument #1 ($param) must be of type stdClass|int|null, string given
63+
Using anon class name:
64+
TypeError: zend_obj_stdclass_or_int(): Argument #1 ($param) must be of type stdClass|int, string given
65+
TypeError: zend_obj_stdclass_or_int_or_null(): Argument #1 ($param) must be of type stdClass|int|null, string given
66+
Using []:
67+
TypeError: zend_obj_stdclass_or_int(): Argument #1 ($param) must be of type stdClass|int, array given
68+
TypeError: zend_obj_stdclass_or_int_or_null(): Argument #1 ($param) must be of type stdClass|int|null, array given
69+
Using new stdClass():
70+
object(stdClass)#2 (0) {
71+
}
72+
object(stdClass)#2 (0) {
73+
}
74+
Using new S():
75+
TypeError: zend_obj_stdclass_or_int(): Argument #1 ($param) must be of type stdClass|int, S given
76+
TypeError: zend_obj_stdclass_or_int_or_null(): Argument #1 ($param) must be of type stdClass|int|null, S given
77+
Using STDOUT:
78+
TypeError: zend_obj_stdclass_or_int(): Argument #1 ($param) must be of type stdClass|int, resource given
79+
TypeError: zend_obj_stdclass_or_int_or_null(): Argument #1 ($param) must be of type stdClass|int|null, resource given

0 commit comments

Comments
 (0)