Skip to content

Commit 2075fa4

Browse files
committed
feat: add tests
1 parent 8511641 commit 2075fa4

File tree

5 files changed

+55
-2
lines changed

5 files changed

+55
-2
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
--TEST--
2+
Disallow duplicate short and full ctors
3+
--FILE--
4+
<?php
5+
6+
class DTO (
7+
public int $number,
8+
) {
9+
public function __construct() {}
10+
}
11+
12+
?>
13+
--EXPECTF--
14+
Fatal error: Cannot redeclare DTO::__construct() in %s on line %d
File renamed without changes.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
--TEST--
2+
Pass short ctor parameters
3+
--FILE--
4+
<?php
5+
6+
class Empty0 ();
7+
8+
var_dump(new Empty0());
9+
10+
class DTO (
11+
public int $number,
12+
);
13+
14+
var_dump(new DTO(50));
15+
16+
class Value (int $number);
17+
18+
var_dump(new Value(50));
19+
?>
20+
--EXPECTF--
21+
object(Empty0)#1 (0) {
22+
}
23+
object(DTO)#1 (1) {
24+
["number"]=>
25+
int(50)
26+
}
27+
object(Value)#1 (0) {
28+
}

Zend/zend_language_parser.y

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ static YYSIZE_T zend_yytnamerr(char*, const char*);
252252
%token T_ERROR
253253

254254
%type <ast> top_statement namespace_name name statement function_declaration_statement
255-
%type <ast> class_declaration_statement class_body_statement trait_declaration_statement legacy_namespace_name
255+
%type <ast> class_declaration_statement class_body_statement class_short_ctor trait_declaration_statement legacy_namespace_name
256256
%type <ast> interface_declaration_statement interface_extends_list
257257
%type <ast> group_use_declaration inline_use_declarations inline_use_declaration
258258
%type <ast> mixed_group_use_declaration use_declaration unprefixed_use_declaration
@@ -603,8 +603,18 @@ is_variadic:
603603

604604
class_declaration_statement:
605605
class_modifiers_optional T_CLASS { $<num>$ = CG(zend_lineno); } T_STRING
606+
class_short_ctor
606607
extends_from implements_list backup_doc_comment class_body_statement
607-
{ $$ = zend_ast_create_decl(ZEND_AST_CLASS, $1, $<num>3, $7, zend_ast_get_str($4), $5, $6, $8, NULL, NULL); }
608+
{
609+
zend_ast* stmts = zend_ast_create_list(0, ZEND_AST_STMT_LIST, $5, $9);
610+
$$ = zend_ast_create_decl(ZEND_AST_CLASS, $1, $<num>3, $8, zend_ast_get_str($4), $6, $7, stmts, NULL, NULL); }
611+
;
612+
613+
class_short_ctor:
614+
'(' parameter_list ')'
615+
{ $$ = zend_ast_create_decl(ZEND_AST_METHOD, ZEND_ACC_PUBLIC, CG(zend_lineno), NULL,
616+
ZSTR_KNOWN(ZEND_STR_CTOR), $2, NULL, zend_ast_create_list(0, ZEND_AST_STMT_LIST), NULL, NULL);; }
617+
| %empty { $$ = NULL; }
608618
;
609619

610620
class_body_statement:

Zend/zend_string.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,7 @@ EMPTY_SWITCH_DEFAULT_CASE()
588588
_(ZEND_STR_VALUE, "value") \
589589
_(ZEND_STR_KEY, "key") \
590590
_(ZEND_STR_MAGIC_INVOKE, "__invoke") \
591+
_(ZEND_STR_CTOR, "__construct") \
591592
_(ZEND_STR_PREVIOUS, "previous") \
592593
_(ZEND_STR_CODE, "code") \
593594
_(ZEND_STR_MESSAGE, "message") \

0 commit comments

Comments
 (0)