Skip to content

Commit e460595

Browse files
authored
Add tests, flag constants for php 8.2 readonly classes (#228)
* Add tests, flag constants for php 8.2 readonly classes * Address review comments
1 parent 91f5946 commit e460595

File tree

8 files changed

+46
-5
lines changed

8 files changed

+46
-5
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,8 @@ ast\flags\CLASS_FINAL
253253
ast\flags\CLASS_TRAIT
254254
ast\flags\CLASS_INTERFACE
255255
ast\flags\CLASS_ANONYMOUS
256-
ast\flags\CLASS_ENUM
256+
ast\flags\CLASS_ENUM // php 8.1 enums
257+
ast\flags\CLASS_READONLY // php 8.2 readonly classes
257258
258259
// Used by ast\AST_PARAM (combinable)
259260
ast\flags\PARAM_REF

ast.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ static const char *class_flags[] = {
147147
AST_FLAG(CLASS_INTERFACE),
148148
AST_FLAG(CLASS_ANONYMOUS),
149149
AST_FLAG(CLASS_ENUM),
150+
AST_FLAG(CLASS_READONLY),
150151
NULL
151152
};
152153

@@ -842,7 +843,6 @@ static void ast_fill_children_ht(HashTable *ht, zend_ast *ast, ast_state_info_t
842843
}
843844
}
844845
#endif
845-
846846
ZVAL_LONG(&id_zval, state->declIdCounter);
847847
state->declIdCounter++;
848848
zend_hash_add_new(ht, AST_STR(str___declId), &id_zval);
@@ -990,8 +990,14 @@ static void ast_to_zval(zval *zv, zend_ast *ast, ast_state_info_t *state) {
990990

991991
if (ast_kind_is_decl(ast->kind)) {
992992
zend_ast_decl *decl = (zend_ast_decl *) ast;
993+
uint32_t flags = decl->flags;
994+
#if PHP_VERSION_ID >= 80200
995+
if (ast->kind == ZEND_AST_CLASS) {
996+
flags &= ~ZEND_ACC_NO_DYNAMIC_PROPERTIES;
997+
}
998+
#endif
993999

994-
AST_NODE_SET_PROP_FLAGS(obj, decl->flags);
1000+
AST_NODE_SET_PROP_FLAGS(obj, flags);
9951001

9961002
// This is an undeclared dynamic property and has no cache slot.
9971003
ast_update_property_long(zv, AST_STR(str_endLineno), decl->end_lineno);
@@ -1398,6 +1404,7 @@ PHP_MINIT_FUNCTION(ast) {
13981404
ast_register_flag_constant("CLASS_INTERFACE", ZEND_ACC_INTERFACE);
13991405
ast_register_flag_constant("CLASS_ANONYMOUS", ZEND_ACC_ANON_CLASS);
14001406
ast_register_flag_constant("CLASS_ENUM", ZEND_ACC_ENUM);
1407+
ast_register_flag_constant("CLASS_READONLY", ZEND_ACC_READONLY_CLASS);
14011408

14021409
ast_register_flag_constant("PARAM_REF", ZEND_PARAM_REF);
14031410
ast_register_flag_constant("PARAM_VARIADIC", ZEND_PARAM_VARIADIC);

ast_stub.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@
143143
const CLASS_INTERFACE = 1;
144144
const CLASS_ANONYMOUS = 4;
145145
const CLASS_ENUM = 268435456;
146+
const CLASS_READONLY = 65536;
146147
const PARAM_REF = 8;
147148
const PARAM_VARIADIC = 16;
148149
const TYPE_NULL = 1;

package.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
- Change documentation files to properly namespace attributes as `#[\AllowDynamicProperties]`. The reflection attributes are already correct.
3333
- Deprecated AST versions 50 and 60.
3434
- Drop support for php <= 7.1
35+
- Add support for php 8.2 readonly classes. Add `ast\flags\CLASS_READONLY` constant.
3536
</notes>
3637
<contents>
3738
<dir name="/">

php_ast.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ extern ast_str_globals str_globals;
8181
# define ZEND_AST_TYPE_INTERSECTION ((1 << (ZEND_AST_IS_LIST_SHIFT + 1)) - 6)
8282
#endif
8383

84+
#if PHP_VERSION_ID < 80200
85+
# define ZEND_ACC_READONLY_CLASS (1 << 23)
86+
#endif
87+
8488
/* Pretend it still exists */
8589
# define ZEND_AST_LIST ((1 << (ZEND_AST_IS_LIST_SHIFT + 1)) - 1)
8690

tests/metadata.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ AST_FUNC_DECL: (combinable) [MODIFIER_PUBLIC, MODIFIER_PROTECTED, MODIFIER_PRIVA
5555
AST_CLOSURE: (combinable) [MODIFIER_PUBLIC, MODIFIER_PROTECTED, MODIFIER_PRIVATE, MODIFIER_STATIC, MODIFIER_ABSTRACT, MODIFIER_FINAL, MODIFIER_READONLY, FUNC_RETURNS_REF, FUNC_GENERATOR]
5656
AST_METHOD: (combinable) [MODIFIER_PUBLIC, MODIFIER_PROTECTED, MODIFIER_PRIVATE, MODIFIER_STATIC, MODIFIER_ABSTRACT, MODIFIER_FINAL, MODIFIER_READONLY, FUNC_RETURNS_REF, FUNC_GENERATOR]
5757
AST_ARROW_FUNC: (combinable) [MODIFIER_PUBLIC, MODIFIER_PROTECTED, MODIFIER_PRIVATE, MODIFIER_STATIC, MODIFIER_ABSTRACT, MODIFIER_FINAL, MODIFIER_READONLY, FUNC_RETURNS_REF, FUNC_GENERATOR]
58-
AST_CLASS: (combinable) [CLASS_ABSTRACT, CLASS_FINAL, CLASS_TRAIT, CLASS_INTERFACE, CLASS_ANONYMOUS, CLASS_ENUM]
58+
AST_CLASS: (combinable) [CLASS_ABSTRACT, CLASS_FINAL, CLASS_TRAIT, CLASS_INTERFACE, CLASS_ANONYMOUS, CLASS_ENUM, CLASS_READONLY]
5959
AST_MAGIC_CONST: [MAGIC_LINE, MAGIC_FILE, MAGIC_DIR, MAGIC_NAMESPACE, MAGIC_FUNCTION, MAGIC_METHOD, MAGIC_CLASS, MAGIC_TRAIT]
6060
AST_TYPE: [TYPE_NULL, TYPE_FALSE, TYPE_BOOL, TYPE_LONG, TYPE_DOUBLE, TYPE_STRING, TYPE_ARRAY, TYPE_OBJECT, TYPE_CALLABLE, TYPE_VOID, TYPE_ITERABLE, TYPE_STATIC, TYPE_MIXED, TYPE_NEVER]
6161
AST_CALLABLE_CONVERT: []

tests/php82_metadata.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
--TEST--
22
Dynamic property support in php 8.2+
33
--SKIPIF--
4-
<?php if (!class_exists('AllowDynamicProperties')) die('skip PHP >=8.2 only'); ?>
4+
<?php if (PHP_VERSION_ID < 80200) die('skip PHP >=8.2 only'); ?>
55
--FILE--
66
<?php
77
error_reporting(E_ALL);

tests/php82_readonly_class.phpt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
--TEST--
2+
readonly class support in php 8.2+
3+
--SKIPIF--
4+
<?php if (PHP_VERSION_ID < 80200) die('skip PHP >=8.2 only'); ?>
5+
--FILE--
6+
<?php
7+
require __DIR__ . '/../util.php';
8+
9+
$code = <<<'PHP'
10+
<?php
11+
readonly class X {}
12+
PHP;
13+
14+
$node = ast\parse_code($code, $version=85);
15+
echo ast_dump($node), "\n";
16+
--EXPECTF--
17+
AST_STMT_LIST
18+
0: AST_CLASS
19+
flags: CLASS_READONLY (%d)
20+
name: "X"
21+
docComment: null
22+
extends: null
23+
implements: null
24+
stmts: AST_STMT_LIST
25+
attributes: null
26+
type: null
27+
__declId: 0

0 commit comments

Comments
 (0)