/**
  * Create line comment block.
  *
  * @param string $comment
  *   Comment without leading prefix.
  *
  * @return LineCommentBlockNode
  */
 public static function create($comment)
 {
     $block_comment = new LineCommentBlockNode();
     $comment = trim($comment);
     $lines = array_map('rtrim', explode("\n", $comment));
     foreach ($lines as $line) {
         $comment_node = new CommentNode(T_COMMENT, '// ' . $line . "\n");
         $block_comment->addChild($comment_node);
     }
     return $block_comment;
 }
    public function testCreate()
    {
        $comment = <<<'EOF'
hello
world

EOF;
        $line_comment_text = <<<'EOF'
// hello
// world

EOF;
        $comment_node = LineCommentBlockNode::create($comment);
        $this->assertEquals($line_comment_text, $comment_node->getText());
        $comment_node->addIndent('  ');
        $expected = <<<'EOF'
  // hello
  // world

EOF;
        $this->assertEquals($expected, $comment_node->getText());
        $comment_node->addIndent('  ');
        $expected = <<<'EOF'
    // hello
    // world

EOF;
        $this->assertEquals($expected, $comment_node->getText());
        $comment_node->removeIndent();
        $this->assertEquals($line_comment_text, $comment_node->getText());
    }
Exemple #3
0
 /**
  * Create line comment.
  *
  * @param string $comment
  *   Comment without leading prefix.
  * @return CommentNode|LineCommentBlockNode
  */
 public static function create($comment)
 {
     $comment = trim($comment);
     $nl_count = substr_count($comment, "\n");
     if ($nl_count > 1) {
         return LineCommentBlockNode::create($comment);
     } else {
         return new CommentNode(T_COMMENT, '// ' . $comment . "\n");
     }
 }
Exemple #4
0
 /**
  * Adds a line comment block above the statement.
  *
  * @param \Pharborist\LineCommentBlockNode|string $comment
  *  The comment to add.
  *
  * @return $this
  *
  * @throws \InvalidArgumentException
  */
 public function addCommentAbove($comment)
 {
     if ($comment instanceof LineCommentBlockNode) {
         $this->before($comment);
     } elseif (is_string($comment)) {
         $this->addCommentAbove(LineCommentBlockNode::create($comment));
     } else {
         throw new \InvalidArgumentException();
     }
     return $this;
 }