Returns an array of tags matching the given name. If no tags are found
an empty array is returned.
public getTagsByName ( string $name ) : |
||
$name | string | String to search by. |
리턴 |
public function getMethodsDetails() { $methods = []; foreach ($this->reflection->getMethods(ReflectionMethod::IS_PUBLIC) as $method) { $docblock = new DocBlock($method); $data = ['shortDescription' => $docblock->getShortDescription(), 'longDescription' => $docblock->getLongDescription(), 'argumentsList' => $this->retriveParams($docblock->getTagsByName('param')), 'argumentsDescription' => $this->retriveParamsDescription($docblock->getTagsByName('param')), 'returnValue' => $this->retriveReturnValue($docblock->getTagsByName('return')), 'visibility' => join('', [$method->isFinal() ? 'final ' : '', 'public', $method->isStatic() ? ' static' : ''])]; if (strlen($data['shortDescription'])) { $methods[$method->getName()] = (object) $data; } } return $methods; }
public function resolveSignature($comment) { $docblock = new DocBlock((string) $comment); $return = $docblock->getTagsByName("return"); if (count($return) !== 1) { $returnType = new Type(Type::TYPE_UNKNOWN); } else { $returnType = Type::normalizeType($return[0]->getType()); } $params = $docblock->getTagsByName("param"); $paramTypes = []; foreach ($params as $key => $param) { $paramTypes[] = Type::normalizeType($params[$key]->getType()); } return new Signature($returnType, $paramTypes); }
protected function configure() { $reflection = new \ReflectionClass(get_called_class()); $baseNamespaceChunks = []; foreach (explode('\\', $reflection->getNamespaceName()) as $namespaceChunk) { $baseNamespaceChunks[] = $namespaceChunk; if ($namespaceChunk == consoleBase::COMMANDS_DIRECTORY) { break; } } $namespace = str_replace(implode('\\', $baseNamespaceChunks), '', get_called_class()); $namespace = trim($namespace, '\\'); $commandNameData = explode('\\', $namespace); $phpdoc = new DocBlock($reflection); /** @var DocBlock\Tag $tag */ $tag = reset($phpdoc->getTagsByName('consoleNs')); $commandNameValues = []; if ($tag) { $consoleNs = trim($tag->getDescription()); if (!empty($consoleNs)) { $commandNameValues[] = $consoleNs; } } foreach ($commandNameData as $commandNameValue) { $commandNameValues[] = $commandNameValue; } $this->setName(implode(':', $commandNameValues))->setDescription($phpdoc->getShortDescription())->setHelp($phpdoc->getLongDescription()); $this->defineArguments(); }
protected function getProperties($propertyName) { $reflection = new \ReflectionClass($this->model); $docBlock = new DocBlock($reflection); $properties = $docBlock->getTagsByName($propertyName); return $this->buildProperties($properties); }
/** * Extracts the package from the DocBlock. * * @param DocBlock $docBlock * * @return string|null */ protected function extractPackageFromDocBlock($docBlock) { $packageTags = $docBlock ? $docBlock->getTagsByName('package') : null; if (!$packageTags) { return null; } /** @var DocBlock\Tag $tag */ $tag = reset($packageTags); return trim($tag->getContent()); }
/** * @param string $method * @return array */ public function getMethodDecorators($method) { $reflectionMethod = new \ReflectionMethod($this->getInstance(), $method); $docBlock = new DocBlock($reflectionMethod); $decorators = []; foreach ($docBlock->getTagsByName(static::TAG_NAME) as $tag) { $decorators[] = $tag->getContent(); } return $decorators; }
/** * @param \ReflectionProperty $reflectionProperty * * @return string|null */ public function detectType(\ReflectionProperty $reflectionProperty) { $docBlock = new DocBlock($reflectionProperty->getDocComment()); $tags = $docBlock->getTagsByName('var'); if (count($tags) == 0) { return null; } /** @var VarTag $typeTag */ $typeTag = reset($tags); return $typeTag->getType(); }
/** * * @param ReflectionProperty $reflector * @return ParsePropertyBlockResult */ public function parsePropertyDocBlock(ReflectionProperty $reflector) { $phpdoc = new DocBlock($reflector->getDocComment()); /* @var $varTags VarTag[] */ $varTags = $phpdoc->getTagsByName("var"); /* @var $varTag VarTag */ $varTag = $varTags[0]; $result = new ParsePropertyBlockResult(); $result->description = $phpdoc->getShortDescription(); $result->type = (string) $varTag->getType(); return $result; }
/** * Discover Magical API * * @param ClassNode $node */ public function apply(ClassNode $node) { $parentClass = $node->getParentClass(); $reflectionClass = new \ReflectionClass($parentClass); $phpdoc = new DocBlock($reflectionClass->getDocComment()); $tagList = $phpdoc->getTagsByName('method'); foreach ($tagList as $tag) { $methodNode = new MethodNode($tag->getMethodName()); $methodNode->setStatic($tag->isStatic()); $node->addMethod($methodNode); } }
/** * 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 = new DocBlock($reflectionProperty->getDocComment(), 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; }
/** * Given a function and parameter, attempt to find the type of the parameter. * * @param ReflectionFunctionAbstract $function * @param ParamNode $node * @return Type[] */ public function __invoke(ReflectionFunctionAbstract $function, ParamNode $node) { $context = $this->createContextForFunction($function); $docBlock = new DocBlock($function->getDocComment(), new DocBlock\Context($context->getNamespace(), $context->getNamespaceAliases())); $paramTags = $docBlock->getTagsByName('param'); foreach ($paramTags as $paramTag) { /* @var $paramTag \phpDocumentor\Reflection\DocBlock\Tag\ParamTag */ if ($paramTag->getVariableName() === '$' . $node->name) { return (new ResolveTypes())->__invoke($paramTag->getTypes(), $context); } } return []; }
/** * Process comments into docblock objects. * @param array $docblocks */ protected function processDocBlocks(array $docblocks) { $this->docblocks = array(); foreach ($docblocks as $raw_docblock) { $docblock = new DocBlock($raw_docblock); $tags = $docblock->getTagsByName("var"); foreach ($tags as $tag) { if ($tag && ($description = $tag->getDescription())) { list($key, ) = explode(' ', $description); $this->docblocks[$key] = $docblock; } } } }
protected function setParentsPackage(\DOMElement $parent, \phpDocumentor\Reflection\DocBlock $docblock, $element) { /** @var \phpDocumentor\Reflection\DocBlock\Tag $package */ $package = current($docblock->getTagsByName('package')); /** @var \phpDocumentor\Reflection\DocBlock\Tag $subpackage */ $subpackage = current($docblock->getTagsByName('subpackage')); $package_name = ''; if ($package) { $package_name = str_replace(array('.', '_'), '\\', $package->getContent() . ($subpackage ? '\\' . $subpackage->getContent() : '')); } if (!$package_name) { $package_name = $element->getDefaultPackageName(); } $parent->setAttribute('package', $package_name); }
/** * {@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()); }
/** * @param \Reflector $reflector * @param string $tagName * * @return string|null */ public function resolveElementType(\Reflector $reflector, $tagName) { $docBlock = new DocBlock($reflector); $returnTags = $docBlock->getTagsByName($tagName); /** @var ReturnTag $returnTag */ $returnTag = reset($returnTags); $type = $returnTag->getType(); $isCollection = false; if ($extractedType = $this->extractTypeFromCollectionType($type)) { $isCollection = true; $type = $extractedType; } if (static::isTypeObject($type) && ($reflector instanceof \ReflectionMethod || $reflector instanceof \ReflectionProperty)) { $type = $this->resolveClassName($type, $reflector->getDeclaringClass()); } return $type . ($isCollection ? '[]' : ''); }
/** * 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; }
public static function servicesListProvider() { $sp = _elgg_services(); $class = new \ReflectionClass(get_class($sp)); $phpdoc = new DocBlock($class); $readonly_props = $phpdoc->getTagsByName('property-read'); /* @var \phpDocumentor\Reflection\DocBlock\Tag\PropertyReadTag[] $readonly_props */ foreach ($readonly_props as $prop) { $name = substr($prop->getVariableName(), 1); $type = $prop->getType(); // stuff set in PHPUnit bootstrap if ($name === 'mailer') { $type = InMemory::class; } $sets[] = [$name, $type]; } return $sets; }
/** * Discover Magical API * * @param ClassNode $node */ public function apply(ClassNode $node) { $parentClass = $node->getParentClass(); $reflectionClass = new \ReflectionClass($parentClass); $phpdoc = new DocBlock($reflectionClass->getDocComment()); $tagList = $phpdoc->getTagsByName('method'); $interfaces = $reflectionClass->getInterfaces(); foreach ($interfaces as $interface) { $phpdoc = new DocBlock($interface); $tagList = array_merge($tagList, $phpdoc->getTagsByName('method')); } foreach ($tagList as $tag) { $methodName = $tag->getMethodName(); if (!$reflectionClass->hasMethod($methodName)) { $methodNode = new MethodNode($tag->getMethodName()); $methodNode->setStatic($tag->isStatic()); $node->addMethod($methodNode); } } }
/** * Normalize the return tag (make full namespace, replace interfaces) */ protected function normalizeReturn() { //Get the return type and adjust them for beter autocomplete $returnTags = $this->phpdoc->getTagsByName('return'); if ($returnTags) { /** @var ReturnTag $tag */ $tag = reset($returnTags); // Get the expanded type $returnValue = $tag->getType(); // Replace the interfaces foreach ($this->interfaces as $interface => $real) { $returnValue = str_replace($interface, $real, $returnValue); } // Set the changed content $tag->setContent($returnValue . ' ' . $tag->getDescription()); $this->return = $returnValue; } else { $this->return = null; } }
/** * {@inheritDoc} */ public function loadMetadata($class) { $properties = Reflection::getClassProperties($class, true); $metadataProperties = []; foreach ($properties as $property) { $docBlock = new DocBlock($property); $docVarTags = $docBlock->getTagsByName('var'); if (count($docVarTags)) { /** @var \phpDocumentor\Reflection\DocBlock\Tag\VarTag $docVarTag */ $docVarTag = array_pop($docVarTags); $types = $docVarTag->getTypes(); $types = array_map(function ($type) { return ltrim($type, '\\'); }, $types); if (count($types)) { $propertyMetadata = new PropertyMetadata($types); $metadataProperties[$property->getName()] = $propertyMetadata; } } } $classMetadata = new ClassMetadata($metadataProperties); return $classMetadata; }
/** * Extract response property * * @param ActionInterface $action * @param CallableInterface $callable * @param \ReflectionProperty $property * * @return ResponseProperty */ protected function extractResponseProperty(ActionInterface $action, CallableInterface $callable, \ReflectionProperty $property) { $docBlock = new DocBlock($property); $description = $docBlock->getShortDescription(); $type = 'mixed'; $child = null; $varTags = $docBlock->getTagsByName('var'); if (count($varTags)) { /** @var \phpDocumentor\Reflection\DocBlock\Tag\VarTag $varTag */ $varTag = $varTags[0]; $varTypes = $varTag->getTypes(); $varType = null; if (count($varTypes)) { $varType = array_pop($varTypes); } switch (true) { case 'int' == $varType: case 'integer' == $varType: $type = 'int'; break; case 'string' == $varType: $type = 'string'; break; case 'float' == $varType: case 'double' == $varType: $type = 'float'; break; case 'boolean' == $varType: case 'bool' == $varType: $type = 'bool'; break; case 'DateTime' == ltrim($varType, '\\'): $type = 'datetime'; break; default: if ($varType && class_exists($varType)) { $type = 'object'; $childProperties = $this->getPropertiesForClass($action, $callable, $varType); $child = []; foreach ($childProperties as $childProperty) { $child[$childProperty->getName()] = $this->extractResponseProperty($action, $callable, $childProperty); } break; } else { if ($varType && substr($varType, strlen($varType) - 2) == '[]') { $childClass = substr($varType, 0, strlen($varType) - 2); $type = 'collection'; $childProperties = $this->getPropertiesForClass($action, $callable, $childClass); $child = []; foreach ($childProperties as $childProperty) { $child[$childProperty->getName()] = $this->extractResponseProperty($action, $callable, $childProperty); } break; } } $this->tryFormatPhpType($varTag, $varType); } } $responseProperty = new ResponseProperty($property->getName(), $type, $description, $child); return $responseProperty; }
/** * @param MessageInterface $object * @param array $data * * @return MessageInterface */ private function _doHydrate(MessageInterface $object, $data) { $reflected = new \ReflectionClass($object); if ($object instanceof MessageCollectionInterface && $object instanceof ArrayCollectionInterface) { foreach ($data->{$object->getDataMemberName()} as $singleData) { $class = $object->createChildClass(); $object->add($this->_doHydrate($class, $singleData)); } } foreach ($data as $key => $value) { if ($reflected->hasProperty($key)) { $property = $reflected->getProperty($key); $setter = $this->generateSetter($key); $doc = new DocBlock($property); if ($doc->hasTag('var')) { $tags = $doc->getTagsByName('var'); if (count($tags) !== 1) { throw new RuntimeException('Property "' . $property->getName() . '" of class ' . $property->getDeclaringClass() . 'has more @var tags. Only one is allowed.'); } $type = $tags[0]->getType(); switch (true) { /** * Internal type Enum */ /*case $type === '\\Enum': $getter = $this->generateGetter($key); $object->{$getter}()->setValue($value); break;*/ /** * All basic types */ case in_array(strtolower($type), $this->basicTypes, false): $object->{$setter}($value); break; /** * Object types - special cases first */ /** * Object types - special cases first */ case $type === '\\DateTime': $class = new \DateTime($value); $object->{$setter}($class); break; case $type === '\\DateTimeZone': if (empty($value)) { continue; } $class = new \DateTimeZone($value); $object->{$setter}($class); break; case !is_array($value) && class_exists($type, true): $class = new $type($value); $object->{$setter}($class); break; /** * Try to find class and hydrate object */ /** * Try to find class and hydrate object */ default: $possibleClassNames = []; $possibleClassNames[] = $this->baseNS . $type; $possibleClassNames[] = $reflected->getNamespaceName() . $type; $possibleClassNames[] = $type; foreach ($possibleClassNames as $className) { if (class_exists($className, true)) { $class = new $className(); $hydrated = $this->_doHydrate($class, $value); $object->{$setter}($hydrated); continue 2; } } /** * Class not found, we use raw $value. */ $object->{$setter}($value); break; } } } elseif ($key === '_links') { foreach ($value as $link) { $object->addMethod($link->method, $link->rel, $link->href); } } } return $object; }
/** * @param \ReflectionClass $reflectionClass * * @return LegacyMethodTag[] */ public function getTagList(\ReflectionClass $reflectionClass) { $phpdoc = new DocBlock($reflectionClass->getDocComment()); return $phpdoc->getTagsByName('method'); }
/** * {@inheritDoc} */ public function extract(ActionInterface $action, CallableInterface $callable) { /** @var \ReflectionClass $requestClass */ $requestClass = null; $inputParameters = $callable->getReflection()->getParameters(); foreach ($inputParameters as $parameter) { if ($class = $parameter->getClass()) { if ($class->implementsInterface('FiveLab\\Component\\Api\\Request\\RequestInterface')) { $requestClass = $class; break; } } } if (!$requestClass) { return []; } $requestParameters = []; $requestObject = $requestClass->newInstance(); $objectMetadata = $this->objectMapper->getMetadataFactory()->load($requestObject, ObjectMetadata::DEFAULT_GROUP); if (!$objectMetadata) { // Can not load metadata for object return []; } foreach ($objectMetadata->getProperties() as $property) { if (!$property->reflection) { $property->reflection = new \ReflectionProperty($requestClass->getName(), $property->getPropertyName()); } $docBlock = new DocBlock($property->reflection); $content = $docBlock->getShortDescription(); $typeTags = $docBlock->getTagsByName('var'); $type = null; if ($typeTags) { /** @var \phpDocumentor\Reflection\DocBlock\Tag\VarTag $typeTag */ $typeTag = array_pop($typeTags); $type = $typeTag->getType(); //$type = $this->formatPHPType($type); } $defaultValue = null; if ($property->reflection->isDefault()) { if (!$property->reflection->isPublic()) { $property->reflection->setAccessible(true); } $defaultValue = $property->reflection->getValue($requestObject); } $parameter = new Parameter($property->getFieldName(), $type, $this->isPropertyRequired($property->reflection), $content, $defaultValue); $requestParameters[$property->getFieldName()] = $parameter; } return $requestParameters; }
public static function isInherit(DocBlock $docBlock) { return $docBlock->getTagsByName('inheritdoc') || $docBlock->getTagsByName('inherited'); }
/** * @param DocBlock $docBlock * @param string $name * @return null|string */ protected function getTag($docBlock, $name) { $tags = $docBlock->getTagsByName($name); if ($tags) { /** @var DocBlock\Tag $tag */ $tag = reset($tags); return $tag->getContent(); } return null; }
/** * @depends testConstructWithTagsOnly * @covers \phpDocumentor\Reflection\DocBlock::getTagsByName * * @return void */ public function testGetTagsByNameMultipleMatch() { $fixture = <<<DOCBLOCK /** * @param string * @param int * @return void */ DOCBLOCK; $object = new DocBlock($fixture); $this->assertEmpty($object->getTagsByName('material_unit')); $this->assertCount(1, $object->getTagsByName('return')); $this->assertCount(2, $object->getTagsByName('param')); }
/** * @param Node $node * * @return null|string */ protected function getDeprecatedDocComment(Node $node) { try { $docBlock = new DocBlock((string) $node->getDocComment()); /** @var DocBlock\Tag\DeprecatedTag[] $deprecatedTag */ $deprecatedTag = $docBlock->getTagsByName('deprecated'); if (0 === count($deprecatedTag)) { return; } $comment = $deprecatedTag[0]->getContent(); return preg_replace('/[[:blank:]]+/', ' ', $comment); } catch (\Exception $e) { return; } }
protected function assertTagsEqual($object, $tagName, array $expectedTagToType) { $docblock = new DocBlock(new \ReflectionObject($object)); $actualTagToType = []; foreach ($docblock->getTagsByName($tagName) as $tag) { $actualTagToType[$tag->getVariableName()] = $tag->getType(); } $this->assertEquals($expectedTagToType, $actualTagToType); }
protected function acquireRelations() { // getting the relations – on the other hand – is not, since they need to be loaded // to be visible. thus, reflection is used to find the methods that return relation // objects $reflectionClass = new \ReflectionClass($this->model); foreach ($reflectionClass->getMethods() as $method) { $docBlock = new DocBlock($method); $returnTag = $docBlock->getTagsByName('return'); if (count($returnTag) >= 1 && $this->isEloquentRelation($returnTag[0]->getType())) { $this->relations[] = $method->getName(); } } }