4 * This file is part of the Behat.
5 * (c) Konstantin Kudryashov <ever.zet@gmail.com>
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
11 namespace Behat\Testwork\Argument;
13 use Behat\Testwork\Argument\Exception\UnknownParameterValueException;
14 use ReflectionFunctionAbstract;
16 use ReflectionParameter;
19 * Validates function arguments.
21 * @author Konstantin Kudryashov <ever.zet@gmail.com>
26 * Validates that all arguments are in place, throws exception otherwise.
28 * @param ReflectionFunctionAbstract $function
29 * @param mixed[] $arguments
31 * @throws UnknownParameterValueException
33 public function validateArguments(ReflectionFunctionAbstract $function, array $arguments)
35 foreach ($function->getParameters() as $num => $parameter) {
36 $this->validateArgument($parameter, $num, $arguments);
41 * Validates given argument.
43 * @param ReflectionParameter $parameter
44 * @param integer $parameterIndex
45 * @param array $givenArguments
47 private function validateArgument(ReflectionParameter $parameter, $parameterIndex, array $givenArguments)
49 if ($parameter->isDefaultValueAvailable()) {
53 if (array_key_exists($parameterIndex, $givenArguments)) {
57 if (array_key_exists($parameter->getName(), $givenArguments)) {
61 throw new UnknownParameterValueException(sprintf(
62 'Can not find a matching value for an argument `$%s` of the method `%s`.',
63 $parameter->getName(),
64 $this->getFunctionPath($parameter->getDeclaringFunction())
69 * Returns function path for a provided reflection.
71 * @param ReflectionFunctionAbstract $function
75 private function getFunctionPath(ReflectionFunctionAbstract $function)
77 if ($function instanceof ReflectionMethod) {
80 $function->getDeclaringClass()->getName(),
85 return sprintf('%s()', $function->getName());