Overview

Namespaces

  • Pharborist
    • Constants
    • ControlStructures
    • Exceptions
    • Functions
    • Generators
    • Namespaces
    • Objects
    • Operators
    • Types
    • Variables

Classes

  • Pharborist\Variables\CompoundVariableNode
  • Pharborist\Variables\GlobalStatementNode
  • Pharborist\Variables\ReferenceVariableNode
  • Pharborist\Variables\StaticVariableNode
  • Pharborist\Variables\StaticVariableStatementNode
  • Pharborist\Variables\VariableNode
  • Pharborist\Variables\VariableVariableNode

Interfaces

  • Pharborist\Variables\VariableExpressionNode
  • Overview
  • Namespace
  • Class
  1: <?php
  2: namespace Pharborist;
  3: 
  4: /**
  5:  * A comment.
  6:  */
  7: class CommentNode extends HiddenNode {
  8:   use UncommentTrait;
  9: 
 10:   // Comment types
 11:   const DOC = '/**';
 12:   const BLOCK = '/*';
 13:   const SINGLE = '//';
 14:   const HASH = '#';
 15: 
 16:   /**
 17:    * @var string
 18:    */
 19:   protected $commentType;
 20: 
 21:   /**
 22:    * Construct token.
 23:    * @param int $type
 24:    * @param string $text
 25:    * @param SourcePosition $position
 26:    */
 27:   public function __construct($type, $text, $position = NULL) {
 28:     parent::__construct($type, $text, $position);
 29:     $prefix = substr($text, 0, 3);
 30:     if ($prefix === self::DOC) {
 31:       $this->commentType = self::DOC;
 32:     }
 33:     else {
 34:       $prefix = substr($prefix, 0, 2);
 35:       if ($prefix === self::BLOCK) {
 36:         $this->commentType = self::BLOCK;
 37:       }
 38:       elseif ($prefix === self::SINGLE) {
 39:         $this->commentType = self::SINGLE;
 40:       }
 41:       elseif ($prefix[0] === self::HASH) {
 42:         $this->commentType = self::HASH;
 43:       }
 44:     }
 45:   }
 46: 
 47:   /**
 48:    * Create line comment.
 49:    *
 50:    * @param string $comment
 51:    *   Comment without leading prefix.
 52:    * @return CommentNode|LineCommentBlockNode
 53:    */
 54:   public static function create($comment) {
 55:     $comment = trim($comment);
 56:     $nl_count = substr_count($comment, "\n");
 57:     if ($nl_count > 1) {
 58:       return LineCommentBlockNode::create($comment);
 59:     }
 60:     else {
 61:       return new CommentNode(T_COMMENT, '// ' . $comment . "\n");
 62:     }
 63:   }
 64: 
 65:   /**
 66:    * @return string
 67:    */
 68:   public function getCommentType() {
 69:     return $this->commentType;
 70:   }
 71: 
 72:   /**
 73:    * @return bool
 74:    */
 75:   public function isLineComment() {
 76:     return $this->commentType === self::SINGLE || $this->commentType === self::HASH;
 77:   }
 78: 
 79:   /**
 80:    * @return string
 81:    */
 82:   public function getCommentText() {
 83:     switch ($this->commentType) {
 84:       case self::SINGLE:
 85:       case self::HASH:
 86:         $comment_text = rtrim(substr($this->text, strlen($this->commentType)));
 87:         if ($comment_text[0] === ' ') {
 88:           $comment_text = substr($comment_text, 1);
 89:         }
 90:         return $comment_text;
 91:       case self::DOC:
 92:         $lines = explode("\n", $this->text);
 93:         if (count($lines) === 1) {
 94:           return trim(substr($this->text, 3, -2));
 95:         }
 96:         else {
 97:           $last_index = count($lines) - 1;
 98:           unset($lines[0]); // ignore first line
 99:           unset($lines[$last_index]); // ignore last line
100:           $comment = '';
101:           $first = TRUE;
102:           foreach ($lines as $line) {
103:             $line = trim($line);
104:             if ($line !== '' && $line[0] === '*') {
105:               if (!$first) {
106:                 $comment .= "\n";
107:               }
108:               else {
109:                 $first = FALSE;
110:               }
111:               $comment .= substr($line, 2);
112:             }
113:           }
114:           return $comment;
115:         }
116:       case self::BLOCK:
117:         return trim(substr($this->text, 2, -2));
118:       default:
119:         throw new \LogicException("Unhandled comment type in CommentNode::getCommentText()");
120:     }
121:   }
122: }
123: 
Pharborist API documentation generated by ApiGen