/**
  * @return self
  */
 public static function from(\ReflectionParameter $from)
 {
     $param = new static();
     $param->name = $from->getName();
     $param->reference = $from->isPassedByReference();
     if ($from->isArray()) {
         $param->typeHint = 'array';
     } elseif (PHP_VERSION_ID >= 50400 && $from->isCallable()) {
         $param->typeHint = 'callable';
     } else {
         try {
             $param->typeHint = $from->getClass() ? '\\' . $from->getClass()->getName() : NULL;
         } catch (\ReflectionException $e) {
             if (preg_match('#Class (.+) does not exist#', $e->getMessage(), $m)) {
                 $param->typeHint = '\\' . $m[1];
             } else {
                 throw $e;
             }
         }
     }
     $param->optional = PHP_VERSION_ID < 50407 ? $from->isOptional() || $param->typeHint && $from->allowsNull() : $from->isDefaultValueAvailable();
     $param->defaultValue = PHP_VERSION_ID === 50316 ? $from->isOptional() : $from->isDefaultValueAvailable() ? $from->getDefaultValue() : NULL;
     $namespace = $from->getDeclaringClass() ? $from->getDeclaringClass()->getNamespaceName() : NULL;
     $namespace = $namespace ? "\\{$namespace}\\" : '\\';
     if (Nette\Utils\Strings::startsWith($param->typeHint, $namespace)) {
         $param->typeHint = substr($param->typeHint, strlen($namespace));
     }
     return $param;
 }
Exemplo n.º 2
0
 public function getType() : Type
 {
     if ($this->type === null) {
         $phpDocType = $this->phpDocType;
         if ($phpDocType !== null && $this->reflection->isDefaultValueAvailable() && $this->reflection->getDefaultValue() === null) {
             $phpDocType = $phpDocType->makeNullable();
         }
         $this->type = TypehintHelper::decideTypeFromReflection($this->reflection->getType(), $phpDocType, $this->reflection->getDeclaringClass() !== null ? $this->reflection->getDeclaringClass()->getName() : null, $this->reflection->isVariadic());
     }
     return $this->type;
 }
Exemplo n.º 3
0
 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();
 }
Exemplo n.º 4
0
 public function getDeclaringClass()
 {
     $phpReflection = parent::getDeclaringClass();
     $zendReflection = new ZendL_Reflection_Class($phpReflection->getName());
     unset($phpReflection);
     return $zendReflection;
 }
Exemplo n.º 5
0
 /**
  * @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());
 }
Exemplo n.º 6
0
 /**
  * Gets the declaring class.
  * 
  * @return \Wingu\OctopusCore\Reflection\ReflectionClass
  */
 public function getDeclaringClass()
 {
     $class = parent::getDeclaringClass();
     if ($class !== null) {
         $class = new ReflectionClass($class->getName());
     }
     return $class;
 }
 protected function getDependency(\ReflectionParameter $param, $container)
 {
     $dependency = $param->getClass();
     if (is_null($dependency)) {
         $message = "Unable to resolve [{$param->name}] in {$param->getDeclaringClass()->getName()}";
         $this->raiseException($message);
     }
     return $container->get($dependency->name);
 }
Exemplo n.º 8
0
 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;
 }
Exemplo n.º 9
0
 /**
  * Get declaring class reflection object
  *
  * @param  string $reflectionClass Reflection class to use
  * @return \Zend\Reflection\ReflectionClass
  */
 public function getDeclaringClass($reflectionClass = 'Zend\\Reflection\\ReflectionClass')
 {
     $phpReflection = parent::getDeclaringClass();
     $zendReflection = new $reflectionClass($phpReflection->getName());
     if (!$zendReflection instanceof ReflectionClass) {
         throw new Exception\InvalidArgumentException('Invalid reflection class provided; must extend Zend_Reflection_Class');
     }
     unset($phpReflection);
     return $zendReflection;
 }
Exemplo n.º 10
0
 /**
  * Get declaring class reflection object
  *
  * @param  string $reflectionClass Reflection class to use
  * @return Zend_Reflection_Class
  */
 public function getDeclaringClass($reflectionClass = 'Zend_Reflection_Class')
 {
     $phpReflection = parent::getDeclaringClass();
     $zendReflection = new $reflectionClass($phpReflection->getName());
     if (!$zendReflection instanceof Zend_Reflection_Class) {
         #require_once 'Zend/Reflection/Exception.php';
         throw new Zend_Reflection_Exception('Invalid reflection class provided; must extend Zend_Reflection_Class');
     }
     unset($phpReflection);
     return $zendReflection;
 }
Exemplo n.º 11
0
 /**
  * Returns in which class this parameter is defined (not the type hint of the parameter)
  * @return ezcReflectionClass
  */
 function getDeclaringClass()
 {
     if ($this->parameter != null) {
         $class = $this->parameter->getDeclaringClass();
     } else {
         $class = parent::getDeclaringClass();
     }
     if (!empty($class)) {
         return new ezcReflectionClass($class->getName());
     } else {
         return null;
     }
 }
Exemplo n.º 12
0
 private function resolveArgument(\ReflectionParameter $parameter)
 {
     $name = StringUtil::underscore($parameter->name);
     if ('container' === $name) {
         return $this->container;
     }
     if (isset($this->container[$name])) {
         return $this->container[$name];
     }
     if ($parameter->isOptional()) {
         return $parameter->getDefaultValue();
     }
     throw new \RuntimeException(sprintf('Unable to resolve parameter "%s" of class "%s" no service/parameter found with name "%s". ' . 'Consider adding a default value.', $name, $parameter->getDeclaringClass()->name, $name));
 }
Exemplo n.º 13
0
 private static function buildField(\ReflectionParameter $parameter, $data)
 {
     // If parameter turns out to be an Object then recurse
     if (!empty($parameter->getClass())) {
         return self::build($parameter->getClass()->getName(), $data);
     }
     // If parameter is type of array, then check whether it is an array of Object
     if ($parameter->isArray()) {
         $possibleObjectClassName = $parameter->getDeclaringClass()->getName() . '_' . $parameter->getName() . '_item';
         if (class_exists($possibleObjectClassName)) {
             return array_map(function ($values) use($possibleObjectClassName) {
                 return self::build($possibleObjectClassName, $values);
             }, $data);
         }
     }
     return $data;
 }
 /**
  * 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;
 }
Exemplo n.º 15
0
 /**
  * @return string|NULL
  */
 public static function getParameterType(\ReflectionParameter $param)
 {
     if (PHP_VERSION_ID >= 70000) {
         if ($param->hasType()) {
             $type = PHP_VERSION_ID >= 70100 ? $param->getType()->getName() : (string) $param->getType();
             return strtolower($type) === 'self' ? $param->getDeclaringClass()->getName() : $type;
         }
     } elseif ($param->isArray() || $param->isCallable()) {
         return $param->isArray() ? 'array' : 'callable';
     } else {
         try {
             return ($ref = $param->getClass()) ? $ref->getName() : NULL;
         } catch (\ReflectionException $e) {
             if (preg_match('#Class (.+) does not exist#', $e->getMessage(), $m)) {
                 return $m[1];
             }
             throw $e;
         }
     }
 }
Exemplo n.º 16
0
function argData(ReflectionParameter $arg)
{
    $details = "";
    $declaringclass = $arg->getDeclaringClass();
    $name = $arg->getName();
    $class = $arg->getClass();
    $position = $arg->getPosition();
    $details .= "\${$name} has position {$position}\n";
    if (!empty($class)) {
        $classname = $class->getName();
        $details .= "\${$name} must be a {$classname} object\n";
    }
    if ($arg->isPassedByReference()) {
        $details .= "\${$name} is passed by reference\n";
    }
    if ($arg->isDefaultValueAvailable()) {
        $def = $arg->getDefaultValue();
        $details .= "\${$name} has default: {$def}\n";
    }
    if ($arg->allowsNull()) {
        $details .= "\${$name} can be null\n";
    }
    return $details;
}
Exemplo n.º 17
0
 /**
  * Returns the declaring class
  *
  * @return ClassReflection The declaring class
  */
 public function getDeclaringClass()
 {
     return new ClassReflection(parent::getDeclaringClass()->getName());
 }
 /**
  * Returns the declaring class reflection.
  *
  * @return \TokenReflection\IReflectionClass
  */
 public function getDeclaringClass()
 {
     $class = parent::getDeclaringClass();
     return $class ? ReflectionClass::create($class, $this->broker) : null;
 }
Exemplo n.º 19
0
 private static function getNamespaceOfParameter(\ReflectionParameter $parameter)
 {
     $class = $parameter->getDeclaringClass();
     return $class->inNamespace() ? $class->getNamespaceName() . '\\' : '';
 }
Exemplo n.º 20
0
 /**
  * @return Nette\Reflection\ClassReflection
  */
 public function getDeclaringClass()
 {
     return ($ref = parent::getDeclaringClass()) ? new ClassReflection($ref->getName()) : NULL;
 }
 /**
  * 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;
 }
 /**
  * @return ClassReflection
  */
 public function getDeclaringClass()
 {
     return ($ref = parent::getDeclaringClass()) ? ClassReflection::import($ref) : NULL;
 }
Exemplo n.º 23
0
 /**
  * Resolve a non-class hinted dependency.
  *
  * @param  \ReflectionParameter  $parameter
  * @return mixed
  *
  * @throws BindingResolutionException
  */
 protected function resolveNonClass(ReflectionParameter $parameter)
 {
     if ($parameter->isDefaultValueAvailable()) {
         //反射参数是否存在默认值
         return $parameter->getDefaultValue();
         //返回参数默认值
     }
     //不存在默认值抛异常
     $message = "Unresolvable dependency resolving [{$parameter}] in class {$parameter->getDeclaringClass()->getName()}";
     throw new BindingResolutionException($message);
     //BindingResolutionException
 }
Exemplo n.º 24
0
 /**
  * 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()));
 }
Exemplo n.º 25
0
 /**
  * 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);
 }
 /**
  * Returns the declaring class name.
  *
  * @return string|null
  */
 public function getDeclaringClassName()
 {
     $class = parent::getDeclaringClass();
     return $class ? $class->getName() : null;
 }
Exemplo n.º 27
0
 function getDeclaringClass()
 {
     return ($ref = parent::getDeclaringClass()) ? new ClassType($ref->getName()) : NULL;
 }
Exemplo n.º 28
0
 /**
  *  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;
 }
 /**
  * @param \ReflectionParameter $argument
  * @return object
  * @throws InjectionException
  */
 private function getInstanceOfArgument(\ReflectionParameter $argument)
 {
     if (!$argument->getClass()) {
         if ($argument->isOptional()) {
             return $argument->getDefaultValue();
         }
         throw new InjectionException('Invalid argument without class typehint class: [' . $argument->getDeclaringClass()->name . '] method: [' . $argument->getDeclaringFunction()->name . '] argument: [' . $argument->name . ']');
     }
     $argumentClassConfig = $this->config->getClassConfig($argument->getClass()->name);
     $arguments = $this->getParamsFromTypeHint($argument);
     $providedInstance = $this->getNamedProvidedInstance($argument->getClass()->name, $argumentClassConfig, $argument->getDeclaringFunction()->getDocComment(), $argument->name, $arguments);
     if ($providedInstance) {
         return $providedInstance;
     }
     $namedClassName = $this->getNamedClassOfArgument($argument->getClass()->name, $argument->getDeclaringFunction()->getDocComment(), $argument->name);
     if ($namedClassName) {
         return $this->getInstanceOfClass($namedClassName, $arguments);
     }
     return $this->getInstanceOfClass($argument->getClass()->name, $arguments);
 }
Exemplo n.º 30
0
 /**
  * Returns the declaring class
  *
  * @return \TYPO3\CMS\Extbase\Reflection\ClassReflection The declaring class
  */
 public function getDeclaringClass()
 {
     return new \TYPO3\CMS\Extbase\Reflection\ClassReflection(parent::getDeclaringClass()->getName());
 }