/** * Make sure it throws an exception if wrong arguments are passed. * * @covers \BrightNucleus\Invoker\Helper::parseParams */ public function testParseParamsThrowsExceptionOnWrongArguments() { $array = ['argNonSense' => 'Nonsense']; $reflection = new ReflectionFunction('BrightNucleus\\Invoker\\helperFunction'); $this->setExpectedException('ReflectionException', 'Internal error: Failed to retrieve the default value'); Helper::parseParams($reflection->getParameters(), $array); }
/** * Check the accepted arguments for a given class constructor and pass * associative array values in the right order. * * @since 0.2.0 * * @param string $class The class that needs to be instantiated. * @param array $args Associative array that contains the arguments. * * @return object Instantiated object. * @throws InvalidArgumentException If a valid method is missing. */ protected function instantiateClass($class, array $args) { try { $reflectionMethod = new ReflectionMethod($class, '__construct'); $ordered_arguments = Helper::parseParams($reflectionMethod->getParameters(), $args); $reflectionClass = new ReflectionClass($class); return $reflectionClass->newInstanceArgs((array) $ordered_arguments); } catch (Exception $exception) { throw new InvalidArgumentException(sprintf(_('Failed to instantiate class "%1$s". Reason: %2$s'), $class, $exception->getMessage())); } }
/** * Check the accepted arguments for a given function and pass associative * array values in the right order. * * @since 0.1.0 * * @param string $function Name of the function to invoke. * @param array $args Associative array that contains the arguments. * @return mixed Return value of the invoked function. * @throws InvalidArgumentException If a valid function is missing. */ public function invokeFunction($function, array $args = array()) { if (!$function || !is_string($function) || '' === $function) { throw new InvalidArgumentException(_('Missing valid function to invoke.')); } try { $reflection = new ReflectionFunction($function); $ordered_arguments = Helper::parseParams($reflection->getParameters(), $args); return $reflection->invokeArgs($ordered_arguments); } catch (Exception $exception) { throw new InvalidArgumentException(sprintf(_('Failed to invoke function "%1$s". Reason: %2$s'), $function, $exception->getMessage())); } }
/** * Check the accepted arguments for a given method and pass associative * array values in the right order. * * @since 0.1.0 * * @param object $object The object that contains the method to invoke. * @param string $method Name of the method to invoke. * @param array $args Associative array that contains the arguments. * @return mixed Return value of the invoked method. * @throws InvalidArgumentException If a valid method is missing. */ protected function invokeMethod($object, $method, array $args = []) { if (!$method || !is_string($method) || '' === $method) { throw new InvalidArgumentException(_('Missing valid method to invoke.')); } try { $reflection = new ReflectionMethod(get_class($object), $method); $ordered_arguments = Helper::parseParams($reflection->getParameters(), $args); return $reflection->invokeArgs($object, $ordered_arguments); } catch (Exception $exception) { throw new InvalidArgumentException(sprintf(_('Failed to invoke method "%1$s" of class "%2$s". Reason: %3$s'), $method, get_class($object), $exception->getMessage())); } }