/** * @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; }
/** * 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; }
<?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 ::createForNamespace * @uses phpDocumentor\Reflection\Types\Context */ public function testAllOpeningBracesAreCheckedWhenSearchingForEndOfClass() { $php = '<?php namespace Foo; trait FooTrait {} trait BarTrait {} class FooClass { use FooTrait; public function bar() { echo "{$baz}"; echo "${baz}"; } } class BarClass { use BarTrait; public function bar() { echo "{$baz}"; echo "${baz}"; } } '; $fixture = new ContextFactory(); $context = $fixture->createForNamespace('Foo', $php); $this->assertSame([], $context->getNamespaceAliases()); }
public function create(array $nodes, $namespace) { $content = $this->printer->prettyPrintFile($nodes); $context = $this->contextFactory->createForNamespace($namespace ?: '\\', $content); return new Generator($context, $this->resolver); }