Skip to content

Commit

Permalink
Fix error when there are default values for arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
SerafimArts authored and lisachenko committed Dec 12, 2017
1 parent 0888578 commit f934d91
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions src/Aspect/AbstractContractAspect.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,22 @@ public function __construct(Reader $reader)
*/
protected function fetchMethodArguments(MethodInvocation $invocation)
{
$parameters = $invocation->getMethod()->getParameters();
$argumentNames = array_map(function (\ReflectionParameter $parameter) {
return $parameter->name;
}, $parameters);
$parameters = array_combine($argumentNames, $invocation->getArguments());
$result = [];
$parameters = $invocation->getMethod()->getParameters();
$argumentValues = $invocation->getArguments();

return $parameters;
// Number of arguments can be less than number of parameters because of default values
foreach ($parameters as $parameterIndex => $reflectionParameter) {
$hasArgumentValue = array_key_exists($parameterIndex, $argumentValues);
$argumentValue = $hasArgumentValue ? $argumentValues[$parameterIndex] : null;
if (!$hasArgumentValue && $reflectionParameter->isDefaultValueAvailable()) {
$argumentValue = $reflectionParameter->getDefaultValue();
}
$result[$reflectionParameter->name] = $argumentValue;

}

return $result;
}

/**
Expand Down

0 comments on commit f934d91

Please sign in to comment.