Skip to content

Commit b68c77d

Browse files
committed
feat: complete tests
1 parent 0ff2216 commit b68c77d

File tree

6 files changed

+153
-5
lines changed

6 files changed

+153
-5
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
--TEST--
2+
Pass short ctor parameters
3+
--FILE--
4+
<?php
5+
6+
try {
7+
assert(false && function () {
8+
9+
abstract class Family($string);
10+
11+
class Child1($string) extends Family () {}
12+
class Child2($string) extends Family (555) {}
13+
14+
});
15+
} catch (Throwable $e) {
16+
echo $e->getMessage(), PHP_EOL;
17+
}
18+
19+
?>
20+
--EXPECTF--
21+
assert(false && function () {
22+
abstract class Family {
23+
public function __construct($string) {
24+
}
25+
26+
}
27+
28+
class Child1 extends Family {
29+
public function __construct($string) {
30+
parent::__construct();
31+
}
32+
33+
}
34+
35+
class Child2 extends Family {
36+
public function __construct($string) {
37+
parent::__construct(555);
38+
}
39+
40+
}
41+
42+
})
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
--TEST--
2+
Pass short ctor parameters
3+
--FILE--
4+
<?php
5+
6+
try {
7+
assert(false && function () {
8+
9+
abstract class Family(protected $protected);
10+
11+
class Child1(public $string) extends Family ($string) {}
12+
13+
14+
});
15+
} catch (Throwable $e) {
16+
echo $e->getMessage(), PHP_EOL;
17+
}
18+
19+
?>
20+
--EXPECTF--
21+
assert(false && function () {
22+
abstract class Family {
23+
public function __construct(protected $protected) {
24+
}
25+
26+
}
27+
28+
class Child1 extends Family {
29+
public function __construct(public $string) {
30+
parent::__construct($string);
31+
}
32+
33+
}
34+
35+
})

Zend/tests/ctor/short-ctor-const.phpt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--TEST--
2+
Pass short ctor parameters
3+
--FILE--
4+
<?php
5+
6+
abstract class Family(protected $string);
7+
8+
class Child1($string) extends Family (self::VALUE) {
9+
const VALUE = 123;
10+
}
11+
12+
var_dump(new Child1("test"));
13+
?>
14+
--EXPECTF--
15+
object(Child1)#1 (1) {
16+
["string":protected]=>
17+
int(123)
18+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
--TEST--
2+
Pass short ctor parameters
3+
--FILE--
4+
<?php
5+
6+
abstract class Family(protected $string);
7+
8+
class Child1($string) extends Family ($string) {}
9+
10+
var_dump(new Child1("test"));
11+
?>
12+
--EXPECTF--
13+
object(Child1)#1 (1) {
14+
["string":protected]=>
15+
string(4) "test"
16+
}

Zend/tests/ctor/short-ctor-prop.phpt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
--TEST--
2+
Pass short ctor parameters
3+
--FILE--
4+
<?php
5+
6+
abstract class Family(protected $string);
7+
8+
class Child1($string) extends Family ($this->internal_string) {
9+
private $internal_string = "internal";
10+
}
11+
12+
var_dump(new Child1("test"));
13+
?>
14+
--EXPECTF--
15+
object(Child1)#1 (2) {
16+
["string":protected]=>
17+
string(8) "internal"
18+
["internal_string":"Child1":private]=>
19+
string(8) "internal"
20+
}

Zend/zend_language_parser.y

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ static YYSIZE_T zend_yytnamerr(char*, const char*);
3636
#define YYMALLOC malloc
3737
#define YYFREE free
3838
#endif
39+
void* temp;
3940
}
4041

4142
%code requires {
@@ -252,7 +253,7 @@ static YYSIZE_T zend_yytnamerr(char*, const char*);
252253
%token T_ERROR
253254

254255
%type <ast> top_statement namespace_name name statement function_declaration_statement
255-
%type <ast> class_declaration_statement class_body_statement class_short_ctor trait_declaration_statement legacy_namespace_name
256+
%type <ast> class_declaration_statement class_body_statement parent_ctr_call class_short_ctor trait_declaration_statement legacy_namespace_name
256257
%type <ast> interface_declaration_statement interface_extends_list
257258
%type <ast> group_use_declaration inline_use_declarations inline_use_declaration
258259
%type <ast> mixed_group_use_declaration use_declaration unprefixed_use_declaration
@@ -605,21 +606,37 @@ class_declaration_statement:
605606
class_modifiers_optional T_CLASS { $<num>$ = CG(zend_lineno); } T_STRING
606607
class_short_ctor
607608
extends_from implements_list backup_doc_comment class_body_statement
608-
{
609-
zend_ast* stmts = zend_ast_create_list(0, ZEND_AST_STMT_LIST, $5, $9);
609+
{
610+
zend_ast_decl *ctor = $5;
611+
if (ctor && ctor->child[2] && temp) {
612+
ctor->child[2] = zend_ast_list_add(ctor->child[2], temp);
613+
}
614+
zend_ast* stmts = zend_ast_create_list(2, ZEND_AST_STMT_LIST, $5, $9);
610615
$$ = zend_ast_create_decl(ZEND_AST_CLASS, $1, $<num>3, $8, zend_ast_get_str($4), $6, $7, stmts, NULL, NULL); }
611616
;
612617

613618
class_short_ctor:
614619
'(' parameter_list ')'
615620
{ $$ = 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);; }
621+
ZSTR_KNOWN(ZEND_STR_CTOR), $2, NULL, zend_ast_create_list(0, ZEND_AST_STMT_LIST), NULL, NULL); }
617622
| %empty { $$ = NULL; }
618623
;
619624

620625
class_body_statement:
621626
'{' class_statement_list '}' { $$ = $2; }
622627
| ';' { $$ = NULL; }
628+
;
629+
630+
parent_ctr_call:
631+
argument_list
632+
{
633+
zval zv; ZVAL_INTERNED_STR(&zv, ZSTR_KNOWN(ZEND_STR_PARENT));
634+
$$ = zend_ast_create(ZEND_AST_STATIC_CALL,
635+
zend_ast_create_zval_ex(&zv, ZEND_NAME_NOT_FQ),
636+
zend_ast_create_zval_from_str(ZSTR_KNOWN(ZEND_STR_CTOR)),
637+
$1); }
638+
| %empty { $$ = NULL; }
639+
;
623640

624641
class_modifiers_optional:
625642
%empty { $$ = 0; }
@@ -685,7 +702,7 @@ enum_case_expr:
685702

686703
extends_from:
687704
%empty { $$ = NULL; }
688-
| T_EXTENDS class_name { $$ = $2; }
705+
| T_EXTENDS class_name parent_ctr_call { $$ = $2; temp = $3; }
689706
;
690707

691708
interface_extends_list:

0 commit comments

Comments
 (0)