private function getTypeOfParameter(\ReflectionParameter $param) { if ($class = $param->getClass()) { return $class->name; } $paramName = $param->name; if (preg_match('/^[\\s\\/*]*@param\\s+(\\S+)\\s+\\$' . preg_quote($paramName, '/') . '\\W/m', $param->getDeclaringFunction()->getDocComment(), $matches)) { return $this->getFullNameOfType($matches[1], $param->getDeclaringFunction()->getDeclaringClass()); } return; }
public function __construct(\ReflectionParameter $parameter, array $classConfig, \rg\injektor\Configuration $config, \rg\injektor\DependencyInjectionContainer $dic, $mode) { $this->parameter = $parameter; $this->classConfig = $classConfig; $this->config = $config; $this->dic = $dic; $this->name = $parameter->name; $this->nameForAnnotationParsing = $parameter->name; $this->docComment = $this->parameter->getDeclaringFunction()->getDocComment(); $this->additionalArguments = $this->dic->getParamsFromTypeHint($this->parameter); $this->mode = $mode; $this->analyze(); }
public function __construct(\ReflectionParameter $parameter, $message = null, $code = null, \Exception $previous = null) { if (null === $message) { $message = sprintf('Unable to resolve argument $%s (#%d) of %s.', $parameter->name, $parameter->getPosition(), static::getFunctionName($parameter->getDeclaringFunction())); } parent::__construct($message, $code, $previous); }
/** * @param \ReflectionParameter $parameter * @return mixed */ private function getArgumentMock(\ReflectionParameter $parameter) { if ($parameter->getClass() !== null) { return $this->testCase->getMockBuilder($parameter->getClass()->getName())->disableOriginalConstructor()->getMock(); } throw new \Mocktainer\UnmockableMethodArgumentException($parameter->getDeclaringClass()->getName(), $parameter->getDeclaringFunction()->getName(), $parameter->getName()); }
public function getParameterAnnotations(\ReflectionParameter $parameter) { $class = $parameter->getDeclaringClass() ?: 'Closure'; $method = $parameter->getDeclaringFunction(); if (!$method->isUserDefined()) { return array(); } $context = 'parameter ' . ($class === 'Closure' ? $class : $class->getName()) . '::' . $method->getName() . '($' . $parameter->getName() . ')'; if ($class === 'Closure') { $this->parser->setImports($this->getClosureImports($method)); } else { $this->parser->setImports($this->getImports($class)); $this->parser->setIgnoredAnnotationNames($this->getIgnoredAnnotationNames($class)); } $lines = file($method->getFileName()); $lines = array_slice($lines, $start = $method->getStartLine() - 1, $method->getEndLine() - $start); $methodBody = Implode($lines); $methodBody = str_replace("\n", null, $methodBody); $signature = preg_split('/\\)\\s*\\{/', $methodBody); $signature = $signature[0]; $signature = substr($signature, strpos($signature, "function")); if (preg_match_all('/\\/\\*\\*(.*?)\\*\\/' . '.*?\\$(\\w+)/', $signature, $matches)) { $docComments = $matches[1]; $names = $matches[2]; for ($i = 0, $len = count($names); $i < $len; ++$i) { if ($names[$i] === $parameter->name) { return $this->parser->parse($docComments[$i], $context); } } } return array(); }
/** * Creates a PHP parameter from reflection * * @param \ReflectionParameter $ref * @return PhpParameter */ public static function fromReflection(\ReflectionParameter $ref) { $parameter = new static(); $parameter->setName($ref->name)->setPassedByReference($ref->isPassedByReference()); if ($ref->isDefaultValueAvailable()) { $parameter->setDefaultValue($ref->getDefaultValue()); } // find type and description in docblock $docblock = new Docblock($ref->getDeclaringFunction()); $params = $docblock->getTags('param'); $tag = $params->find($ref->name, function (ParamTag $t, $name) { return $t->getVariable() == '$' . $name; }); if ($tag !== null) { $parameter->setType($tag->getType(), $tag->getDescription()); } // set type if not found in comment if ($parameter->getType() === null) { if ($ref->isArray()) { $parameter->setType('array'); } elseif ($class = $ref->getClass()) { $parameter->setType($class->getName()); } elseif (method_exists($ref, 'isCallable') && $ref->isCallable()) { $parameter->setType('callable'); } } return $parameter; }
/** * Get this parameters declaring (@see Nerd\Source\Funktion) object * * @return Nerd\Source\Funktion */ public function getDeclaringFunction() { $function = parent::getDeclaringFunction(); if ($function instanceof \ReflectionMethod) { return new Method($this->getDeclaringClass()->getName(), $function->getName()); } return new Funktion($function->getName()); }
function getParameterAnnotations(\ReflectionParameter $parameter) { $key = "@[Annot]" . $parameter->getDeclaringClass() . "#" . $parameter->getDeclaringFunction() . "#" . $parameter->name; if (($annotations = $this->cache->fetch($key)) === false) { $annotations = $this->reader->getParameterAnnotations($parameter); $this->cache->save($key, $annotations); } return $annotations; }
protected static function getParameterTypeFromDoc(\ReflectionParameter $parameter) { $constructor = $parameter->getDeclaringFunction(); $docComment = $constructor->getDocComment(); if (!preg_match('#@inject(?<target>.+)\\$' . $parameter->getName() . '#', $docComment, $match)) { return null; } return str_replace(' ', '', $match['target']); }
public function getDeclaringFunction() { $phpReflection = parent::getDeclaringFunction(); if ($phpReflection instanceof ReflectionMethod) { $zendReflection = new ZendL_Reflection_Method($this->getDeclaringClass()->getName(), $phpReflection->getName()); } else { $zendReflection = new ZendL_Reflection_Function($phpReflection->getName()); } unset($phpReflection); return $zendReflection; }
private static function getArgumentType(ReflectionParameter $parameter) { $comment = $parameter->getDeclaringFunction()->getDocComment(); if (!$comment) { return null; } $matches = null; if (!preg_match("/@param\\s+([^\\s]+)\\s+\\\${$parameter->name}/", $comment, $matches)) { return null; } return $matches[1]; }
/** * Returns the function or method declaring this parameter * @return ezcReflectionFunction|ezcReflectionMethod * @since PHP 5.2.3 */ public function getDeclaringFunction() { if ($this->parameter instanceof parent) { $func = $this->parameter->getDeclaringFunction(); } else { $func = parent::getDeclaringFunction(); } if ($func instanceof ReflectionMethod) { return new ezcReflectionMethod($func->getDeclaringClass(), $func->getName()); } else { return new ezcReflectionFunction($func->getName()); } }
/** * {@inheritdoc} */ public function getParameterKey(\ReflectionParameter $parameter) { $function = $parameter->getDeclaringFunction(); if ($function instanceof \ReflectionMethod) { foreach ($this->reader->getMethodAnnotations($function) as $annotation) { if ($annotation instanceof Inject) { $value = $annotation->getValue($parameter->getName()); if ($value !== null) { return $value; } } } } return null; }
/** * {@inheritdoc} * @throws ParameterDefinitionAlreadyExistsException * @throws ReferenceNotImplementsException * @throws MethodDefinitionNotFoundException * @throws ParameterDefinitionNotFoundException * @throws \InvalidArgumentException */ public function analyze(DefinitionAnalyzer $analyzer, ClassDefinition $classDefinition, \ReflectionParameter $reflectionParameter) { $methodName = $reflectionParameter->getDeclaringFunction()->getName(); $parameterName = $reflectionParameter->getName(); // Define parameter only if method definition is available if ($classDefinition->hasMethod($methodName)) { $methodDefinition = $classDefinition->getMethod($methodName); // Define parameter definition if not exists $parameterDefinition = $methodDefinition->setupParameter($parameterName); $this->setReflectionMetadata($parameterDefinition, $reflectionParameter); $dependency = $parameterDefinition->getDependency(); // If dependency was not set // UndefinedReference is the default value of dependency which was not use before if (!$dependency || $dependency instanceof UndefinedReference) { $this->setDependencyByReflection($parameterDefinition, $reflectionParameter); } $this->setOrderArguments($methodDefinition, $reflectionParameter->getDeclaringFunction()); } }
/** * Denormalize a method parameter value. * * @param \ReflectionParameter $reflectionParameter * @param mixed $value Parameter value to denormalize * @return mixed */ protected function denormalizeParameter(\ReflectionParameter $reflectionParameter, $value) { if ($reflectionParameter->getClass() === null && !$reflectionParameter->isArray()) { return $value; } if (!$this->serializer instanceof DenormalizerInterface) { throw new LogicException('Cannot denormalize because injected serializer is not a denormalizer'); } if ($reflectionParameter->getClass() !== null) { return $this->serializer->denormalize($value, $reflectionParameter->getClass()->name); } if ($reflectionParameter->isArray()) { $className = $reflectionParameter->getDeclaringClass()->getName(); $methodName = $reflectionParameter->getDeclaringFunction()->getName(); $parameterName = $reflectionParameter->getName(); return $this->serializer->denormalize($value, $className . '::' . $methodName . '(' . $parameterName . ')'); } return $value; }
/** * Get declaring function reflection object * * @param string $reflectionClass Reflection class to use * @return Zend_Reflection_Function|\Zend\Reflection\ReflectionMethod */ public function getDeclaringFunction($reflectionClass = null) { $phpReflection = parent::getDeclaringFunction(); if ($phpReflection instanceof \ReflectionMethod) { $baseClass = 'Zend\\Reflection\\ReflectionMethod'; if (null === $reflectionClass) { $reflectionClass = $baseClass; } $zendReflection = new $reflectionClass($this->getDeclaringClass()->getName(), $phpReflection->getName()); } else { $baseClass = 'Zend\\Reflection\\ReflectionFunction'; if (null === $reflectionClass) { $reflectionClass = $baseClass; } $zendReflection = new $reflectionClass($phpReflection->getName()); } if (!$zendReflection instanceof $baseClass) { throw new Exception\InvalidArgumentException('Invalid reflection class provided; must extend ' . $baseClass); } unset($phpReflection); return $zendReflection; }
/** * Determine the fully-qualified name of a type-hint for the given param without actually loading typehinted classes. * * @param \ReflectionParameter $param * @return string Hinted typename or NULL when no type-hint is present. */ protected function getParamType(\ReflectionParameter $param) { if ($param->isArray()) { return 'array'; } if ($param->isCallable()) { return 'callable'; } $m = NULL; if (defined('HHVM_VERSION')) { // @codeCoverageIgnoreStart $type = $param->getTypehintText(); if ('' === trim($type)) { $type = NULL; } // @codeCoverageIgnoreEnd } elseif (preg_match("'\\[\\s*<[^>]+>\\s+([a-z_][a-z_0-9]*(?:\\s*\\\\\\s*[a-z_][a-z_0-9]*)*)'i", (string) $param, $m)) { $type = preg_replace("'\\s+'", '', $m[1]); } else { $type = NULL; } if ($type !== NULL) { switch (strtolower($type)) { case 'self': $ref = $param->getDeclaringFunction(); if ($ref instanceof \ReflectionMethod) { return $ref->getDeclaringClass()->name; } throw new \RuntimeException(sprintf('Unable to resolve "self" in parameter "%s" of function %s', $param->name, $ref->name)); case 'boolean': return 'bool'; case 'integer': return 'int'; } return $type; } return NULL; }
/** * Get declaring function reflection object * * @param string $reflectionClass Reflection class to use * @return Zend_Reflection_Function|Zend_Reflection_Method */ public function getDeclaringFunction($reflectionClass = null) { $phpReflection = parent::getDeclaringFunction(); if ($phpReflection instanceof ReflectionMethod) { $baseClass = 'Zend_Reflection_Method'; if (null === $reflectionClass) { $reflectionClass = $baseClass; } $zendReflection = new $reflectionClass($this->getDeclaringClass()->getName(), $phpReflection->getName()); } else { $baseClass = 'Zend_Reflection_Function'; if (null === $reflectionClass) { $reflectionClass = $baseClass; } $zendReflection = new $reflectionClass($phpReflection->getName()); } if (!$zendReflection instanceof $baseClass) { #require_once 'Zend/Reflection/Exception.php'; throw new Zend_Reflection_Exception('Invalid reflection class provided; must extend ' . $baseClass); } unset($phpReflection); return $zendReflection; }
/** * @return [string, bool] */ public static function getParameterType(\ReflectionParameter $param) { $def = gettype($param->isDefaultValueAvailable() ? $param->getDefaultValue() : NULL); if (PHP_VERSION_ID >= 70000) { return array((string) $param->getType() ?: $def, $param->hasType() && !$param->getType()->isBuiltin()); } elseif ($param->isArray()) { return array('array', FALSE); } elseif (PHP_VERSION_ID >= 50400 && $param->isCallable()) { return array('callable', FALSE); } else { try { return ($ref = $param->getClass()) ? array($ref->getName(), TRUE) : array($def, FALSE); } catch (\ReflectionException $e) { if (preg_match('#Class (.+) does not exist#', $e->getMessage(), $m)) { throw new \LogicException(sprintf("Class %s not found. Check type hint of parameter \$%s in %s() or 'use' statements.", $m[1], $param->getName(), $param->getDeclaringFunction()->getDeclaringClass()->getName() . '::' . $param->getDeclaringFunction()->getName())); } throw $e; } } }
/** * Returns the declaring function name. * * @return string|null */ public function getDeclaringFunctionName() { $function = parent::getDeclaringFunction(); return $function ? $function->getName() : $function; }
/** * normalize one param of a method * * @since 7-5-11 * @param ReflectionParameter $rparam * @param array $params see {@link normalizeParams()} for description of the $params array * @return mixed the normalized param */ public function normalizeParam(\ReflectionParameter $rparam, array $params = array()) { $ret_param = null; $index = $rparam->getPosition(); // first try and resolve numeric keys, then do string keys... // we use array_key_exists because values could be null if (array_key_exists($index, $params)) { $ret_param = $params[$index]; } else { $field_name = $rparam->getName(); if (array_key_exists($field_name, $params)) { $ret_param = $params[$field_name]; } else { try { $rclass = $rparam->getClass(); } catch (\ReflectionException $e) { throw new \ReflectionException(sprintf('%s which is param %s of method %s::%s()', $e->getMessage(), $index + 1, $rparam->getDeclaringClass()->getName(), $rparam->getDeclaringFunction()->getName())); } //try/catch if ($rclass === null) { if ($this->existsField($field_name)) { $ret_param = $this->getField($field_name); } else { if ($rparam->isDefaultValueAvailable()) { $ret_param = $rparam->getDefaultValue(); } else { $declaring_class = $rparam->getDeclaringClass(); $declaring_func = $rparam->getDeclaringFunction(); throw new \UnexpectedValueException(sprintf('no suitable value could be found for %s::%s() param "%s"', empty($declaring_class) ? 'unknown' : $declaring_class->getName(), empty($declaring_func) ? 'unkown' : $declaring_func->getName(), $field_name)); } } //if/else if/else } else { $class_name = $rclass->getName(); try { $ret_param = $this->getInstance($class_name); } catch (\Exception $e) { if ($rparam->isDefaultValueAvailable()) { $ret_param = $rparam->getDefaultValue(); } else { throw $e; } //if/else } //try/catch } //if/else } //if/else } //if/else return $ret_param; }
/** * Creates a new reflection parameter instance from the passed PHP reflection parameter. * * @param \ReflectionParameter $reflectionParameter The reflection parameter to load the data from * * @return \AppserverIo\Lang\Reflection\ReflectionParameter The instance */ public static function fromPhpReflectionParameter(\ReflectionParameter $reflectionParameter) { // load class, method and parameter name from the reflection parameter $className = $reflectionParameter->getDeclaringClass()->getName(); $methodName = $reflectionParameter->getDeclaringFunction()->getName(); $parameterName = $reflectionParameter->getName(); // initialize and return the parameter instance return new ReflectionParameter($className, $methodName, $parameterName); }
public function getDeclaringFunction() { $f = parent::getDeclaringFunction(); /** @var $f ReflectionMethod */ if (is_a($f, "ReflectionMethod")) { return new ReflectionMethod($f->getDeclaringClass()->getName(), $f->getName()); } else { return $f; } }
/** * @return MethodReflection | FunctionReflection */ public function getDeclaringFunction() { return ($ref = parent::getDeclaringFunction()) instanceof ReflectionMethod ? MethodReflection::import($ref) : FunctionReflection::import($ref); }
/** * Reflection parameter * * @param \ReflectionParameter $parameter * * @return $this */ private function validateParameter(\ReflectionParameter $parameter) { $catcher = function ($className) { $generator = $this->generator($className); if ($generator) { include $generator->generate(); } }; if (($reflectionClass = $parameter->getDeclaringClass()) && ($reflectionMethod = $parameter->getDeclaringFunction())) { $type = $this->typeHintIndex->lookup($reflectionClass->getName(), $reflectionMethod->getName(), '$' . $parameter->getName()); if ($type && !class_exists($type) && !interface_exists($type)) { $catcher($type); return $this; } } spl_autoload_register($catcher); try { $parameter->getClass(); } catch (\ReflectionException $e) { // Ignore reflection exception, as it is an intended behaviour for our catcher } spl_autoload_unregister($catcher); return $this; }
/** * @param \ReflectionClass $classRefl * @param \ReflectionParameter $parameter * * @return string */ private function getParameterTypeFromIndex(\ReflectionClass $classRefl, \ReflectionParameter $parameter) { return $this->typeHintIndex->lookup($classRefl->getName(), $parameter->getDeclaringFunction()->getName(), '$' . $parameter->getName()); }
/** * Handles unresolved arguments for getArgument(). * * Subclasses that override this method may return a default value * instead of throwing an exception. * * @throws \RuntimeException * Thrown when there is a missing parameter. */ protected function handleUnresolvedArgument(\ReflectionParameter $parameter) { $class = $parameter->getDeclaringClass(); $function = $parameter->getDeclaringFunction(); if ($class && !$function->isClosure()) { $function_name = $class->getName() . '::' . $function->getName(); } else { $function_name = $function->getName(); } throw new \RuntimeException(sprintf('Callable "%s" requires a value for the "$%s" argument.', $function_name, $parameter->getName())); }
/** * Returns the documented type for the given parameter. * * @param ReflectionParameter $param * @return string The documented parameter type. * @throws Mol_Controller_Exception_ParameterTagMissing If the parameter was not documented correctly. */ private function getTypeFor(ReflectionParameter $param) { /* @var $method ReflectionFunction */ $method = $param->getDeclaringFunction(); $types = $this->getDocumentedParamsFor($method); if (!isset($types[$param->getName()])) { throw new Mol_Controller_Exception_ParameterTagMissing($param->getName()); } return $types[$param->getName()]; }
/** * Determine the parameter type by looking into annotations * * @param \ReflectionParameter $reflectedParameter * * @return string * @since 0.1 * @todo Look for the correct parameter by name */ protected function determineParameterType(\ReflectionParameter $reflectedParameter) { if (strlen($reflectedParameter->getDeclaringFunction()->getDocComment())) { $name = $reflectedParameter->getName(); $pattern = '/[ ]+\\*[ ]+@param[ ]+([a-zA-Z\\\\]+).*/'; preg_match($pattern, $reflectedParameter->getDeclaringFunction()->getDocComment(), $matches); if (is_array($matches) && array_key_exists(1, $matches)) { return $matches[1]; } } }
/** * @param \ReflectionParameter $argument * @return array */ public function getParamsFromTypeHint(\ReflectionParameter $argument) { return $this->annotationReader->getParamsFromTypeHint($argument->getDeclaringFunction()->getDocComment(), $argument->name, 'param'); }