Example #1
0
 /**
  * {@inheritdoc}
  */
 public static function create($body, TypeResolver $typeResolver = null, DescriptionFactory $descriptionFactory = null, TypeContext $context = null)
 {
     Assert::stringNotEmpty($body);
     Assert::allNotNull([$typeResolver, $descriptionFactory]);
     $parts = preg_split('/(\\s+)/Su', $body, 3, PREG_SPLIT_DELIM_CAPTURE);
     $type = null;
     $variableName = '';
     $isVariadic = false;
     // if the first item that is encountered is not a variable; it is a type
     if (isset($parts[0]) && strlen($parts[0]) > 0 && $parts[0][0] !== '$') {
         $type = $typeResolver->resolve(array_shift($parts), $context);
         array_shift($parts);
     }
     // if the next item starts with a $ or ...$ it must be the variable name
     if (isset($parts[0]) && strlen($parts[0]) > 0 && ($parts[0][0] == '$' || substr($parts[0], 0, 4) === '...$')) {
         $variableName = array_shift($parts);
         array_shift($parts);
         if (substr($variableName, 0, 3) === '...') {
             $isVariadic = true;
             $variableName = substr($variableName, 3);
         }
         if (substr($variableName, 0, 1) === '$') {
             $variableName = substr($variableName, 1);
         }
     }
     $description = $descriptionFactory->create(implode('', $parts), $context);
     return new static($variableName, $type, $isVariadic, $description);
 }
Example #2
0
 /**
  * {@inheritdoc}
  */
 public static function create($body, TypeResolver $typeResolver = null, DescriptionFactory $descriptionFactory = null, TypeContext $context = null)
 {
     Assert::string($body);
     Assert::allNotNull([$typeResolver, $descriptionFactory]);
     $parts = preg_split('/\\s+/Su', $body, 2);
     $type = $typeResolver->resolve(isset($parts[0]) ? $parts[0] : '', $context);
     $description = $descriptionFactory->create(isset($parts[1]) ? $parts[1] : '', $context);
     return new static($type, $description);
 }
 /**
  * @param string[] $stringTypes
  * @param Context $context
  * @return \phpDocumentor\Reflection\Type[]
  */
 public function __invoke($stringTypes, Context $context)
 {
     $resolvedTypes = [];
     $resolver = new TypeResolver();
     foreach ($stringTypes as $stringType) {
         $resolvedTypes[] = $resolver->resolve($stringType, $context);
     }
     return $resolvedTypes;
 }
Example #4
0
 /**
  * @param ReflectionProperty $reflectionProperty
  * @param string             $contextClass
  *
  * @return \phpDocumentor\Reflection\Type[]
  */
 public function __invoke(ReflectionProperty $reflectionProperty, $contextClass)
 {
     $typeResolver = new TypeResolver();
     $context = (new ContextFactory())->createFromReflector($reflectionProperty);
     return array_map(function (Type $type) use($reflectionProperty, $contextClass) {
         return $this->expandSelfAndStaticTypes($type, $reflectionProperty, $contextClass);
     }, array_unique(array_filter(array_merge([], ...array_map(function (VarTag $varTag) use($typeResolver, $context) {
         return array_map(function ($type) use($typeResolver, $context) {
             return $typeResolver->resolve($type, $context);
         }, $varTag->getTypes());
     }, (new DocBlock($reflectionProperty, new DocBlock\Context($context->getNamespace(), $context->getNamespaceAliases())))->getTagsByName('var'))))));
 }
Example #5
0
 /**
  * @param ReflectionParameter $reflectionParameter
  * @param string              $contextClass
  *
  * @return Type[]
  */
 public function __invoke(ReflectionParameter $reflectionParameter, $contextClass)
 {
     $typeResolver = new TypeResolver();
     $context = (new ContextFactory())->createFromReflector($reflectionParameter);
     return array_map(function (Type $type) use($reflectionParameter, $contextClass) {
         return $this->expandSelfAndStaticTypes($type, $reflectionParameter, $contextClass);
     }, array_unique(array_filter(array_merge([], [], ...array_map(function (ParamTag $varTag) use($typeResolver, $context) {
         return array_map(function ($type) use($typeResolver, $context) {
             return $typeResolver->resolve($type, $context);
         }, $varTag->getTypes());
     }, $this->getParamTagsForParameter($reflectionParameter, $context))))));
 }
Example #6
0
 /**
  * {@inheritdoc}
  */
 public static function create($body, TypeResolver $typeResolver = null, DescriptionFactory $descriptionFactory = null, Context $context = null)
 {
     Assert::stringNotEmpty($body);
     Assert::allNotNull([$typeResolver, $descriptionFactory]);
     // 1. none or more whitespace
     // 2. optionally the keyword "static" followed by whitespace
     // 3. optionally a word with underscores followed by whitespace : as
     //    type for the return value
     // 4. then optionally a word with underscores followed by () and
     //    whitespace : as method name as used by phpDocumentor
     // 5. then a word with underscores, followed by ( and any character
     //    until a ) and whitespace : as method name with signature
     // 6. any remaining text : as description
     if (!preg_match('/^
             # Static keyword
             # Declares a static method ONLY if type is also present
             (?:
                 (static)
                 \\s+
             )?
             # Return type
             (?:
                 ([\\w\\|_\\\\]+)
                 \\s+
             )?
             # Legacy method name (not captured)
             (?:
                 [\\w_]+\\(\\)\\s+
             )?
             # Method name
             ([\\w\\|_\\\\]+)
             # Arguments
             \\(([^\\)]*)\\)
             \\s*
             # Description
             (.*)
         $/sux', $body, $matches)) {
         return null;
     }
     list(, $static, $returnType, $methodName, $arguments, $description) = $matches;
     $static = $static === 'static';
     $returnType = $typeResolver->resolve($returnType, $context);
     $description = $descriptionFactory->create($description, $context);
     $arguments = explode(',', $arguments);
     foreach ($arguments as &$argument) {
         $argument = explode(' ', trim($argument));
         if ($argument[0][0] === '$') {
             $argumentName = substr($argument[0], 1);
             $argumentType = new Void();
         } else {
             $argumentType = $typeResolver->resolve($argument[0], $context);
             $argumentName = '';
             if (isset($argument[1])) {
                 $argumentName = substr($argument[1], 1);
             }
         }
         $argument = ['name' => $argumentName, 'type' => $argumentType];
     }
     return new static($methodName, $arguments, $returnType, $static, $description);
 }
<?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));
 /**
  * @covers ::__construct
  * @covers ::resolve
  * @uses phpDocumentor\Reflection\Types\Context
  *
  * @expectedException \InvalidArgumentException
  */
 public function testExceptionIsThrownIfTypeIsNotAString()
 {
     $fixture = new TypeResolver();
     $fixture->resolve(['a'], new Context(''));
 }
<?php

use phpDocumentor\Reflection\TypeResolver;
require '../vendor/autoload.php';
$typeResolver = new TypeResolver();
// Will yield an object of type phpDocumentor\Types\Compound
var_export($typeResolver->resolve('string|integer'));
// Will return the string "string|int"
var_dump((string) $typeResolver->resolve('string|integer'));
<?php

use phpDocumentor\Reflection\Types\Context;
use phpDocumentor\Reflection\TypeResolver;
require '../vendor/autoload.php';
$typeResolver = new TypeResolver();
// Will use the namespace and aliases to resolve to \phpDocumentor\Types\Resolver|Mockery\MockInterface
$context = new Context('\\phpDocumentor', ['m' => 'Mockery']);
var_dump((string) $typeResolver->resolve('Types\\Resolver|m\\MockInterface', $context));