/**
  * Factory method responsible for instantiating the correct sub type.
  *
  * @param string  $tagLine The text for this tag, including description.
  * @param Context $context
  *
  * @throws \InvalidArgumentException if an invalid tag line was presented.
  *
  * @return static A new tag object.
  */
 public function create($tagLine, Context $context = null)
 {
     if (!$context) {
         $context = new Context('');
     }
     list($tagName, $tagBody) = $this->extractTagParts($tagLine);
     $handler = Generic::class;
     if (isset($this->tagHandlerMappings[$tagName])) {
         $handler = $this->tagHandlerMappings[$tagName];
     } elseif ($this->isAnnotation($tagName)) {
         $tagName = (string) $this->fqsenResolver->resolve($tagName, $context);
         if (isset($this->tagHandlerMappings[$tagName])) {
             $handler = $this->tagHandlerMappings[$tagName];
         }
     }
     $parameters = (new \ReflectionMethod($handler, 'create'))->getParameters();
     $wiring = array_merge($this->serviceLocator, ['name' => $tagName, 'body' => $tagBody, Context::class => $context]);
     $arguments = [];
     foreach ($parameters as $index => $parameter) {
         $typeHint = $parameter->getClass() ? $parameter->getClass()->getName() : null;
         if (isset($wiring[$typeHint])) {
             $arguments[] = $wiring[$typeHint];
             continue;
         }
         $parameterName = $parameter->getName();
         if (isset($wiring[$parameterName])) {
             $arguments[] = $wiring[$parameterName];
             continue;
         }
         $arguments[] = null;
     }
     return call_user_func_array([$handler, 'create'], $arguments);
 }
Beispiel #2
0
 /**
  * {@inheritdoc}
  */
 public static function create($body, FqsenResolver $resolver = null, DescriptionFactory $descriptionFactory = null, TypeContext $context = null)
 {
     Assert::string($body);
     Assert::allNotNull([$resolver, $descriptionFactory]);
     $parts = preg_split('/\\s+/Su', $body, 2);
     return new static($resolver->resolve($parts[0], $context), $descriptionFactory->create(isset($parts[1]) ? $parts[1] : '', $context));
 }
 /**
  * @param Node\Stmt\ClassMethod $node
  * @param string                $text
  *
  * @return string
  */
 public function addParams(Node\Stmt\ClassMethod $node, $text)
 {
     $docBlock = new Docblock($text);
     foreach ($node->getParams() as $param) {
         if ($param->type && $param->type instanceof Node\Name) {
             $type = $this->resolver->resolve(implode('\\', $param->type->parts), $this->context);
             $type = str_replace('\\\\', '\\', $type);
         } elseif ($param->type && is_string($param->type)) {
             $type = $param->type;
         }
         if (!isset($type)) {
             continue;
         }
         $docBlock->appendTag(new ParamTag((string) $type . ' $' . $param->name));
     }
     $string = $docBlock->toString();
     return str_replace("* \n", "*\n", $string);
     //TODO remove once https://github.com/gossi/docblock/pull/2 is merged
 }
 /**
  * @param $type
  * @return string
  */
 private function getResolvedType($type)
 {
     $context_factory = new ContextFactory();
     $context = $context_factory->createFromReflector($this->getDeclaringClass());
     $fqn_resolver = new FqsenResolver();
     return (string) $fqn_resolver->resolve($type, $context);
 }
<?php

use phpDocumentor\Reflection\FqsenResolver;
use phpDocumentor\Reflection\TypeResolver;
use phpDocumentor\Reflection\Types\ContextFactory;
require '../vendor/autoload.php';
$typeResolver = new TypeResolver();
$fqsenResolver = new FqsenResolver();
$contextFactory = new ContextFactory();
$context = $contextFactory->createForNamespace('My\\Example', file_get_contents('Classy.php'));
// Class named: \phpDocumentor\Reflection\Types\Resolver
var_dump((string) $typeResolver->resolve('Types\\Resolver', $context));
// String
var_dump((string) $typeResolver->resolve('string', $context));
// Property named: \phpDocumentor\Reflection\Types\Resolver::$keyWords
var_dump((string) $fqsenResolver->resolve('Types\\Resolver::$keyWords', $context));
Beispiel #6
0
 /**
  * Resolves the given FQSEN string into an FQSEN object.
  *
  * @param string $type
  *
  * @return Object_
  */
 private function resolveTypedObject($type, Context $context = null)
 {
     return new Object_($this->fqsenResolver->resolve($type, $context));
 }
 /**
  * @param string $relativeType
  *
  * @return string
  */
 private function convertToFullyQualifiedType($relativeType)
 {
     $type = $this->resolver->resolve($relativeType, $this->context);
     return str_replace('\\\\', '\\', $type);
 }