-
Notifications
You must be signed in to change notification settings - Fork 7.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
As per RFC: https://wiki.php.net/rfc/variadics
- Loading branch information
Showing
43 changed files
with
2,908 additions
and
2,278 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
Zend/tests/variadic/adding_additional_optional_parameter.phpt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
--TEST-- | ||
It's possible to add additional optional arguments with matching signature | ||
--FILE-- | ||
<?php | ||
|
||
interface DB { | ||
public function query($query, string ...$params); | ||
} | ||
|
||
class MySQL implements DB { | ||
public function query($query, string $extraParam = null, string ...$params) { } | ||
} | ||
|
||
?> | ||
===DONE=== | ||
--EXPECT-- | ||
===DONE=== |
16 changes: 16 additions & 0 deletions
16
Zend/tests/variadic/adding_additional_optional_parameter_error.phpt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
--TEST-- | ||
Additional optional parameters must have a matching prototype | ||
--FILE-- | ||
<?php | ||
|
||
interface DB { | ||
public function query($query, string ...$params); | ||
} | ||
|
||
class MySQL implements DB { | ||
public function query($query, int $extraParam = null, string ...$params) { } | ||
} | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: Declaration of MySQL::query() must be compatible with DB::query($query, string ...$params) in %s on line %d |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
--TEST-- | ||
Basic variadic function | ||
--FILE-- | ||
<?php | ||
|
||
function test1(... $args) { | ||
var_dump($args); | ||
} | ||
|
||
test1(); | ||
test1(1); | ||
test1(1, 2, 3); | ||
|
||
function test2($arg1, $arg2, ...$args) { | ||
var_dump($arg1, $arg2, $args); | ||
} | ||
|
||
test2(1, 2); | ||
test2(1, 2, 3); | ||
test2(1, 2, 3, 4, 5); | ||
|
||
?> | ||
--EXPECT-- | ||
array(0) { | ||
} | ||
array(1) { | ||
[0]=> | ||
int(1) | ||
} | ||
array(3) { | ||
[0]=> | ||
int(1) | ||
[1]=> | ||
int(2) | ||
[2]=> | ||
int(3) | ||
} | ||
int(1) | ||
int(2) | ||
array(0) { | ||
} | ||
int(1) | ||
int(2) | ||
array(1) { | ||
[0]=> | ||
int(3) | ||
} | ||
int(1) | ||
int(2) | ||
array(3) { | ||
[0]=> | ||
int(3) | ||
[1]=> | ||
int(4) | ||
[2]=> | ||
int(5) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
--TEST-- | ||
Variadic arguments with by-reference passing | ||
--FILE-- | ||
<?php | ||
|
||
function test(&... $args) { | ||
$i = 0; | ||
foreach ($args as &$arg) { | ||
$arg = $i++; | ||
} | ||
} | ||
|
||
test(); | ||
test($a); | ||
var_dump($a); | ||
test($b, $c, $d); | ||
var_dump($b, $c, $d); | ||
|
||
?> | ||
--EXPECT-- | ||
int(0) | ||
int(0) | ||
int(1) | ||
int(2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
--TEST-- | ||
By-ref variadics enforce the reference | ||
--FILE-- | ||
<?php | ||
|
||
function test(&... $args) { } | ||
|
||
test(1); | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: Only variables can be passed by reference in %s on line %d |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
--TEST-- | ||
Variadic argument cannot have a default value | ||
--FILE-- | ||
<?php | ||
|
||
function test(...$args = 123) {} | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: Variadic parameter cannot have a default value in %s on line %d |
16 changes: 16 additions & 0 deletions
16
Zend/tests/variadic/non_variadic_implements_variadic_error.phpt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
--TEST-- | ||
It's not possible to turn a variadic function into a non-variadic one | ||
--FILE-- | ||
<?php | ||
|
||
interface DB { | ||
public function query($query, ...$params); | ||
} | ||
|
||
class MySQL implements DB { | ||
public function query($query, $params) { } | ||
} | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: Declaration of MySQL::query() must be compatible with DB::query($query, ...$params) in %s on line %d |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
--TEST-- | ||
Only the last argument can be variadic | ||
--FILE-- | ||
<?php | ||
|
||
function test($foo, ...$bar, $baz) {} | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: Only the last parameter can be variadic in %s on line %d |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
--TEST-- | ||
Optional parameter before variadic parameter | ||
--FILE-- | ||
<?php | ||
|
||
function fn($reqParam, $optParam = null, ...$params) { | ||
var_dump($reqParam, $optParam, $params); | ||
} | ||
|
||
fn(1); | ||
fn(1, 2); | ||
fn(1, 2, 3); | ||
fn(1, 2, 3, 4); | ||
fn(1, 2, 3, 4, 5); | ||
|
||
?> | ||
--EXPECT-- | ||
int(1) | ||
NULL | ||
array(0) { | ||
} | ||
int(1) | ||
int(2) | ||
array(0) { | ||
} | ||
int(1) | ||
int(2) | ||
array(1) { | ||
[0]=> | ||
int(3) | ||
} | ||
int(1) | ||
int(2) | ||
array(2) { | ||
[0]=> | ||
int(3) | ||
[1]=> | ||
int(4) | ||
} | ||
int(1) | ||
int(2) | ||
array(3) { | ||
[0]=> | ||
int(3) | ||
[1]=> | ||
int(4) | ||
[2]=> | ||
int(5) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
--TEST-- | ||
It's not possible to remove required parameter before a variadic parameter | ||
--FILE-- | ||
<?php | ||
|
||
/* Theoretically this should be valid because it weakens the constraint, but | ||
* PHP does not allow this (for non-variadics), so I'm not allowing it here, too, | ||
* to stay consistent. */ | ||
|
||
interface DB { | ||
public function query($query, ...$params); | ||
} | ||
|
||
class MySQL implements DB { | ||
public function query(...$params) { } | ||
} | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: Declaration of MySQL::query() must be compatible with DB::query($query, ...$params) in %s on line %d |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
--TEST-- | ||
Variadic arguments enforce typehints | ||
--FILE-- | ||
<?php | ||
|
||
function test(array... $args) { | ||
var_dump($args); | ||
} | ||
|
||
test(); | ||
test([0], [1], [2]); | ||
test([0], [1], 2); | ||
|
||
?> | ||
--EXPECTF-- | ||
array(0) { | ||
} | ||
array(3) { | ||
[0]=> | ||
array(1) { | ||
[0]=> | ||
int(0) | ||
} | ||
[1]=> | ||
array(1) { | ||
[0]=> | ||
int(1) | ||
} | ||
[2]=> | ||
array(1) { | ||
[0]=> | ||
int(2) | ||
} | ||
} | ||
|
||
Catchable fatal error: Argument 3 passed to test() must be of the type array, integer given, called in %s on line %d |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
--TEST-- | ||
Error suppression for typehints on variadic arguments works | ||
--FILE-- | ||
<?php | ||
|
||
function test(array... $args) { | ||
var_dump($args); | ||
} | ||
|
||
set_error_handler(function($errno, $errstr) { | ||
var_dump($errstr); | ||
return true; | ||
}); | ||
|
||
test([0], [1], 2); | ||
|
||
?> | ||
--EXPECTF-- | ||
string(%d) "Argument 3 passed to test() must be of the type array, integer given, called in %s on line %d and defined" | ||
array(3) { | ||
[0]=> | ||
array(1) { | ||
[0]=> | ||
int(0) | ||
} | ||
[1]=> | ||
array(1) { | ||
[0]=> | ||
int(1) | ||
} | ||
[2]=> | ||
int(2) | ||
} |
Oops, something went wrong.