Skip to content

Commit

Permalink
Using variadic on some methods
Browse files Browse the repository at this point in the history
  • Loading branch information
jrbasso committed Dec 14, 2016
1 parent 02f8000 commit 1ab2eff
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 34 deletions.
4 changes: 2 additions & 2 deletions src/Collection/CollectionInterface.php
Expand Up @@ -891,8 +891,8 @@ public function zip($items);
*
* ```
* $collection = new Collection([1, 2]);
* $zipped = $collection->zipWith([3, 4], [5, 6], function () {
* return array_sum(func_get_args());
* $zipped = $collection->zipWith([3, 4], [5, 6], function (...$args) {
* return array_sum($args);
* });
* $zipped->toList(); // returns [9, 12]; [(1 + 3 + 5), (2 + 4 + 6)]
* ```
Expand Down
5 changes: 2 additions & 3 deletions src/Controller/Controller.php
Expand Up @@ -570,13 +570,12 @@ public function redirect($url, $status = 302)
*
* @param string $action The new action to be 'redirected' to.
* Any other parameters passed to this method will be passed as parameters to the new action.
* @param array ...$args Arguments passed to the action
* @return mixed Returns the return value of the called action
*/
public function setAction($action)
public function setAction($action, ...$args)
{
$this->request = $this->request->withParam('action', $action);
$args = func_get_args();
unset($args[0]);

return call_user_func_array([&$this, $action], $args);
}
Expand Down
5 changes: 2 additions & 3 deletions src/Http/ServerRequest.php
Expand Up @@ -634,17 +634,16 @@ public function __isset($name)
*
* @param string|array $type The type of request you want to check. If an array
* this method will return true if the request matches any type.
* @param array ...$args List of arguments
* @return bool Whether or not the request is the type you are checking.
*/
public function is($type)
public function is($type, ...$args)
{
if (is_array($type)) {
$result = array_map([$this, 'is'], $type);

return count(array_filter($result)) > 0;
}
$args = func_get_args();
array_shift($args);

$type = strtolower($type);
if (!isset(static::$_detectors[$type])) {
Expand Down
48 changes: 24 additions & 24 deletions src/I18n/functions.php
Expand Up @@ -19,17 +19,17 @@
* Returns a translated string if one is found; Otherwise, the submitted message.
*
* @param string $singular Text to translate.
* @param mixed $args Array with arguments or multiple arguments in function.
* @param array ...$args Array with arguments or multiple arguments in function.
* @return string|null The translated text, or null if invalid.
* @link http://book.cakephp.org/3.0/en/core-libraries/global-constants-and-functions.html#__
*/
function __($singular, $args = null)
function __($singular, ...$args)
{
if (!$singular) {
return null;
}

$arguments = func_num_args() === 2 ? (array)$args : array_slice(func_get_args(), 1);
$arguments = func_num_args() === 2 ? (array)$args[0] : $args;

return I18n::translator()->translate($singular, $arguments);
}
Expand All @@ -44,17 +44,17 @@ function __($singular, $args = null)
* @param string $singular Singular text to translate.
* @param string $plural Plural text.
* @param int $count Count.
* @param mixed $args Array with arguments or multiple arguments in function.
* @param array ...$args Array with arguments or multiple arguments in function.
* @return string|null Plural form of translated string, or null if invalid.
* @link http://book.cakephp.org/3.0/en/core-libraries/global-constants-and-functions.html#__n
*/
function __n($singular, $plural, $count, $args = null)
function __n($singular, $plural, $count, ...$args)
{
if (!$singular) {
return null;
}

$arguments = func_num_args() === 4 ? (array)$args : array_slice(func_get_args(), 3);
$arguments = func_num_args() === 4 ? (array)$args[0] : $args;

return I18n::translator()->translate(
$plural,
Expand All @@ -70,16 +70,16 @@ function __n($singular, $plural, $count, $args = null)
*
* @param string $domain Domain.
* @param string $msg String to translate.
* @param mixed $args Array with arguments or multiple arguments in function.
* @param array ...$args Array with arguments or multiple arguments in function.
* @return string|null Translated string.
* @link http://book.cakephp.org/3.0/en/core-libraries/global-constants-and-functions.html#__d
*/
function __d($domain, $msg, $args = null)
function __d($domain, $msg, ...$args)
{
if (!$msg) {
return null;
}
$arguments = func_num_args() === 3 ? (array)$args : array_slice(func_get_args(), 2);
$arguments = func_num_args() === 3 ? (array)$args[0] : $args;

return I18n::translator($domain)->translate($msg, $arguments);
}
Expand All @@ -96,17 +96,17 @@ function __d($domain, $msg, $args = null)
* @param string $singular Singular string to translate.
* @param string $plural Plural.
* @param int $count Count.
* @param mixed $args Array with arguments or multiple arguments in function.
* @param array ...$args Array with arguments or multiple arguments in function.
* @return string|null Plural form of translated string.
* @link http://book.cakephp.org/3.0/en/core-libraries/global-constants-and-functions.html#__dn
*/
function __dn($domain, $singular, $plural, $count, $args = null)
function __dn($domain, $singular, $plural, $count, ...$args)
{
if (!$singular) {
return null;
}

$arguments = func_num_args() === 5 ? (array)$args : array_slice(func_get_args(), 4);
$arguments = func_num_args() === 5 ? (array)$args[0] : $args;

return I18n::translator($domain)->translate(
$plural,
Expand All @@ -124,17 +124,17 @@ function __dn($domain, $singular, $plural, $count, $args = null)
*
* @param string $context Context of the text.
* @param string $singular Text to translate.
* @param mixed $args Array with arguments or multiple arguments in function.
* @param array ...$args Array with arguments or multiple arguments in function.
* @return string|null Translated string.
* @link http://book.cakephp.org/3.0/en/core-libraries/global-constants-and-functions.html#__x
*/
function __x($context, $singular, $args = null)
function __x($context, $singular, ...$args)
{
if (!$singular) {
return null;
}

$arguments = func_num_args() === 3 ? (array)$args : array_slice(func_get_args(), 2);
$arguments = func_num_args() === 3 ? (array)$args[0] : $args;

return I18n::translator()->translate($singular, ['_context' => $context] + $arguments);
}
Expand All @@ -152,17 +152,17 @@ function __x($context, $singular, $args = null)
* @param string $singular Singular text to translate.
* @param string $plural Plural text.
* @param int $count Count.
* @param mixed $args Array with arguments or multiple arguments in function.
* @param array ...$args Array with arguments or multiple arguments in function.
* @return string|null Plural form of translated string.
* @link http://book.cakephp.org/3.0/en/core-libraries/global-constants-and-functions.html#__xn
*/
function __xn($context, $singular, $plural, $count, $args = null)
function __xn($context, $singular, $plural, $count, ...$args)
{
if (!$singular) {
return null;
}

$arguments = func_num_args() === 5 ? (array)$args : array_slice(func_get_args(), 4);
$arguments = func_num_args() === 5 ? (array)$args[0] : $args;

return I18n::translator()->translate(
$plural,
Expand All @@ -181,17 +181,17 @@ function __xn($context, $singular, $plural, $count, $args = null)
* @param string $domain Domain.
* @param string $context Context of the text.
* @param string $msg String to translate.
* @param mixed $args Array with arguments or multiple arguments in function.
* @param array ...$args Array with arguments or multiple arguments in function.
* @return string|null Translated string.
* @link http://book.cakephp.org/3.0/en/core-libraries/global-constants-and-functions.html#__dx
*/
function __dx($domain, $context, $msg, $args = null)
function __dx($domain, $context, $msg, ...$args)
{
if (!$msg) {
return null;
}

$arguments = func_num_args() === 4 ? (array)$args : array_slice(func_get_args(), 3);
$arguments = func_num_args() === 4 ? (array)$args[0] : $args;

return I18n::translator($domain)->translate(
$msg,
Expand All @@ -213,17 +213,17 @@ function __dx($domain, $context, $msg, $args = null)
* @param string $singular Singular text to translate.
* @param string $plural Plural text.
* @param int $count Count.
* @param mixed $args Array with arguments or multiple arguments in function.
* @param array ...$args Array with arguments or multiple arguments in function.
* @return string|null Plural form of translated string.
* @link http://book.cakephp.org/3.0/en/core-libraries/global-constants-and-functions.html#__dxn
*/
function __dxn($domain, $context, $singular, $plural, $count, $args = null)
function __dxn($domain, $context, $singular, $plural, $count, ...$args)
{
if (!$singular) {
return null;
}

$arguments = func_num_args() === 6 ? (array)$args : array_slice(func_get_args(), 5);
$arguments = func_num_args() === 6 ? (array)$args[0] : $args;

return I18n::translator($domain)->translate(
$plural,
Expand Down
4 changes: 2 additions & 2 deletions tests/TestCase/Collection/CollectionTest.php
Expand Up @@ -1553,8 +1553,8 @@ public function testZipWith()
});
$this->assertEquals([3, 8], $zipped->toList());

$zipped = $collection->zipWith([3, 4], [5, 6, 7], function () {
return array_sum(func_get_args());
$zipped = $collection->zipWith([3, 4], [5, 6, 7], function (...$args) {
return array_sum($args);
});
$this->assertEquals([9, 12], $zipped->toList());
}
Expand Down

0 comments on commit 1ab2eff

Please sign in to comment.