private function findAvailableParameterValue(ReflectionParameter $p, array $availableParameters)
    {
        // determine the name, in case of a class this is the type hint, in
        // case of a non-class parameter it is the actual name
        $isClass = null !== $p->getClass();
        if ($isClass) {
            $parameterName = $p->getClass()->getName();
        } else {
            $parameterName = $p->getName();
        }

        if (array_key_exists($parameterName, $availableParameters)) {
            // we found the parameter!
            return $availableParameters[$parameterName];
        }
        if ($p->isDefaultValueAvailable()) {
            // we have a default value!
            return $p->getDefaultValue();
        }
        throw new BadFunctionCallException(
            sprintf(
                'parameter "%s" expected by callback not available',
                $parameterName
            )
        );
    }