Created
December 19, 2019 08:47
-
-
Save pedro-stanaka/d3fdd5ecebd396635ab23eebf22313e6 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
<?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