/** * Populates arguments assembled from the event and the given target function. * * @param \ReflectionFunctionAbstract $ref * @param EventParamResolverInterface $resolver * @return array */ protected function loadArguments(\ReflectionFunctionAbstract $ref, EventParamResolverInterface $resolver) { $args = []; if ($ref->getNumberOfParameters() > 1) { foreach (array_slice($ref->getParameters(), 1) as $param) { $args[] = $resolver->resolve($param->getClass(), $param->isDefaultValueAvailable() ? $param->getDefaultValue() : false); } } return $args; }
/** * Resolves function arguments. * * @param array $parameters * * @return array * * @throws UnresolvableArgumentException */ public function resolve(array $parameters) { if (!($number = $this->reflection->getNumberOfParameters())) { return []; } $arguments = array_fill(0, $number, null); foreach ($this->getParameters() as $pos => $parameter) { $result = $this->match($parameter, $parameters); if ($result) { $arguments[$pos] = $result[1]; unset($parameters[$result[0]]); continue; } if ($parameter->isDefaultValueAvailable()) { $arguments[$pos] = $parameter->getDefaultValue(); continue; } throw new UnresolvableArgumentException($parameter); } return $arguments; }
/** * normalize the params of the $rmethod to allow a valid call * * @example * // method signature: foo($bar = '',$baz = '',SomeClass $che); * $rmethod = new ReflectionMethod($instance,'foo'); * $this->normalizeParams($rmethod,array('che','cha')) // retuns array('che','cha',automatically created SomeClass Instance) * $this->normalizeParams($rmethod,array('che')) // retuns array('che','',automatically created SomeClass Instance) * $this->normalizeParams($rmethod,array('che' => new SomeClass(),'bar' => '')) // retuns array('','',passed in SomeClass Instance) * * @param ReflectionFunctionAbstract $rfunc the reflection of the method/function * @param array $params any params you want to pass to override any magically * discovered params * @return array the params ready to be passed to the method using something like call_user_func_array */ public function normalizeParams(\ReflectionFunctionAbstract $rfunc, array $params = array()) { // canary... if ($rfunc->getNumberOfParameters() <= 0) { return $params; } //if $ret_params = array(); $rparams = $rfunc->getParameters(); foreach ($rparams as $rparam) { $ret_params[] = $this->normalizeParam($rparam, $params); } //foreach return $ret_params; }
/** * Create a string representation of the method signature. * * @param ReflectionFunctionAbstract $func The function you want a signature for. * @return void **/ public static function makeFunctionSignature(ReflectionFunctionAbstract $func) { $signature = $func->getName() . '( '; foreach ($func->getParameters() as $param) { $signature .= '$' . $param->getName(); if ($param->isDefaultValueAvailable()) { $signature .= ' = ' . var_export($param->getDefaultValue(), true); } $signature .= ', '; } if ($func->getNumberOfParameters() > 0) { $signature = substr($signature, 0, -2); } $signature .= ' )'; return $signature; }
private static function hashParameters(\ReflectionFunctionAbstract $method) { $res = []; if (PHP_VERSION_ID < 70000 && $method->getNumberOfParameters() && $method->getFileName()) { $res[] = file($method->getFileName())[$method->getStartLine() - 1]; } foreach ($method->getParameters() as $param) { $res[] = [$param->getName(), PHP_VERSION_ID >= 70000 ? PhpReflection::getParameterType($param) : NULL, $param->isVariadic(), $param->isDefaultValueAvailable() ? $param->isDefaultValueConstant() ? $param->getDefaultValueConstantName() : [$param->getDefaultValue()] : NULL]; } return $res; }
/** * @param \ReflectionFunctionAbstract $func * @param integer $num * @return boolean */ public static function matchesNumberOfArguments(\ReflectionFunctionAbstract $func, $num) { return $func->getNumberOfRequiredParameters() <= $num && $func->getNumberOfParameters() >= $num; }