getDocComment() 공개 메소드

The doc comment has to be the last comment associated with the node.
public getDocComment ( ) : null | Comment\Doc
리턴 null | Comment\Doc Doc comment object or null
예제 #1
0
 function enterNode(\PhpParser\Node $node)
 {
     if ($node instanceof \PhpParser\Node\Stmt\Namespace_) {
         $this->addNamespace(array('namespace' => $node->name->toString(), 'line' => $node->getLine(), 'file' => $this->file));
     }
     if ($node instanceof \PhpParser\Node\Stmt\Use_) {
         foreach ($node->uses as $use) {
             $this->addUse(array('name' => $use->name, 'alias' => $use->alias));
         }
     }
     if ($node instanceof \PhpParser\Node\Stmt\Class_) {
         $this->addClass(array('name' => $node->name, 'extends' => $node->extends, 'implements' => $node->implements, 'abstract' => $node->isAbstract(), 'final' => $node->isFinal(), 'comment' => $node->getDocComment(), 'doc' => $this->doc_comment_parser->parse($node->getDocComment()), 'line' => $node->getLine(), 'file' => $this->file));
     }
     if ($node instanceof \PhpParser\Node\Stmt\Interface_) {
         $this->addInterface(array('name' => $node->name, 'extends' => $node->extends, 'comment' => $node->getDocComment(), 'doc' => $this->doc_comment_parser->parse($node->getDocComment()), 'line' => $node->getLine(), 'file' => $this->file));
     }
     if ($node instanceof \PhpParser\Node\Stmt\ClassMethod) {
         $this->addClassMethod(array('byRef' => $node->byRef, 'name' => $node->name, 'params' => $node->params, 'returnType' => $node->returnType, 'public' => $node->isPublic(), 'protected' => $node->isProtected(), 'private' => $node->isPrivate(), 'abstract' => $node->isAbstract(), 'final' => $node->isFinal(), 'static' => $node->isStatic(), 'comment' => $node->getDocComment(), 'doc' => $this->doc_comment_parser->parse($node->getDocComment()), 'line' => $node->getLine(), 'file' => $this->file));
     }
     if ($node instanceof \PhpParser\Node\Stmt\Property) {
         $this->last_property = $node;
     }
     if ($node instanceof \PhpParser\Node\Stmt\PropertyProperty) {
         $this->addClassProperty(array('name' => $node->name, 'default' => $node->default, 'public' => $this->last_property->isPublic(), 'protected' => $this->last_property->isProtected(), 'private' => $this->last_property->isPrivate(), 'static' => $this->last_property->isStatic(), 'comment' => $node->getDocComment(), 'doc' => $this->doc_comment_parser->parse($node->getDocComment()), 'line' => $node->getLine(), 'file' => $this->file));
     }
     // $this->logger->info(get_class($node));
 }
예제 #2
0
 /**
  * Check all nodes
  *
  * @param  Node $node
  * @return void
  **/
 public function enterNode(Node $node)
 {
     // Skip nodes without comments.
     if (!$node->hasAttribute("comments")) {
         return;
     }
     // Check if annotations should be preserved. Only nodes with actual
     // doc comment blocks are processed.
     $comments = [];
     if ($this->preserveAnnotations) {
         $docComment = $node->getDocComment();
         if ($docComment) {
             $text = $docComment->getText();
             // Check if it is a doc comment.
             if (strpos($text, "/**") !== false) {
                 $text = $this->stripComment($text);
                 if ($text) {
                     $comments = [new Comment($text)];
                 }
             }
         }
     }
     // Remove (or set) comments.
     $node->setAttribute("comments", $comments);
     return $node;
 }
예제 #3
0
 public function leaveNode(Node $node)
 {
     if ($node instanceof Node\Stmt\UseUse) {
         $this->uses['@' . $node->alias] = '@' . $node->name->toString();
     }
     if ($comment = $node->getDocComment()) {
         $text = $comment->getText();
         $text = str_replace(array_keys($this->uses), array_values($this->uses), $text);
         $comment->setText($text);
     }
 }
예제 #4
0
 /**
  * @param \PhpParser\Node $node
  * @return string|null
  */
 public static function getDocComment(Node $node)
 {
     $phpDoc = $node->getDocComment();
     $phpDocText = null;
     if ($phpDoc !== null) {
         return $phpDoc->getText();
     }
     $comments = $node->getAttribute('comments');
     if ($comments === null) {
         return null;
     }
     return $comments[count($comments) - 1]->getText();
 }
 /**
  * @param Node $node
  * @return null|string
  */
 private function getDocCommentForNode(Node $node)
 {
     // check if there is a doc comment for the ID argument
     // ->trans(/** @Desc("FOO") */ 'my.id')
     if (null !== ($comment = $node->args[0]->getDocComment())) {
         return $comment->getText();
     }
     // this may be placed somewhere up in the hierarchy,
     // -> /** @Desc("FOO") */ trans('my.id')
     // /** @Desc("FOO") */ ->trans('my.id')
     // /** @Desc("FOO") */ $translator->trans('my.id')
     if (null !== ($comment = $node->getDocComment())) {
         return $comment->getText();
     } elseif (null !== $this->previousNode && $this->previousNode->getDocComment() !== null) {
         $comment = $this->previousNode->getDocComment();
         return is_object($comment) ? $comment->getText() : $comment;
     }
     return null;
 }
예제 #6
0
 /**
  * @param Node $node
  *
  * @return RefComment|null
  */
 private function createDocComment(Node $node)
 {
     $docComment = $node->getDocComment();
     if (!$docComment) {
         return null;
     }
     $ref = new RefComment();
     $ref->text = $docComment->getText();
     $ref->startLine = $docComment->getLine();
     $ref->endLine = $node->getLine() - 1;
     return $ref;
 }
예제 #7
0
 private function mapAttributes(Node $expr)
 {
     return array_merge(["filename" => $this->fileName, "doccomment" => $expr->getDocComment()], $expr->getAttributes());
 }
예제 #8
0
 private function repairComments(Node $node)
 {
     $comment = $node->getDocComment();
     if ($comment && !empty($this->classStack)) {
         $regex = "(@(param|return|var|type)\\s+(\\S+))i";
         $comment->setText(preg_replace_callback($regex, function ($match) {
             $type = $match[2];
             $type = preg_replace('((?<=^|\\|)((?i:self)|\\$this)(?=\\[|$|\\|))', end($this->classStack), $type);
             return '@' . $match[1] . ' ' . $type;
         }, $comment->getText()));
     }
 }
 public function enterNode(Node $node)
 {
     if ($node instanceof Node\Stmt\Namespace_) {
         $this->namespace = implode('\\', $node->name->parts);
         return;
     }
     if ($node instanceof Node\Stmt\Class_) {
         $name = '' === $this->namespace ? $node->name : $this->namespace . '\\' . $node->name;
         if (!class_exists($name)) {
             return;
         }
         $ref = new \ReflectionClass($name);
         if (!$ref->isSubclassOf('Symfony\\Component\\Security\\Core\\Exception\\AuthenticationException') && $ref->name !== 'Symfony\\Component\\Security\\Core\\Exception\\AuthenticationException') {
             return;
         }
         if (!$ref->hasMethod('getMessageKey')) {
             return;
         }
         $this->inAuthException = true;
         return;
     }
     if (!$this->inAuthException) {
         return;
     }
     if ($node instanceof Node\Stmt\ClassMethod) {
         if ('getmessagekey' === strtolower($node->name)) {
             $this->inGetMessageKey = true;
         }
         return;
     }
     if (!$this->inGetMessageKey) {
         return;
     }
     if (!$node instanceof Node\Stmt\Return_) {
         return;
     }
     $ignore = false;
     $desc = $meaning = null;
     if ($docComment = $node->getDocComment()) {
         foreach ($this->docParser->parse($docComment->getText(), 'file ' . $this->file . ' near line ' . $node->getLine()) as $annot) {
             if ($annot instanceof Ignore) {
                 $ignore = true;
             } else {
                 if ($annot instanceof Desc) {
                     $desc = $annot->text;
                 } else {
                     if ($annot instanceof Meaning) {
                         $meaning = $annot->text;
                     }
                 }
             }
         }
     }
     if (!$node->expr instanceof Node\Scalar\String_) {
         if ($ignore) {
             return;
         }
         $message = sprintf('Could not extract id from return value, expected scalar string but got %s (in %s on line %d).', get_class($node->expr), $this->file, $node->expr->getLine());
         if ($this->logger) {
             $this->logger->error($message);
             return;
         }
         throw new RuntimeException($message);
     }
     $message = Message::create($node->expr->value, $this->domain)->setDesc($desc)->setMeaning($meaning)->addSource(new FileSource((string) $this->file, $node->expr->getLine()));
     $this->catalogue->add($message);
 }
예제 #10
0
 public function leaveNode(Node $node)
 {
     /*if ($node instanceof Node\Name) {
     			return new Node\Name($node->toString('_'));
     		}*/
     if ($node instanceof Stmt\Class_ || $node instanceof Stmt\Interface_ || $node instanceof Stmt\Trait_ || $node instanceof Stmt\Function_) {
         $item = [];
         $itemName = $node->namespacedName->toString();
         $item['name'] = $this->ensureUtf8($itemName);
         $comment = $node->getDocComment();
         if ($comment) {
             $item['phpDoc'] = $this->ensureUtf8($node->getDocComment()->getText());
         }
         $item['packageName'] = $this->package['packageName'];
         $item['packageVersion'] = $this->package['packageVersion'];
         $item['fileName'] = $this->fileName;
         unset($this->uses[$itemName]);
         $item['uses'] = array_keys($this->uses);
         if ($node instanceof Stmt\Class_) {
             $item['type'] = ItemDao::TYPE_CLASS;
             $inherits = [];
             if ($node->extends) {
                 $inherits[] = $this->ensureUtf8($node->extends->toString());
             }
             foreach ($node->implements as $implement) {
                 $inherits[] = $this->ensureUtf8($implement->toString());
             }
             $item['inherits'] = $inherits;
         } elseif ($node instanceof Stmt\Interface_) {
             $item['type'] = ItemDao::TYPE_INTERFACE;
             $inherits = [];
             foreach ($node->extends as $extend) {
                 $inherits[] = $this->ensureUtf8($extend->toString());
             }
             $item['inherits'] = $inherits;
         } elseif ($node instanceof Stmt\Trait_) {
             $item['type'] = ItemDao::TYPE_TRAIT;
         } elseif ($node instanceof Stmt\Function_) {
             $item['type'] = ItemDao::TYPE_FUNCTION;
         }
         $this->itemDao->save($item);
         /*} elseif ($node instanceof Node\Name) {
         		$this->uses[$node->toString()] = true;*/
     } elseif ($node instanceof Stmt\Const_) {
         foreach ($node->consts as $const) {
             $this->uses[$const->namespacedName->toString()] = true;
         }
     } elseif ($node instanceof Expr\StaticCall || $node instanceof Expr\StaticPropertyFetch || $node instanceof Expr\ClassConstFetch || $node instanceof Expr\New_ || $node instanceof Expr\Instanceof_) {
         if ($node->class instanceof Name) {
             $className = $node->class->toString();
             $lowerClassName = strtolower($className);
             if ($lowerClassName != "self" && $lowerClassName != "parent" && $lowerClassName != "parent") {
                 $this->uses[$className] = true;
             }
         }
     } elseif ($node instanceof Stmt\Catch_) {
         $this->uses[$node->type->toString()] = true;
     } elseif ($node instanceof Stmt\TraitUse) {
         foreach ($node->traits as $trait) {
             $this->uses[$trait->toString()] = true;
         }
     } elseif ($node instanceof Node\Param && $node->type instanceof Name) {
         $this->uses[$node->type->toString()] = true;
     }
 }
 /**
  * @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;
     }
 }
예제 #12
0
 /**
  * Create a new tag and add it to the tags list.
  *
  * @param string $type
  * @param string $name
  * @return Tag
  */
 public function createTag($type, $name, Node $node)
 {
     return $this->tags[] = new Tag($name, $type, Tag::DEFINITION, $node->getAttribute('startLine'), $node->getAttribute('endLine'), $node->getAttribute('startFilePos'), $node->getAttribute('endFilePos'), $node->getDocComment());
 }
 public function it_doesnt_modify_non_class_method_nodes(Node $node)
 {
     $node->getDocComment()->shouldNotBeCalled();
     $this->leaveNode($node);
 }
 public function enterNode(PhpParser\Node $node)
 {
     if (stripos($node->getDocComment(), '@codeCoverageIgnore') !== FALSE) {
         return PhpParser\NodeTraverser::DONT_TRAVERSE_CHILDREN;
     }
 }
예제 #15
0
 /**
  * Called when entering a node.
  *
  * Return value semantics:
  *  * null:      $node stays as-is
  *  * otherwise: $node is set to the return value
  *
  * @param Node $node
  *
  * @return null|Node
  */
 public function enterNode(Node $node)
 {
     if ($node instanceof Namespace_) {
         $this->namespace = '\\' . $node->name;
         $this->namespaceLine = $node->getLine();
     } elseif ($node instanceof Use_) {
         $use_count = 0;
         foreach ($node->uses as $use) {
             if ($use instanceof UseUse) {
                 if (!$this->resolver->isValid("\\{$use->name}")) {
                     $this->result->error(ErrorType::UNABLE_TO_RESOLVE_USE, $use->getLine(), "Use '\\{$use->name}' could not be resolved");
                 }
                 if (isset($this->aliases[$use->alias])) {
                     $line = $this->aliases[$use->alias]->getNode()->getLine();
                     $this->result->error(ErrorType::DUPLICATE_ALIAS, $use->getLine(), "Alias '{$use->alias}' is already in use on line {$line}");
                 }
                 $this->aliases[$use->alias] = new UseTracker($use->alias, (string) $use->name, $use);
                 $this->useStatements[] = $use;
             } else {
                 // I don't know if this error can ever be generated, as it should be a parse error...
                 $this->result->error(ErrorType::MALFORMED_USE, $use->getLine(), "Malformed use statement");
                 return;
             }
             $use_count++;
         }
         if ($use_count > 1) {
             $this->result->error(ErrorType::MULTI_STATEMENT_USE, $node->getLine(), "Multiple uses in one statement is discouraged");
         }
     } else {
         if ($node instanceof New_) {
             $this->validateMethodAccess($node->getLine(), $node->class, '__construct');
         }
         if ($node instanceof StaticCall) {
             if ($node->name !== '__construct') {
                 $this->validateMethodExists($node->getLine(), $node->class, $node->name);
             }
             $this->validateMethodAccess($node->getLine(), $node->class, $node->name);
         }
         if (isset($node->class) && $node->class instanceof Name) {
             $this->resolveType($node->getLine(), $node->class);
         }
         if (isset($node->traits)) {
             foreach ($node->traits as $trait) {
                 $this->resolveType($node->getLine(), $trait);
             }
         }
         if (isset($node->implements)) {
             foreach ($node->implements as $implements) {
                 $this->resolveType($node->getLine(), $implements);
             }
         }
         if (isset($node->extends)) {
             if ($node->extends instanceof Name) {
                 $this->resolveType($node->getLine(), $node->extends);
             } else {
                 foreach ($node->extends as $extends) {
                     $this->resolveType($node->getLine(), $extends);
                 }
             }
         }
         if (isset($node->type) && $node->type instanceof Name) {
             $this->resolveType($node->getLine(), $node->type);
         }
         if ($node instanceof ClassMethod && $node->getReturnType() instanceof Name) {
             $this->resolveType($node->getLine(), $node->getReturnType());
         }
         if ($node instanceof ClassMethod or $node instanceof Function_ or $node instanceof Property or $node instanceof Class_ or $node instanceof Variable) {
             /** @var $docblock Doc */
             if ($docblock = $node->getDocComment()) {
                 $annotations = $this->annotationParser->parseString($docblock->getText());
                 foreach ($annotations as $annotation) {
                     if ($annotation instanceof DoctrineAnnotation) {
                         $this->resolveDoctrineComment($docblock->getLine() - 1, $annotation);
                     } elseif ($annotation instanceof NonDoctrineAnnotation) {
                         $this->resolveNonDoctrineComment($docblock->getLine() - 1, $annotation);
                     }
                 }
             }
         }
         if ($node instanceof Catch_) {
             foreach ($node->types as $type) {
                 $this->resolveType($node->getLine(), $type);
             }
         }
         // eclipse can only handle inline type hints in a normal comment block
         if ($node instanceof Variable) {
             $comments = $node->getAttribute('comments');
             if (is_array($comments)) {
                 $phpVariableRegex = '[a-zA-Z_\\x7f-\\xff][a-zA-Z0-9_\\x7f-\\xff]*';
                 // regex for matching /* @var $variable Class */
                 $eclipseInlineTypeHintRegex = '~^/\\* @var \\$' . $phpVariableRegex . ' (\\\\?' . $phpVariableRegex . '(\\\\' . $phpVariableRegex . ')*) \\*/$~';
                 foreach ($comments as $comment) {
                     /* @var $comment Comment */
                     $matches = [];
                     $match = preg_match($eclipseInlineTypeHintRegex, $comment->getText(), $matches);
                     if ($match === 1) {
                         $className = $matches[1];
                         $this->resolveAnnotatedType($comment->getLine(), $className);
                     }
                 }
             }
         }
     }
     $this->nodeStack[] = $node;
 }