goaop / framework

:gem: Go! AOP PHP - modern aspect-oriented framework for the new level of software development
go.aopphp.com
MIT License
1.66k stars 163 forks source link

Default parameters in MethodInvocation->getArguments() #442

Closed bartvanhoutte closed 4 years ago

bartvanhoutte commented 4 years ago

If you have a function that defines a parameter with a default value...

public function foo(array $bar = [])

and you call it without passing a value...

->foo();

MethodInvocation->getArguments() is empty.

It would be nice for getArguments() to contain the default value. Unless you want to know that no value was passed ofcourse :thinking:

lisachenko commented 4 years ago

Method getArguments() returns only arguments that was given to that function.

If you need to access default values, you could do this by accessing $invocation->getMethod()->getParameters() and reflect default values from method signature:

$defaultArgs = [];
foreach ($invocation->getMethod()->getParameters() as $argInfo) {
    if ($argInfo->isDefaultValueAvailable()) {
        $defaultArgs[$argInfo->getPosition()] = $argInfo->getDefaultValue();
    }
}
$args = $invocation->getArguments() + $defaultArgs;
lisachenko commented 4 years ago

But be aware, that refection could be slow, so it is your responsibility to cache default argumenta per each function/method to speed up advice invocation.

bartvanhoutte commented 4 years ago

Perfect, thank you.