Skip to content

Commit aa3f513

Browse files
TysonAndrenikic
authored andcommitted
Add ast\flags\PARENTHESIZED_CONDITIONAL for php 7.4
This will make it easier for static analyzers to warn about the lack of parenthesis, to format code from ASTs, etc.
1 parent 5ed5931 commit aa3f513

File tree

5 files changed

+67
-2
lines changed

5 files changed

+67
-2
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,9 @@ ast\flags\ARRAY_ELEM_REF
345345
346346
// Used by ast\AST_DIM (combinable), since PHP 7.4
347347
ast\flags\DIM_ALTERNATIVE_SYNTAX
348+
349+
// Used by ast\AST_CONDITIONAL (combinable), since PHP 7.4
350+
ast\flags\PARENTHESIZED_CONDITIONAL
348351
```
349352

350353
AST node kinds

ast.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858

5959
#if PHP_VERSION_ID < 70400
6060
# define ZEND_DIM_ALTERNATIVE_SYNTAX (1<<1)
61+
# define ZEND_PARENTHESIZED_CONDITIONAL 1
6162
#endif
6263

6364
/* This contains state of the ast Node creator. */
@@ -242,6 +243,12 @@ static const char *dim_flags[] = {
242243
NULL
243244
};
244245

246+
// Flags of AST_CONDITIONAL are marked as combinable in case any other flags get added in the future.
247+
static const char *conditional_flags[] = {
248+
AST_FLAG(PARENTHESIZED_CONDITIONAL),
249+
NULL
250+
};
251+
245252
static const ast_flag_info flag_info[] = {
246253
{ AST_NAME, 0, name_flags },
247254
{ ZEND_AST_CLASS, 0, class_flags },
@@ -268,6 +275,7 @@ static const ast_flag_info flag_info[] = {
268275
{ ZEND_AST_CLASS_CONST_DECL, 1, visibility_flags },
269276
{ ZEND_AST_TRAIT_ALIAS, 1, modifier_flags },
270277
{ ZEND_AST_DIM, 1, dim_flags },
278+
{ ZEND_AST_CONDITIONAL, 1, conditional_flags },
271279
};
272280

273281
static inline void ast_update_property(zval *object, zend_string *name, zval *value, void **cache_slot) {
@@ -330,7 +338,7 @@ static inline zend_bool ast_kind_uses_attr(zend_ast_kind kind) {
330338
|| kind == ZEND_AST_PROP_GROUP
331339
|| kind == ZEND_AST_GROUP_USE || kind == ZEND_AST_USE_ELEM
332340
|| kind == AST_NAME || kind == AST_CLOSURE_VAR || kind == ZEND_AST_CLASS_CONST_DECL
333-
|| kind == ZEND_AST_ARRAY || kind == ZEND_AST_DIM;
341+
|| kind == ZEND_AST_ARRAY || kind == ZEND_AST_DIM || kind == ZEND_AST_CONDITIONAL;
334342
}
335343

336344
static inline zend_bool ast_kind_is_decl(zend_ast_kind kind) {
@@ -1338,6 +1346,8 @@ PHP_MINIT_FUNCTION(ast) {
13381346

13391347
ast_register_flag_constant("DIM_ALTERNATIVE_SYNTAX", ZEND_DIM_ALTERNATIVE_SYNTAX);
13401348

1349+
ast_register_flag_constant("PARENTHESIZED_CONDITIONAL", ZEND_PARENTHESIZED_CONDITIONAL);
1350+
13411351
INIT_CLASS_ENTRY(tmp_ce, "ast\\Node", ast_node_functions);
13421352
ast_node_ce = zend_register_internal_class(&tmp_ce);
13431353
ast_declare_property(ast_node_ce, AST_STR(str_kind), &zv_null);

package.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
<notes>
2626
- Fix build error in php 7.4.0alpha3.
2727
- Add `DIM_ALTERNATIVE_SYNTAX` as a flag of `AST_DIM` for `$x{'offset'}` (for php 7.4+)
28+
- Add `PARENTHESIZED_CONDITIONAL` as a flag of `AST_CONDITIONAL` for `($a ? $b : $c)` (for php 7.4+)
2829
- Bugfix: Make `ast\kind_uses_flags(ast\AST_ARROW_FUNC)` true in php 7.3 and lower.
2930
</notes>
3031
<contents>
@@ -85,6 +86,7 @@
8586
<file name="php74_ast_assign_coalesce.phpt" role="test" />
8687
<file name="php74_dim_alternative_syntax.phpt" role="test" />
8788
<file name="php74_ordinary_class.phpt" role="test" />
89+
<file name="php74_parenthesized_conditional.phpt" role="test" />
8890
<file name="php74_type_hints.phpt" role="test" />
8991
<file name="prop_doc_comments.phpt" role="test" />
9092
<file name="short_arrow_function.phpt" role="test" />

tests/metadata.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ AST_TRAIT_ALIAS: (combinable) [MODIFIER_PUBLIC, MODIFIER_PROTECTED, MODIFIER_PRI
112112
AST_GROUP_USE: [USE_NORMAL, USE_FUNCTION, USE_CONST]
113113
AST_METHOD_CALL: []
114114
AST_STATIC_CALL: []
115-
AST_CONDITIONAL: []
115+
AST_CONDITIONAL: (combinable) [PARENTHESIZED_CONDITIONAL]
116116
AST_TRY: []
117117
AST_CATCH: []
118118
AST_PARAM: (combinable) [PARAM_REF, PARAM_VARIADIC]
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
--TEST--
2+
parenthesized conditionals in PHP 7.4
3+
--SKIPIF--
4+
<?php if (PHP_VERSION_ID < 70400) die('skip PHP >= 7.4 only'); ?>
5+
--FILE--
6+
<?php
7+
8+
require __DIR__ . '/../util.php';
9+
10+
$code = <<<'PHP'
11+
<?php
12+
return $a ? $b : $c ? $d : $e;
13+
return $a ? $b : ($c ? $d : $e);
14+
PHP;
15+
16+
$node = ast\parse_code($code, $version=70);
17+
echo ast_dump($node), "\n";
18+
?>
19+
--EXPECT--
20+
AST_STMT_LIST
21+
0: AST_RETURN
22+
expr: AST_CONDITIONAL
23+
flags: 0
24+
cond: AST_CONDITIONAL
25+
flags: 0
26+
cond: AST_VAR
27+
name: "a"
28+
true: AST_VAR
29+
name: "b"
30+
false: AST_VAR
31+
name: "c"
32+
true: AST_VAR
33+
name: "d"
34+
false: AST_VAR
35+
name: "e"
36+
1: AST_RETURN
37+
expr: AST_CONDITIONAL
38+
flags: 0
39+
cond: AST_VAR
40+
name: "a"
41+
true: AST_VAR
42+
name: "b"
43+
false: AST_CONDITIONAL
44+
flags: PARENTHESIZED_CONDITIONAL (1)
45+
cond: AST_VAR
46+
name: "c"
47+
true: AST_VAR
48+
name: "d"
49+
false: AST_VAR
50+
name: "e"

0 commit comments

Comments
 (0)