Skip to content

Commit 49ebd1f

Browse files
authored
Support PARAM_MODIFIER_PUBLIC on AST_PARAM nodes (nikic#168)
Fixes nikic#167
1 parent 9307f8a commit 49ebd1f

File tree

6 files changed

+73
-57
lines changed

6 files changed

+73
-57
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -256,9 +256,9 @@ ast\flags\CLASS_ANONYMOUS
256256
// Used by ast\AST_PARAM (combinable)
257257
ast\flags\PARAM_REF
258258
ast\flags\PARAM_VARIADIC
259-
ast\flags\MODIFIER_PUBLIC (only in php 8.0+)
260-
ast\flags\MODIFIER_PROTECTED (only in php 8.0+)
261-
ast\flags\MODIFIER_PRIVATE (only in php 8.0+)
259+
ast\flags\PARAM_MODIFIER_PUBLIC (available since 1.0.8, same as ast\flags\MODIFIER_* in PHP >= 8.0)
260+
ast\flags\PARAM_MODIFIER_PROTECTED (available since 1.0.8)
261+
ast\flags\PARAM_MODIFIER_PRIVATE (available since 1.0.8)
262262
263263
// Used by ast\AST_TYPE (exclusive)
264264
ast\flags\TYPE_ARRAY

ast.c

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,14 @@
6565
#if PHP_VERSION_ID < 80000
6666
# define IS_STATIC 20
6767
# define IS_MIXED 21
68+
/* In PHP 7.0-7.4, PARAM_REF and PARAM_VARIADIC were 1 and 2. */
69+
# define PARAM_MODIFIER_PUBLIC (1 << 2)
70+
# define PARAM_MODIFIER_PROTECTED (1 << 3)
71+
# define PARAM_MODIFIER_PRIVATE (1 << 4)
72+
#else
73+
# define PARAM_MODIFIER_PUBLIC ZEND_ACC_PUBLIC
74+
# define PARAM_MODIFIER_PROTECTED ZEND_ACC_PROTECTED
75+
# define PARAM_MODIFIER_PRIVATE ZEND_ACC_PRIVATE
6876
#endif
6977

7078
/* This contains state of the ast Node creator. */
@@ -107,11 +115,9 @@ static const char *class_flags[] = {
107115
static const char *param_flags[] = {
108116
AST_FLAG(PARAM_REF),
109117
AST_FLAG(PARAM_VARIADIC),
110-
#if PHP_VERSION_ID >= 80000
111-
AST_FLAG(MODIFIER_PUBLIC),
112-
AST_FLAG(MODIFIER_PROTECTED),
113-
AST_FLAG(MODIFIER_PRIVATE),
114-
#endif
118+
AST_FLAG(PARAM_MODIFIER_PUBLIC),
119+
AST_FLAG(PARAM_MODIFIER_PROTECTED),
120+
AST_FLAG(PARAM_MODIFIER_PRIVATE),
115121
NULL
116122
};
117123

@@ -1368,6 +1374,10 @@ PHP_MINIT_FUNCTION(ast) {
13681374
ast_register_flag_constant("MODIFIER_ABSTRACT", ZEND_ACC_ABSTRACT);
13691375
ast_register_flag_constant("MODIFIER_FINAL", ZEND_ACC_FINAL);
13701376

1377+
ast_register_flag_constant("PARAM_MODIFIER_PUBLIC", PARAM_MODIFIER_PUBLIC);
1378+
ast_register_flag_constant("PARAM_MODIFIER_PROTECTED", PARAM_MODIFIER_PROTECTED);
1379+
ast_register_flag_constant("PARAM_MODIFIER_PRIVATE", PARAM_MODIFIER_PRIVATE);
1380+
13711381
ast_register_flag_constant("RETURNS_REF", ZEND_ACC_RETURN_REFERENCE);
13721382
ast_register_flag_constant("FUNC_RETURNS_REF", ZEND_ACC_RETURN_REFERENCE);
13731383
ast_register_flag_constant("FUNC_GENERATOR", ZEND_ACC_GENERATOR);

ast_stub.php

Lines changed: 47 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -66,45 +66,45 @@
6666
const AST_BREAK = 286;
6767
const AST_CONTINUE = 287;
6868
const AST_CLASS_NAME = 276;
69-
const AST_CLASS_CONST_GROUP = 545;
69+
const AST_CLASS_CONST_GROUP = 546;
7070
const AST_DIM = 512;
7171
const AST_PROP = 513;
72-
const AST_STATIC_PROP = 514;
73-
const AST_CALL = 515;
74-
const AST_CLASS_CONST = 516;
75-
const AST_ASSIGN = 517;
76-
const AST_ASSIGN_REF = 518;
77-
const AST_ASSIGN_OP = 519;
78-
const AST_BINARY_OP = 520;
79-
const AST_ARRAY_ELEM = 525;
80-
const AST_NEW = 526;
81-
const AST_INSTANCEOF = 527;
82-
const AST_YIELD = 528;
83-
const AST_STATIC = 531;
84-
const AST_WHILE = 532;
85-
const AST_DO_WHILE = 533;
86-
const AST_IF_ELEM = 534;
87-
const AST_SWITCH = 535;
88-
const AST_SWITCH_CASE = 536;
89-
const AST_DECLARE = 537;
90-
const AST_PROP_ELEM = 774;
91-
const AST_PROP_GROUP = 773;
92-
const AST_CONST_ELEM = 775;
93-
const AST_USE_TRAIT = 538;
94-
const AST_TRAIT_PRECEDENCE = 539;
95-
const AST_METHOD_REFERENCE = 540;
96-
const AST_NAMESPACE = 541;
97-
const AST_USE_ELEM = 542;
98-
const AST_TRAIT_ALIAS = 543;
99-
const AST_GROUP_USE = 544;
100-
const AST_ATTRIBUTE = 546;
101-
const AST_MATCH = 547;
102-
const AST_MATCH_ARM = 548;
72+
const AST_STATIC_PROP = 515;
73+
const AST_CALL = 516;
74+
const AST_CLASS_CONST = 517;
75+
const AST_ASSIGN = 518;
76+
const AST_ASSIGN_REF = 519;
77+
const AST_ASSIGN_OP = 520;
78+
const AST_BINARY_OP = 521;
79+
const AST_ARRAY_ELEM = 526;
80+
const AST_NEW = 527;
81+
const AST_INSTANCEOF = 528;
82+
const AST_YIELD = 529;
83+
const AST_STATIC = 532;
84+
const AST_WHILE = 533;
85+
const AST_DO_WHILE = 534;
86+
const AST_IF_ELEM = 535;
87+
const AST_SWITCH = 536;
88+
const AST_SWITCH_CASE = 537;
89+
const AST_DECLARE = 538;
90+
const AST_PROP_ELEM = 775;
91+
const AST_PROP_GROUP = 774;
92+
const AST_CONST_ELEM = 776;
93+
const AST_USE_TRAIT = 539;
94+
const AST_TRAIT_PRECEDENCE = 540;
95+
const AST_METHOD_REFERENCE = 541;
96+
const AST_NAMESPACE = 542;
97+
const AST_USE_ELEM = 543;
98+
const AST_TRAIT_ALIAS = 544;
99+
const AST_GROUP_USE = 545;
100+
const AST_ATTRIBUTE = 547;
101+
const AST_MATCH = 548;
102+
const AST_MATCH_ARM = 549;
103103
const AST_METHOD_CALL = 768;
104-
const AST_STATIC_CALL = 769;
105-
const AST_CONDITIONAL = 770;
106-
const AST_TRY = 771;
107-
const AST_CATCH = 772;
104+
const AST_STATIC_CALL = 770;
105+
const AST_CONDITIONAL = 771;
106+
const AST_TRY = 772;
107+
const AST_CATCH = 773;
108108
const AST_FOR = 1024;
109109
const AST_FOREACH = 1025;
110110
const AST_PARAM = 1280;
@@ -121,6 +121,9 @@
121121
const MODIFIER_STATIC = 16;
122122
const MODIFIER_ABSTRACT = 64;
123123
const MODIFIER_FINAL = 32;
124+
const PARAM_MODIFIER_PUBLIC = 1;
125+
const PARAM_MODIFIER_PROTECTED = 2;
126+
const PARAM_MODIFIER_PRIVATE = 4;
124127
const RETURNS_REF = 4096;
125128
const FUNC_RETURNS_REF = 4096;
126129
const FUNC_GENERATOR = 16777216;
@@ -184,14 +187,14 @@
184187
const USE_NORMAL = 1;
185188
const USE_FUNCTION = 2;
186189
const USE_CONST = 4;
187-
const MAGIC_LINE = 372;
188-
const MAGIC_FILE = 373;
189-
const MAGIC_DIR = 374;
190-
const MAGIC_NAMESPACE = 379;
191-
const MAGIC_FUNCTION = 378;
192-
const MAGIC_METHOD = 377;
193-
const MAGIC_CLASS = 375;
194-
const MAGIC_TRAIT = 376;
190+
const MAGIC_LINE = 375;
191+
const MAGIC_FILE = 376;
192+
const MAGIC_DIR = 377;
193+
const MAGIC_NAMESPACE = 382;
194+
const MAGIC_FUNCTION = 381;
195+
const MAGIC_METHOD = 380;
196+
const MAGIC_CLASS = 378;
197+
const MAGIC_TRAIT = 379;
195198
const ARRAY_SYNTAX_LIST = 1;
196199
const ARRAY_SYNTAX_LONG = 2;
197200
const ARRAY_SYNTAX_SHORT = 3;

package.xml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@
2929
</stability>
3030
<license uri="https://github.com/nikic/php-ast/blob/master/LICENSE">BSD-3-Clause</license>
3131
<notes>
32-
- TBD
32+
- Recommend using the new constant `ast\flags\PARAM_MODIFIER_*` when checking if parameters use constructor property promotion.
33+
The values of `ast\flags\MODIFIER_*` and `ast\flags\PARAM_VARIADIC` had some overlap in some php 7 versions.
34+
The new constants will have the same values as `ast\flags\MODIFIER_*` in PHP 8.0+, but different values in PHP 7
35+
(and these flags will never be set in php 7).
3336
</notes>
3437
<contents>
3538
<dir name="/">

tests/metadata.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,4 +126,4 @@ AST_TRY: []
126126
AST_CATCH: []
127127
AST_FOR: []
128128
AST_FOREACH: []
129-
AST_PARAM: (combinable) [PARAM_REF, PARAM_VARIADIC%S]
129+
AST_PARAM: (combinable) [PARAM_REF, PARAM_VARIADIC, PARAM_MODIFIER_PUBLIC, PARAM_MODIFIER_PROTECTED, PARAM_MODIFIER_PRIVATE]

tests/php80_promotion.phpt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,13 @@ AST_STMT_LIST
3838
docComment: "/** Doc comment for __construct */"
3939
params: AST_PARAM_LIST
4040
0: AST_PARAM
41-
flags: MODIFIER_PUBLIC (%d)
41+
flags: PARAM_MODIFIER_PUBLIC (%d)
4242
type: AST_TYPE
4343
flags: TYPE_LONG (4)
4444
name: "a"
4545
default: null
4646
1: AST_PARAM
47-
flags: PARAM_REF | MODIFIER_PRIVATE (%d)
47+
flags: PARAM_REF | PARAM_MODIFIER_PRIVATE (%d)
4848
type: AST_NAME
4949
flags: NAME_NOT_FQ (1)
5050
name: "stdClass"
@@ -54,7 +54,7 @@ AST_STMT_LIST
5454
flags: NAME_NOT_FQ (1)
5555
name: "null"
5656
2: AST_PARAM
57-
flags: MODIFIER_PROTECTED (%d)
57+
flags: PARAM_MODIFIER_PROTECTED (%d)
5858
type: AST_TYPE
5959
flags: TYPE_ITERABLE (13)
6060
name: "c"

0 commit comments

Comments
 (0)