Skip to content

Commit 1b23675

Browse files
committed
Adding functionality for typed arrays using custom interface syntax.
1 parent 3626e2d commit 1b23675

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

Zend/zend_language_parser.y

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -629,8 +629,12 @@ interface_declaration_statement:
629629
T_INTERFACE { $<num>$ = CG(zend_lineno); }
630630
T_STRING interface_extends_list backup_doc_comment '{' class_statement_list '}'
631631
{ $$ = zend_ast_create_decl(ZEND_AST_CLASS, ZEND_ACC_INTERFACE, $<num>2, $5, zend_ast_get_str($3), NULL, $4, $7, NULL, NULL); }
632+
| T_INTERFACE { $<num>$ = CG(zend_lineno); }
633+
T_STRING interface_extends_list backup_doc_comment '[' array_statement_list ']'
634+
{ $$ = zend_ast_create_decl(ZEND_AST_CLASS, ZEND_ACC_INTERFACE, $<num>2, $4, zend_ast_get_str($2), NULL, $3, $6, NULL, NULL); }
632635
;
633636

637+
634638
enum_declaration_statement:
635639
T_ENUM { $<num>$ = CG(zend_lineno); }
636640
T_STRING enum_backing_type implements_list backup_doc_comment '{' class_statement_list '}'
@@ -850,12 +854,18 @@ intersection_type:
850854
* to avoid conflicts with "static" modifier for properties. */
851855

852856
type_expr_without_static:
853-
type_without_static { $$ = $1; }
857+
type_without_static { $$ = $1; }
854858
| '?' type_without_static { $$ = $2; $$->attr |= ZEND_TYPE_NULLABLE; }
855859
| union_type_without_static { $$ = $1; }
856860
| intersection_type_without_static { $$ = $1; }
857861
;
858862

863+
type_expr_without_static_or_nullable:
864+
type_without_static { $$ = $1; }
865+
| union_type_without_static { $$ = $1; }
866+
| intersection_type_without_static { $$ = $1; }
867+
;
868+
859869
type_without_static:
860870
T_ARRAY { $$ = zend_ast_create_ex(ZEND_AST_TYPE, IS_ARRAY); }
861871
| T_CALLABLE { $$ = zend_ast_create_ex(ZEND_AST_TYPE, IS_CALLABLE); }
@@ -934,6 +944,19 @@ class_statement_list:
934944
{ $$ = zend_ast_create_list(0, ZEND_AST_STMT_LIST); }
935945
;
936946

947+
array_statement_list:
948+
%empty
949+
{ $$ = zend_ast_create_list(0, ZEND_AST_ARRAY); }
950+
| typed_array_element
951+
{ $$ = zend_ast_create_list(1, ZEND_AST_ARRAY, $1); }
952+
| array_statement_list ',' typed_array_element
953+
{ $$ = zend_ast_list_add($1, $3); }
954+
;
955+
956+
typed_array_element:
957+
T_STRING T_DOUBLE_ARROW type_expr_without_static
958+
{ $$ = zend_ast_create(ZEND_AST_ARRAY_ELEM, $3, zend_ast_zval_create_string($1)); }
959+
;
937960

938961
attributed_class_statement:
939962
property_modifiers optional_type_without_static property_list ';'
@@ -1221,6 +1244,10 @@ expr:
12211244
{ $$ = zend_ast_create_binary_op(ZEND_SPACESHIP, $1, $3); }
12221245
| expr T_INSTANCEOF class_name_reference
12231246
{ $$ = zend_ast_create(ZEND_AST_INSTANCEOF, $1, $3); }
1247+
| type_without_static expr
1248+
{ $$ = zend_ast_create(ZEND_AST_CAST, $2, $4); }
1249+
| '(' type_expr_without_static_or_nullable ')' expr
1250+
{ $$ = zend_ast_create(ZEND_AST_CAST, $2, $4); }
12241251
| '(' expr ')' {
12251252
$$ = $2;
12261253
if ($$->kind == ZEND_AST_CONDITIONAL) $$->attr = ZEND_PARENTHESIZED_CONDITIONAL;

tests/array_types.phpt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
--TEST--
2+
Test for array structures within interfaces
3+
--FILE--
4+
<?php
5+
interface iArrayA [
6+
'a' => string
7+
]
8+
9+
interface iArrayB extends iArrayA [
10+
'b' => string,
11+
'c' => ?string
12+
];
13+
14+
$array = (iArrayA & iArrayB) [
15+
'a' => 'hello',
16+
'b' => 'world',
17+
'c' => null
18+
];
19+
20+
var_dump($array);
21+
?>
22+
--EXPECT--
23+
array(3) {
24+
["a"]=>
25+
string(5) "hello"
26+
["b"]=>
27+
string(5) "world"
28+
["c"]=>
29+
NULL
30+
}

0 commit comments

Comments
 (0)