Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pedro-stanaka/d3fdd5ecebd396635ab23eebf22313e6 to your computer and use it in GitHub Desktop.
Save pedro-stanaka/d3fdd5ecebd396635ab23eebf22313e6 to your computer and use it in GitHub Desktop.
<?php
class ArgsMerger
{
/**
* @param mixed[] $args
* @param ReflectionMethod $reflectionMethod
*
* @return array
*/
public static function mergeArgsWithDefaults($args, \ReflectionMethod $reflectionMethod)
{
foreach (array_slice($reflectionMethod->getParameters(), count($args)) as $param) {
/**
* @var ReflectionParameter $param
*/
$args[] = $param->getDefaultValue();
}
return $args;
}
}
interface ProxyCandidateInterface
{
public function foo(string $a, string $b, string $c = 'foo');
public function fooImproved(string $a, string $b, string $c = 'foo', int $d = 1);
public function fooEachVar($a, $b, $c = 'foo');
}
class GeneratedProxy implements ProxyCandidateInterface
{
public function foo(string $a, string $b, string $c = 'foo')
{
var_dump(func_get_args());
}
public function fooImproved(string $a, string $b, string $c = 'foo', int $d = 1)
{
var_dump(ArgsMerger::mergeArgsWithDefaults(func_get_args(), new ReflectionMethod(__CLASS__, __FUNCTION__)));
}
public function fooEachVar($a, $b, $c = 'foo')
{
var_dump([$a, $b, $c]);
}
}
$ex = new GeneratedProxy();
echo 'foo - no output of defaults';
$ex->foo('sample', 'other');
echo 'foo - WITH output of defaults';
$ex->fooImproved('sample', 'other', '2123');
echo 'foo - Previous way before 2.2.2';
$ex->fooEachVar('sample', 'other');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment