Skip to content

Commit 34c0b66

Browse files
committed
Expose ReflectionParameter::isPromoted()
1 parent fe75c27 commit 34c0b66

File tree

6 files changed

+50
-6
lines changed

6 files changed

+50
-6
lines changed

Zend/zend_compile.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5894,7 +5894,9 @@ void zend_compile_params(zend_ast *ast, zend_ast *return_type_ast, uint32_t fall
58945894
zend_alloc_cache_slots(zend_type_get_num_classes(arg_info->type));
58955895
}
58965896

5897-
ZEND_TYPE_FULL_MASK(arg_info->type) |= _ZEND_ARG_INFO_FLAGS(is_ref, is_variadic);
5897+
uint32_t arg_info_flags = _ZEND_ARG_INFO_FLAGS(is_ref, is_variadic)
5898+
| (visibility ? _ZEND_IS_PROMOTED_BIT : 0);
5899+
ZEND_TYPE_FULL_MASK(arg_info->type) |= arg_info_flags;
58985900
if (opcode == ZEND_RECV) {
58995901
opline->op2.num = type_ast ?
59005902
ZEND_TYPE_FULL_MASK(arg_info->type) : MAY_BE_ANY;

Zend/zend_compile.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -941,10 +941,13 @@ ZEND_API zend_string *zend_type_to_string(zend_type type);
941941
/* The send mode and is_variadic flag are stored as part of zend_type */
942942
#define _ZEND_SEND_MODE_SHIFT _ZEND_TYPE_EXTRA_FLAGS_SHIFT
943943
#define _ZEND_IS_VARIADIC_BIT (1 << (_ZEND_TYPE_EXTRA_FLAGS_SHIFT + 2))
944+
#define _ZEND_IS_PROMOTED_BIT (1 << (_ZEND_TYPE_EXTRA_FLAGS_SHIFT + 3))
944945
#define ZEND_ARG_SEND_MODE(arg_info) \
945946
((ZEND_TYPE_FULL_MASK((arg_info)->type) >> _ZEND_SEND_MODE_SHIFT) & 3)
946947
#define ZEND_ARG_IS_VARIADIC(arg_info) \
947948
((ZEND_TYPE_FULL_MASK((arg_info)->type) & _ZEND_IS_VARIADIC_BIT) != 0)
949+
#define ZEND_ARG_IS_PROMOTED(arg_info) \
950+
((ZEND_TYPE_FULL_MASK((arg_info)->type) & _ZEND_IS_PROMOTED_BIT) != 0)
948951

949952
#define ZEND_DIM_IS (1 << 0) /* isset fetch needed for null coalesce */
950953
#define ZEND_DIM_ALTERNATIVE_SYNTAX (1 << 1) /* deprecated curly brace usage */

ext/reflection/php_reflection.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2870,6 +2870,22 @@ ZEND_METHOD(ReflectionParameter, isVariadic)
28702870
}
28712871
/* }}} */
28722872

2873+
/* {{{ proto public bool ReflectionParameter::isPromoted()
2874+
Returns this constructor parameter has been promoted to a property */
2875+
ZEND_METHOD(ReflectionParameter, isPromoted)
2876+
{
2877+
reflection_object *intern;
2878+
parameter_reference *param;
2879+
2880+
if (zend_parse_parameters_none() == FAILURE) {
2881+
RETURN_THROWS();
2882+
}
2883+
GET_REFLECTION_OBJECT_PTR(param);
2884+
2885+
RETVAL_BOOL(ZEND_ARG_IS_PROMOTED(param->arg_info));
2886+
}
2887+
/* }}} */
2888+
28732889
/* {{{ proto public bool ReflectionType::allowsNull()
28742890
Returns whether parameter MAY be null */
28752891
ZEND_METHOD(ReflectionType, allowsNull)

ext/reflection/php_reflection.stub.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,8 @@ public function getDefaultValueConstantName() {}
555555
/** @return bool */
556556
public function isVariadic() {}
557557

558+
public function isPromoted(): bool {}
559+
558560
/** @return ReflectionAttribute[] */
559561
public function getAttributes(?string $name = null, int $flags = 0): array {}
560562
}

ext/reflection/php_reflection_arginfo.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,8 @@ ZEND_END_ARG_INFO()
402402

403403
#define arginfo_class_ReflectionParameter_isVariadic arginfo_class_ReflectionFunctionAbstract___clone
404404

405+
#define arginfo_class_ReflectionParameter_isPromoted arginfo_class_ReflectionProperty_isPromoted
406+
405407
#define arginfo_class_ReflectionParameter_getAttributes arginfo_class_ReflectionFunctionAbstract_getAttributes
406408

407409
#define arginfo_class_ReflectionType___clone arginfo_class_ReflectionFunctionAbstract___clone
@@ -647,6 +649,7 @@ ZEND_METHOD(ReflectionParameter, getDefaultValue);
647649
ZEND_METHOD(ReflectionParameter, isDefaultValueConstant);
648650
ZEND_METHOD(ReflectionParameter, getDefaultValueConstantName);
649651
ZEND_METHOD(ReflectionParameter, isVariadic);
652+
ZEND_METHOD(ReflectionParameter, isPromoted);
650653
ZEND_METHOD(ReflectionParameter, getAttributes);
651654
ZEND_METHOD(ReflectionType, allowsNull);
652655
ZEND_METHOD(ReflectionType, __toString);
@@ -907,6 +910,7 @@ static const zend_function_entry class_ReflectionParameter_methods[] = {
907910
ZEND_ME(ReflectionParameter, isDefaultValueConstant, arginfo_class_ReflectionParameter_isDefaultValueConstant, ZEND_ACC_PUBLIC)
908911
ZEND_ME(ReflectionParameter, getDefaultValueConstantName, arginfo_class_ReflectionParameter_getDefaultValueConstantName, ZEND_ACC_PUBLIC)
909912
ZEND_ME(ReflectionParameter, isVariadic, arginfo_class_ReflectionParameter_isVariadic, ZEND_ACC_PUBLIC)
913+
ZEND_ME(ReflectionParameter, isPromoted, arginfo_class_ReflectionParameter_isPromoted, ZEND_ACC_PUBLIC)
910914
ZEND_ME(ReflectionParameter, getAttributes, arginfo_class_ReflectionParameter_getAttributes, ZEND_ACC_PUBLIC)
911915
ZEND_FE_END
912916
};

ext/reflection/tests/constructor_promotion.phpt

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ Using Reflection on promoted properties
44
<?php
55

66
class Test {
7+
public $z;
78
public function __construct(
89
public int $x,
910
/** @SomeAnnotation() */
10-
public string $y = "123"
11+
public string $y = "123",
12+
string $z = "abc",
1113
) {}
1214
}
1315

@@ -17,11 +19,20 @@ echo $rc, "\n";
1719
$y = $rc->getProperty('y');
1820
var_dump($y->isPromoted());
1921
var_dump($y->getDocComment());
22+
$z = $rc->getProperty('z');
23+
var_dump($z->isPromoted());
24+
25+
echo "\n";
26+
27+
$rp = new ReflectionParameter([Test::class, '__construct'], 'y');
28+
var_dump($rp->isPromoted());
29+
$rp = new ReflectionParameter([Test::class, '__construct'], 'z');
30+
var_dump($rp->isPromoted());
2031

2132
?>
2233
--EXPECTF--
2334
Class [ <user> class Test ] {
24-
@@ %s 3-9
35+
@@ %s 3-11
2536

2637
- Constants [0] {
2738
}
@@ -32,22 +43,28 @@ Class [ <user> class Test ] {
3243
- Static methods [0] {
3344
}
3445

35-
- Properties [2] {
46+
- Properties [3] {
47+
Property [ public $z = NULL ]
3648
Property [ public int $x ]
3749
Property [ public string $y ]
3850
}
3951

4052
- Methods [1] {
4153
Method [ <user, ctor> public method __construct ] {
42-
@@ %s 4 - 8
54+
@@ %s 5 - 10
4355

44-
- Parameters [2] {
56+
- Parameters [3] {
4557
Parameter #0 [ <required> int $x ]
4658
Parameter #1 [ <optional> string $y = '123' ]
59+
Parameter #2 [ <optional> string $z = 'abc' ]
4760
}
4861
}
4962
}
5063
}
5164

5265
bool(true)
5366
string(24) "/** @SomeAnnotation() */"
67+
bool(false)
68+
69+
bool(true)
70+
bool(false)

0 commit comments

Comments
 (0)