Skip to content

Commit ee0d1c7

Browse files
committed
Partial named params implementation
1 parent 27ad19c commit ee0d1c7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+5554
-2518
lines changed

Zend/tests/arg_unpack/non_integer_keys.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@ try {
1818

1919
?>
2020
--EXPECT--
21-
Exception: Cannot unpack Traversable with non-integer keys
21+
Exception: Keys must be of type int|string during argument unpacking

Zend/tests/arg_unpack/string_keys.phpt

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,6 @@ set_error_handler(function($errno, $errstr) {
77
var_dump($errstr);
88
});
99

10-
try {
11-
var_dump(...[1, 2, "foo" => 3, 4]);
12-
} catch (Error $ex) {
13-
var_dump($ex->getMessage());
14-
}
1510
try {
1611
var_dump(...new ArrayIterator([1, 2, "foo" => 3, 4]));
1712
} catch (Error $ex) {
@@ -20,5 +15,4 @@ try {
2015

2116
?>
2217
--EXPECT--
23-
string(36) "Cannot unpack array with string keys"
24-
string(42) "Cannot unpack Traversable with string keys"
18+
string(68) "Cannot use positional argument after named argument during unpacking"

Zend/tests/bug43343.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ $foo = 'bar';
88
var_dump(new namespace::$foo);
99
?>
1010
--EXPECTF--
11-
Parse error: syntax error, unexpected token "namespace" in %s on line %d
11+
Parse error: syntax error, unexpected token "namespace", expecting ":" in %s on line %d

Zend/tests/named_params/__call.phpt

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
--TEST--
2+
Check that __invoke() works with named parameters
3+
--FILE--
4+
<?php
5+
6+
class Test {
7+
public function __call(string $method, array $args) {
8+
$this->{'_'.$method}(...$args);
9+
}
10+
11+
public static function __callStatic(string $method, array $args) {
12+
(new static)->{'_'.$method}(...$args);
13+
}
14+
15+
private function _method($a = 'a', $b = 'b') {
16+
echo "a: $a, b: $b\n";
17+
}
18+
}
19+
20+
$obj = new class { public function __toString() { return "STR"; } };
21+
22+
$test = new Test;
23+
$test->method(a: 'A', b: 'B');
24+
$test->method(b: 'B');
25+
$test->method(b: $obj);
26+
Test::method(a: 'A', b: 'B');
27+
Test::method(b: 'B');
28+
Test::method(b: $obj);
29+
30+
?>
31+
--EXPECT--
32+
a: A, b: B
33+
a: a, b: B
34+
a: a, b: STR
35+
a: A, b: B
36+
a: a, b: B
37+
a: a, b: STR

Zend/tests/named_params/__invoke.phpt

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
--TEST--
2+
Check that __invoke() works with named parameters
3+
--FILE--
4+
<?php
5+
6+
class Test {
7+
public function __invoke($a = 'a', $b = 'b') {
8+
echo "a: $a, b: $b\n";
9+
}
10+
}
11+
12+
class Test2 {
13+
public function __invoke($a = 'a', $b = 'b', ...$rest) {
14+
echo "a: $a, b: $b\n";
15+
var_dump($rest);
16+
}
17+
}
18+
19+
$test = new Test;
20+
$test(b: 'B', a: 'A');
21+
$test(b: 'B');
22+
try {
23+
$test(b: 'B', c: 'C');
24+
} catch (Error $e) {
25+
echo $e->getMessage(), "\n";
26+
}
27+
echo "\n";
28+
29+
$test2 = new Test2;
30+
$test2(b: 'B', a: 'A', c: 'C');
31+
$test2(b: 'B', c: 'C');
32+
echo "\n";
33+
34+
$test3 = function($a = 'a', $b = 'b') {
35+
echo "a: $a, b: $b\n";
36+
};
37+
$test3(b: 'B', a: 'A');
38+
$test3(b: 'B');
39+
try {
40+
$test3(b: 'B', c: 'C');
41+
} catch (Error $e) {
42+
echo $e->getMessage(), "\n";
43+
}
44+
45+
?>
46+
--EXPECT--
47+
a: A, b: B
48+
a: a, b: B
49+
Unknown named parameter $c
50+
51+
a: A, b: B
52+
array(1) {
53+
["c"]=>
54+
string(1) "C"
55+
}
56+
a: a, b: B
57+
array(1) {
58+
["c"]=>
59+
string(1) "C"
60+
}
61+
62+
a: A, b: B
63+
a: a, b: B
64+
Unknown named parameter $c
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
--TEST--
2+
Named params in attributes
3+
--FILE--
4+
<?php
5+
6+
<<Attribute>>
7+
class MyAttribute {
8+
public function __construct(
9+
public $a = 'a',
10+
public $b = 'b',
11+
public $c = 'c',
12+
) {}
13+
}
14+
15+
<<MyAttribute('A', c: 'C')>>
16+
class Test1 {}
17+
18+
<<MyAttribute('A', a: 'C')>>
19+
class Test2 {}
20+
21+
$attr = (new ReflectionClass(Test1::class))->getAttributes()[0];
22+
var_dump($attr->getName());
23+
var_dump($attr->getArguments());
24+
var_dump($attr->newInstance());
25+
26+
$attr = (new ReflectionClass(Test2::class))->getAttributes()[0];
27+
try {
28+
var_dump($attr->newInstance());
29+
} catch (Error $e) {
30+
echo $e->getMessage(), "\n";
31+
}
32+
33+
?>
34+
--EXPECT--
35+
string(11) "MyAttribute"
36+
array(2) {
37+
[0]=>
38+
string(1) "A"
39+
["c"]=>
40+
string(1) "C"
41+
}
42+
object(MyAttribute)#1 (3) {
43+
["a"]=>
44+
string(1) "A"
45+
["b"]=>
46+
string(1) "b"
47+
["c"]=>
48+
string(1) "C"
49+
}
50+
Named parameter $a overwrites previous argument
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
--TEST--
2+
Named params in attributes: Duplicate named parameter error
3+
--FILE--
4+
<?php
5+
6+
<<MyAttribute(a: 'A', a: 'A')>>
7+
class Test {}
8+
9+
?>
10+
--EXPECTF--
11+
Fatal error: Duplicate named parameter $a in %s on line %d
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
--TEST--
2+
Named params in attributes: Positional after named error
3+
--FILE--
4+
<?php
5+
6+
<<Attribute>>
7+
class MyAttribute { }
8+
9+
<<MyAttribute(a: 'A', 'B')>>
10+
class Test {}
11+
12+
?>
13+
--EXPECTF--
14+
Fatal error: Cannot use positional argument after named argument in %s on line %d

Zend/tests/named_params/basic.phpt

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
--TEST--
2+
Basic test
3+
--FILE--
4+
<?php
5+
6+
function test($a, $b, $c = "c", $d = "d", $e = "e") {
7+
echo "a=$a, b=$b, c=$c, d=$d, e=$e\n";
8+
}
9+
10+
function test3(&$a, &$b, &$c = "c", &$d = "d", &$e = "e") {
11+
echo "a=$a, b=$b, c=$c, d=$d, e=$e\n";
12+
}
13+
14+
function &id($x) {
15+
return $x;
16+
}
17+
18+
$a = "A"; $b = "B"; $c = "C"; $d = "D"; $e = "E";
19+
20+
echo "SEND_VAL:\n";
21+
test("A", "B", "C", d: "D", e: "E");
22+
test("A", "B", "C", e: "E", d: "D");
23+
test(e: "E", a: "A", d: "D", b: "B", c: "C");
24+
test("A", "B", "C", e: "E");
25+
26+
echo "SEND_VAL_EX:\n";
27+
test2("A", "B", "C", d: "D", e: "E");
28+
test2("A", "B", "C", e: "E", d: "D");
29+
test2(e: "E", a: "A", d: "D", b: "B", c: "C");
30+
test2("A", "B", "C", e: "E");
31+
32+
echo "SEND_VAR:\n";
33+
test($a, $b, $c, d: $d, e: $e);
34+
test($a, $b, $c, e: $e, d: $d);
35+
test(e: $e, a: $a, d: $d, b: $b, c: $c);
36+
test(a: $a, b: $b, c: $c, e: $e);
37+
38+
echo "SEND_VAR_EX:\n";
39+
test2($a, $b, $c, d: $d, e: $e);
40+
test2($a, $b, $c, e: $e, d: $d);
41+
test2(e: $e, a: $a, d: $d, b: $b, c: $c);
42+
test2(a: $a, b: $b, c: $c, e: $e);
43+
44+
echo "SEND_VAR_NO_REF:\n";
45+
test3(id("A"), id("B"), id("C"), d: id("D"), e: id("E"));
46+
test3(id("A"), id("B"), id("C"), e: id("E"), d: id("D"));
47+
test3(e: id("E"), a: id("A"), d: id("D"), b: id("B"), c: id("C"));
48+
test3(id("A"), id("B"), id("C"), e: id("E"));
49+
50+
echo "SEND_VAR_NO_REF_EX:\n";
51+
test4(id("A"), id("B"), id("C"), d: id("D"), e: id("E"));
52+
test4(id("A"), id("B"), id("C"), e: id("E"), d: id("D"));
53+
test4(e: id("E"), a: id("A"), d: id("D"), b: id("B"), c: id("C"));
54+
test4(id("A"), id("B"), id("C"), e: id("E"));
55+
56+
echo "SEND_REF:\n";
57+
test3($a, $b, $c, d: $d, e: $e);
58+
test3($a, $b, $c, e: $e, d: $d);
59+
test3(e: $e, a: $a, d: $d, b: $b, c: $c);
60+
test3(a: $a, b: $b, c: $c, e: $e);
61+
62+
function test2($a, $b, $c = "c", $d = "d", $e = "e") {
63+
echo "a=$a, b=$b, c=$c, d=$d, e=$e\n";
64+
}
65+
66+
function test4(&$a, &$b, &$c = "c", &$d = "d", &$e = "e") {
67+
echo "a=$a, b=$b, c=$c, d=$d, e=$e\n";
68+
}
69+
70+
?>
71+
--EXPECT--
72+
SEND_VAL:
73+
a=A, b=B, c=C, d=D, e=E
74+
a=A, b=B, c=C, d=D, e=E
75+
a=A, b=B, c=C, d=D, e=E
76+
a=A, b=B, c=C, d=d, e=E
77+
SEND_VAL_EX:
78+
a=A, b=B, c=C, d=D, e=E
79+
a=A, b=B, c=C, d=D, e=E
80+
a=A, b=B, c=C, d=D, e=E
81+
a=A, b=B, c=C, d=d, e=E
82+
SEND_VAR:
83+
a=A, b=B, c=C, d=D, e=E
84+
a=A, b=B, c=C, d=D, e=E
85+
a=A, b=B, c=C, d=D, e=E
86+
a=A, b=B, c=C, d=d, e=E
87+
SEND_VAR_EX:
88+
a=A, b=B, c=C, d=D, e=E
89+
a=A, b=B, c=C, d=D, e=E
90+
a=A, b=B, c=C, d=D, e=E
91+
a=A, b=B, c=C, d=d, e=E
92+
SEND_VAR_NO_REF:
93+
a=A, b=B, c=C, d=D, e=E
94+
a=A, b=B, c=C, d=D, e=E
95+
a=A, b=B, c=C, d=D, e=E
96+
a=A, b=B, c=C, d=d, e=E
97+
SEND_VAR_NO_REF_EX:
98+
a=A, b=B, c=C, d=D, e=E
99+
a=A, b=B, c=C, d=D, e=E
100+
a=A, b=B, c=C, d=D, e=E
101+
a=A, b=B, c=C, d=d, e=E
102+
SEND_REF:
103+
a=A, b=B, c=C, d=D, e=E
104+
a=A, b=B, c=C, d=D, e=E
105+
a=A, b=B, c=C, d=D, e=E
106+
a=A, b=B, c=C, d=d, e=E

0 commit comments

Comments
 (0)