Skip to content

Commit c205daa

Browse files
committed
Introduce Deprecated constructor and streamline naming.
1 parent e5e7fea commit c205daa

File tree

5 files changed

+57
-7
lines changed

5 files changed

+57
-7
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
--TEST--
2+
#[Deprecated] attribute
3+
--FILE--
4+
<?php
5+
6+
#[Deprecated("use test() instead")]
7+
function test() {
8+
}
9+
10+
$reflection = new ReflectionFunction('test');
11+
var_dump($reflection->getAttributes()[0]->newInstance());
12+
13+
--EXPECTF--
14+
object(Deprecated)#3 (1) {
15+
["message"]=>
16+
string(18) "use test() instead"
17+
}

Zend/zend_attributes.c

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,18 @@ ZEND_METHOD(Attribute, __construct)
9090
ZVAL_LONG(OBJ_PROP_NUM(Z_OBJ_P(ZEND_THIS), 0), flags);
9191
}
9292

93+
ZEND_METHOD(Deprecated, __construct)
94+
{
95+
zend_string *message;
96+
97+
ZEND_PARSE_PARAMETERS_START(0, 1)
98+
Z_PARAM_OPTIONAL
99+
Z_PARAM_STR(message)
100+
ZEND_PARSE_PARAMETERS_END();
101+
102+
ZVAL_STR(OBJ_PROP_NUM(Z_OBJ_P(ZEND_THIS), 0), message);
103+
}
104+
93105
static zend_attribute *get_attribute(HashTable *attributes, zend_string *lcname, uint32_t offset)
94106
{
95107
if (attributes) {
@@ -313,10 +325,15 @@ void zend_register_attribute_ce(void)
313325
attr = zend_internal_attribute_register(zend_ce_attribute, ZEND_ATTRIBUTE_TARGET_CLASS);
314326
attr->validator = validate_attribute;
315327

316-
INIT_CLASS_ENTRY(ce, "Deprecated", NULL);
328+
INIT_CLASS_ENTRY(ce, "Deprecated", class_Deprecated_methods);
317329
zend_ce_deprecated_attribute = zend_register_internal_class(&ce);
318330
zend_ce_deprecated_attribute->ce_flags |= ZEND_ACC_FINAL;
319331

332+
ZVAL_UNDEF(&tmp);
333+
str = zend_string_init(ZEND_STRL("message"), 1);
334+
zend_declare_typed_property(zend_ce_deprecated_attribute, str, &tmp, ZEND_ACC_PUBLIC, NULL, (zend_type) ZEND_TYPE_INIT_CODE(IS_STRING, 0, 0));
335+
zend_string_release(str);
336+
320337
attr = zend_internal_attribute_register(zend_ce_deprecated_attribute, ZEND_ATTRIBUTE_TARGET_FUNCTION | ZEND_ATTRIBUTE_TARGET_METHOD);
321338
attr->validator = validate_deprecated_attribute;
322339
}

Zend/zend_attributes.stub.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,8 @@ final class Attribute
66
{
77
public function __construct(int $flags = Attribute::TARGET_ALL) {}
88
}
9+
10+
final class Deprecated
11+
{
12+
public function __construct(?string $message = null) {}
13+
}

Zend/zend_attributes_arginfo.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,26 @@
11
/* This is a generated file, edit the .stub.php file instead.
2-
* Stub hash: 54eede8541597ec2ac5c04e31d14e2db7e8c5556 */
2+
* Stub hash: 9cd28e1ad07c22dc16341b5ee781f0c29f689307 */
33

44
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Attribute___construct, 0, 0, 0)
55
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, flags, IS_LONG, 0, "Attribute::TARGET_ALL")
66
ZEND_END_ARG_INFO()
77

8+
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Deprecated___construct, 0, 0, 0)
9+
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, message, IS_STRING, 1, "null")
10+
ZEND_END_ARG_INFO()
11+
812

913
ZEND_METHOD(Attribute, __construct);
14+
ZEND_METHOD(Deprecated, __construct);
1015

1116

1217
static const zend_function_entry class_Attribute_methods[] = {
1318
ZEND_ME(Attribute, __construct, arginfo_class_Attribute___construct, ZEND_ACC_PUBLIC)
1419
ZEND_FE_END
1520
};
21+
22+
23+
static const zend_function_entry class_Deprecated_methods[] = {
24+
ZEND_ME(Deprecated, __construct, arginfo_class_Deprecated___construct, ZEND_ACC_PUBLIC)
25+
ZEND_FE_END
26+
};

Zend/zend_execute.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1491,7 +1491,7 @@ static zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_wrong_property_read(z
14911491
ZEND_API ZEND_COLD void ZEND_FASTCALL zend_deprecated_function(const zend_function *fbc)
14921492
{
14931493
zend_attribute *deprecated;
1494-
zend_string *reason = NULL;
1494+
zend_string *message_suffix = NULL;
14951495

14961496
if (fbc->common.attributes != NULL) {
14971497
deprecated = zend_get_attribute_str(fbc->common.attributes, "deprecated", sizeof("deprecated")-1);
@@ -1500,22 +1500,22 @@ ZEND_API ZEND_COLD void ZEND_FASTCALL zend_deprecated_function(const zend_functi
15001500
zval message;
15011501

15021502
if (FAILURE != zend_get_attribute_value(&message, deprecated, 0, fbc->common.scope)) {
1503-
reason = Z_STR(message);
1503+
message_suffix = Z_STR(message);
15041504
}
15051505
}
15061506
}
15071507

1508-
if (reason != NULL) {
1508+
if (message_suffix != NULL) {
15091509
if (fbc->common.scope) {
15101510
zend_error(E_DEPRECATED, "Method %s::%s() is deprecated, %s",
15111511
ZSTR_VAL(fbc->common.scope->name),
15121512
ZSTR_VAL(fbc->common.function_name),
1513-
ZSTR_VAL(reason)
1513+
ZSTR_VAL(message_suffix)
15141514
);
15151515
} else {
15161516
zend_error(E_DEPRECATED, "Function %s() is deprecated, %s",
15171517
ZSTR_VAL(fbc->common.function_name),
1518-
ZSTR_VAL(reason)
1518+
ZSTR_VAL(message_suffix)
15191519
);
15201520
}
15211521
} else {

0 commit comments

Comments
 (0)