Skip to content

Commit c01addc

Browse files
committed
Add support for deprecating class constants
1 parent 4d9a8e0 commit c01addc

File tree

5 files changed

+96
-9
lines changed

5 files changed

+96
-9
lines changed

Zend/Optimizer/pass1.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ void zend_optimizer_pass1(zend_op_array *op_array, zend_optimizer_ctx *ctx)
164164
if (ce) {
165165
zend_class_constant *cc = zend_hash_find_ptr(
166166
&ce->constants_table, Z_STR(ZEND_OP2_LITERAL(opline)));
167-
if (cc && (ZEND_CLASS_CONST_FLAGS(cc) & ZEND_ACC_PPP_MASK) == ZEND_ACC_PUBLIC && !(ce->ce_flags & ZEND_ACC_TRAIT)) {
167+
if (cc && !(ZEND_CLASS_CONST_FLAGS(cc) & ZEND_ACC_DEPRECATED) && (ZEND_CLASS_CONST_FLAGS(cc) & ZEND_ACC_PPP_MASK) == ZEND_ACC_PUBLIC && !(ce->ce_flags & ZEND_ACC_TRAIT)) {
168168
zval *c = &cc->value;
169169
if (Z_TYPE_P(c) == IS_CONSTANT_AST) {
170170
zend_ast *ast = Z_ASTVAL_P(c);

Zend/zend_compile.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1728,7 +1728,9 @@ static bool zend_try_compile_const_expr_resolve_class_name(zval *zv, zend_ast *c
17281728
/* We don't use zend_verify_const_access because we need to deal with unlinked classes. */
17291729
static bool zend_verify_ct_const_access(zend_class_constant *c, zend_class_entry *scope)
17301730
{
1731-
if (c->ce->ce_flags & ZEND_ACC_TRAIT) {
1731+
if (ZEND_CLASS_CONST_FLAGS(c) & ZEND_ACC_DEPRECATED) {
1732+
return 0;
1733+
} else if (c->ce->ce_flags & ZEND_ACC_TRAIT) {
17321734
/* This condition is only met on directly accessing trait constants,
17331735
* because the ce is replaced to the class entry of the composing class
17341736
* on binding. */

Zend/zend_constants.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,14 @@ ZEND_API zval *zend_get_class_constant_ex(zend_string *class_name, zend_string *
347347
goto failure;
348348
}
349349

350+
if (UNEXPECTED(ZEND_CLASS_CONST_FLAGS(c) & ZEND_ACC_DEPRECATED)) {
351+
if ((flags & ZEND_FETCH_CLASS_SILENT) == 0) {
352+
zend_error(E_DEPRECATED, "Constant %s::%s is deprecated", ZSTR_VAL(class_name), ZSTR_VAL(constant_name));
353+
if (EG(exception)) {
354+
goto failure;
355+
}
356+
}
357+
}
350358
ret_constant = &c->value;
351359
}
352360
}

Zend/zend_vm_def.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5983,6 +5983,17 @@ ZEND_VM_HANDLER(181, ZEND_FETCH_CLASS_CONSTANT, VAR|CONST|UNUSED|CLASS_FETCH, CO
59835983
HANDLE_EXCEPTION();
59845984
}
59855985

5986+
bool is_constant_deprecated = ZEND_CLASS_CONST_FLAGS(c) & ZEND_ACC_DEPRECATED;
5987+
if (UNEXPECTED(is_constant_deprecated)) {
5988+
zend_error(E_DEPRECATED, "Constant %s::%s is deprecated", ZSTR_VAL(ce->name), ZSTR_VAL(constant_name));
5989+
5990+
if (EG(exception)) {
5991+
ZVAL_UNDEF(EX_VAR(opline->result.var));
5992+
FREE_OP2();
5993+
HANDLE_EXCEPTION();
5994+
}
5995+
}
5996+
59865997
value = &c->value;
59875998
// Enums require loading of all class constants to build the backed enum table
59885999
if (ce->ce_flags & ZEND_ACC_ENUM && ce->enum_backing_type != IS_UNDEF && ce->type == ZEND_USER_CLASS && !(ce->ce_flags & ZEND_ACC_CONSTANTS_UPDATED)) {
@@ -5999,7 +6010,7 @@ ZEND_VM_HANDLER(181, ZEND_FETCH_CLASS_CONSTANT, VAR|CONST|UNUSED|CLASS_FETCH, CO
59996010
HANDLE_EXCEPTION();
60006011
}
60016012
}
6002-
if (OP2_TYPE == IS_CONST) {
6013+
if (OP2_TYPE == IS_CONST && !is_constant_deprecated) {
60036014
CACHE_POLYMORPHIC_PTR(opline->extended_value, ce, value);
60046015
}
60056016
} else {

Zend/zend_vm_execute.h

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

0 commit comments

Comments
 (0)