hasTag() public method

Checks if a tag of a certain type is present in this DocBlock.
public hasTag ( string $name ) : boolean
$name string Tag name to check for.
return boolean
Exemplo n.º 1
0
    public function testTagCaseSensitivity()
    {
        $fixture = <<<DOCBLOCK
/**
 * This is a short description.
 *
 * This is a long description.
 *
 * @method null something()
 * @Method({"GET", "POST"})
 */
DOCBLOCK;
        $object = new DocBlock($fixture);
        $this->assertEquals('This is a short description.', $object->getShortDescription());
        $this->assertEquals('This is a long description.', $object->getLongDescription()->getContents());
        $tags = $object->getTags();
        $this->assertCount(2, $tags);
        $this->assertTrue($object->hasTag('method'));
        $this->assertTrue($object->hasTag('Method'));
        $this->assertInstanceOf(__NAMESPACE__ . '\\DocBlock\\Tag\\MethodTag', $tags[0]);
        $this->assertInstanceOf(__NAMESPACE__ . '\\DocBlock\\Tag', $tags[1]);
        $this->assertNotInstanceOf(__NAMESPACE__ . '\\DocBlock\\Tag\\MethodTag', $tags[1]);
    }
 /**
  * @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;
 }
Exemplo n.º 3
0
 /**
  * @covers ::__construct
  * @covers ::hasTag
  * @uses \phpDocumentor\Reflection\DocBlock\Description
  * @expectedException \InvalidArgumentException
  */
 public function testExceptionIsThrownIfNameForCheckingTagsIsNotString()
 {
     $fixture = new DocBlock();
     $fixture->hasTag([]);
 }
Exemplo n.º 4
0
 protected function isValidNode(DocBlock $docblock)
 {
     return $docblock->hasTag('api-template') == false && $docblock->hasTag('api-group') == true && $docblock->hasTag('api-method') == true && $docblock->hasTag('api-uri') == true;
 }
Exemplo n.º 5
0
 public function beforeTraverse(array $nodes)
 {
     $node = null;
     $key = 0;
     foreach ($nodes as $k => $n) {
         if (!$n instanceof PHPParser_Node_Stmt_InlineHTML) {
             $node = $n;
             $key = $k;
             break;
         }
     }
     if ($node) {
         $comments = (array) $node->getAttribute('comments');
         // remove non-DocBlock comments
         $comments = array_values(array_filter($comments, function ($comment) {
             return $comment instanceof PHPParser_Comment_Doc;
         }));
         if (!empty($comments)) {
             try {
                 $docblock = new DocBlock((string) $comments[0], null, new Location($comments[0]->getLine()));
                 // the first DocBlock in a file documents the file if
                 // * it precedes another DocBlock or
                 // * it contains a @package tag and doesn't precede a class
                 //   declaration or
                 // * it precedes a non-documentable element (thus no include,
                 //   require, class, function, define, const)
                 if (count($comments) > 1 || !$node instanceof PHPParser_Node_Stmt_Class && !$node instanceof PHPParser_Node_Stmt_Interface && $docblock->hasTag('package') || !$this->isNodeDocumentable($node)) {
                     $this->doc_block = $docblock;
                     // remove the file level DocBlock from the node's comments
                     array_shift($comments);
                 }
             } catch (Exception $e) {
                 $this->log($e->getMessage(), Log::CRIT);
             }
         }
         // always update the comments attribute so that standard comments
         // do not stop DocBlock from being attached to an element
         $node->setAttribute('comments', $comments);
         $nodes[$key] = $node;
     }
     Dispatcher::getInstance()->dispatch('reflection.docblock-extraction.post', PostDocBlockExtractionEvent::createInstance($this)->setDocblock($this->doc_block));
     return $nodes;
 }
 public function store_class_constant(Node\Stmt\Class_ $class, Node\Stmt\ClassConst $constant)
 {
     if ($comments = $constant->getAttribute('comments')) {
         $phpdoc = new DocBlock($comments[0]->getText());
         $description = $phpdoc->getShortDescription();
         // short circuit @ignore functions
         if ($phpdoc->hasTag('ignore')) {
             return;
         }
     } else {
         $description = '';
     }
     $this->store_model('class_constants', array('constant' => $constant->consts[0]->name, 'class' => $class->name, 'namespace' => !empty($class->namespacedName) ? implode('\\', array_slice($class->namespacedName->parts, 0, -1)) : '', 'file' => $this->_current_file, 'line' => $constant->getLine(), 'type' => $this->get_type_for_node($constant->consts[0]->value), 'description' => $description));
 }
Exemplo n.º 7
0
 /**
  * Create options and arguments from the public properties on your command
  *
  * @param \ReflectionClass $class
  * @param                  $argsAdded
  *
  * @return null
  */
 protected function createOptionsFromPublic(\ReflectionClass $class, $argsAdded)
 {
     $properties = $class->getProperties(\ReflectionProperty::IS_PUBLIC);
     if (empty($properties)) {
         return null;
     }
     foreach ($properties as $property) {
         $propBlock = new DocBlock($property);
         $short = null;
         $description = $propBlock->getShortDescription();
         $mode = InputOption::VALUE_OPTIONAL;
         if ($propBlock->hasTag('short')) {
             $short = head($propBlock->getTagsByName('short'))->getDescription();
         }
         if ($propBlock->hasTag('description')) {
             $description = head($propBlock->getTagsByName('description'))->getDescription();
         }
         if ($propBlock->hasTag('valuerequired')) {
             $mode = InputOption::VALUE_REQUIRED;
         }
         if ($propBlock->hasTag('flag')) {
             $mode = InputOption::VALUE_NONE;
         }
         $this->addOption($property->getName(), $short, $mode, $description, $property->getValue($this));
     }
 }