/** * @covers ::createForNamespace * @uses phpDocumentor\Reflection\Types\Context */ public function testReadsAliasesFromProvidedNamespaceAndContent() { $fixture = new ContextFactory(); $expected = ['m' => 'Mockery', 'DocBlock' => 'phpDocumentor\\Reflection\\DocBlock', 'Tag' => 'phpDocumentor\\Reflection\\DocBlock\\Tag', 'ReflectionClass' => 'ReflectionClass']; $context = $fixture->createForNamespace(__NAMESPACE__, file_get_contents(__FILE__)); $this->assertSame($expected, $context->getNamespaceAliases()); }
public function testTraitUseIsNotDetectedAsNamespaceUse() { $fixture = new ContextFactory(); $php = "<?php\n namespace Foo;\n\n trait FooTrait {}\n\n class FooClass {\n use FooTrait;\n }\n "; $fixture = new ContextFactory(); $context = $fixture->createForNamespace('Foo', $php); $this->assertSame([], $context->getNamespaceAliases()); }
/** * Given a property, attempt to find the type of the property. * * @param ReflectionProperty $reflectionProperty * @return Type[] */ public function __invoke(ReflectionProperty $reflectionProperty) { $contextFactory = new ContextFactory(); $context = $contextFactory->createForNamespace($reflectionProperty->getDeclaringClass()->getNamespaceName(), $reflectionProperty->getDeclaringClass()->getLocatedSource()->getSource()); $docBlock = DocBlockFactory::createInstance()->create($reflectionProperty->getDocComment(), new Context($context->getNamespace(), $context->getNamespaceAliases())); /* @var \phpDocumentor\Reflection\DocBlock\Tags\Var_ $varTag */ $resolvedTypes = []; $varTags = $docBlock->getTagsByName('var'); foreach ($varTags as $varTag) { $resolvedTypes = array_merge($resolvedTypes, (new ResolveTypes())->__invoke(explode('|', $varTag->getType()), $context)); } return $resolvedTypes; }
/** * {@inheritdoc} * * @param Type $class * @param string $name * @return Type */ public function getType(Type $class, $name) { $reflectionObject = new ReflectionClass($class->toString()); if (!$reflectionObject->hasProperty($name)) { return Type::fromString(null); } $reflectionProperty = $reflectionObject->getProperty($name); $contextFactory = new ContextFactory(); $context = $contextFactory->createFromReflector($reflectionProperty); $phpdoc = new DocBlock($reflectionProperty, $this->convertToDocBlockContext($context)); /** @var VarTag[] $vars */ $vars = $phpdoc->getTagsByName('var'); if (count($vars) === 0) { return Type::fromString(null); } return Type::fromString($vars[0]->getType()); }
/** * Given a property, attempt to find the type of the property * * @param PropertyNode $node * @param ReflectionProperty $reflectionProperty * @return Type[] */ public function __invoke(PropertyNode $node, ReflectionProperty $reflectionProperty) { $contextFactory = new ContextFactory(); $context = $contextFactory->createForNamespace($reflectionProperty->getDeclaringClass()->getNamespaceName(), $reflectionProperty->getDeclaringClass()->getLocatedSource()->getSource()); /* @var \PhpParser\Comment\Doc $comment */ if (!$node->hasAttribute('comments')) { return []; } $comment = $node->getAttribute('comments')[0]; $docBlock = new DocBlock($comment->getReformattedText(), new DocBlock\Context($context->getNamespace(), $context->getNamespaceAliases())); /* @var \phpDocumentor\Reflection\DocBlock\Tag\VarTag $varTag */ $resolvedTypes = []; $varTags = $docBlock->getTagsByName('var'); foreach ($varTags as $varTag) { $resolvedTypes = array_merge($resolvedTypes, (new ResolveTypes())->__invoke($varTag->getTypes(), $context)); } return $resolvedTypes; }
/** * Gets DocBlock from accessor or mutator method. * * @param string $class * @param string $ucFirstProperty * @param int $type * * @return array */ private function getDocBlockFromMethod($class, $ucFirstProperty, $type) { $prefixes = $type === self::ACCESSOR ? ReflectionExtractor::$accessorPrefixes : ReflectionExtractor::$mutatorPrefixes; $prefix = null; foreach ($prefixes as $prefix) { $methodName = $prefix . $ucFirstProperty; try { $reflectionMethod = new \ReflectionMethod($class, $methodName); if (self::ACCESSOR === $type && 0 === $reflectionMethod->getNumberOfRequiredParameters() || self::MUTATOR === $type && $reflectionMethod->getNumberOfParameters() >= 1) { break; } } catch (\ReflectionException $reflectionException) { // Try the next prefix if the method doesn't exist } } if (!isset($reflectionMethod)) { return; } return array($this->docBlockFactory->create($reflectionMethod, $this->contextFactory->createFromReflector($reflectionMethod)), $prefix); }
/** * @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));
<?php use phpDocumentor\Reflection\FqsenResolver; use phpDocumentor\Reflection\TypeResolver; use phpDocumentor\Reflection\Types\ContextFactory; require '../vendor/autoload.php'; require 'Classy.php'; $typeResolver = new TypeResolver(); $fqsenResolver = new FqsenResolver(); $contextFactory = new ContextFactory(); $context = $contextFactory->createFromReflector(new ReflectionMethod('My\\Example\\Classy', '__construct')); // 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)); // Class named: \My\Example\string // - Shows the difference between the FqsenResolver and TypeResolver; the FqsenResolver will assume // that the given value is not a type but most definitely a reference to another element. This is // because conflicts between type keywords and class names can exist and if you know a reference // is not a type but an element you can force that keywords are resolved. var_dump((string) $fqsenResolver->resolve('string', $context));
/** * @covers ::createFromReflector */ public function testEvalDClass() { eval(<<<PHP namespace Foo; class Bar { } PHP ); $fixture = new ContextFactory(); $context = $fixture->createFromReflector(new \ReflectionClass('Foo\\Bar')); $this->assertSame([], $context->getNamespaceAliases()); }
<?php use phpDocumentor\Reflection\FqsenResolver; use phpDocumentor\Reflection\TypeResolver; use phpDocumentor\Reflection\Types\ContextFactory; require '../vendor/autoload.php'; require 'Classy.php'; $typeResolver = new TypeResolver(); $fqsenResolver = new FqsenResolver(); $contextFactory = new ContextFactory(); $context = $contextFactory->createFromReflector(new ReflectionClass('My\\Example\\Classy')); // 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)); // Class named: \My\Example\string // - Shows the difference between the FqsenResolver and TypeResolver; the FqsenResolver will assume // that the given value is not a type but most definitely a reference to another element. This is // because conflicts between type keywords and class names can exist and if you know a reference // is not a type but an element you can force that keywords are resolved. var_dump((string) $fqsenResolver->resolve('string', $context));
public function create(array $nodes, $namespace) { $content = $this->printer->prettyPrintFile($nodes); $context = $this->contextFactory->createForNamespace($namespace ?: '\\', $content); return new Generator($context, $this->resolver); }