| 1: | <?php declare(strict_types=1); |
| 2: | |
| 3: | namespace PhpParser; |
| 4: | |
| 5: | class Comment implements \JsonSerializable |
| 6: | { |
| 7: | protected $text; |
| 8: | protected $startLine; |
| 9: | protected $startFilePos; |
| 10: | protected $startTokenPos; |
| 11: | protected $endLine; |
| 12: | protected $endFilePos; |
| 13: | protected $endTokenPos; |
| 14: | |
| 15: | |
| 16: | |
| 17: | |
| 18: | |
| 19: | |
| 20: | |
| 21: | |
| 22: | |
| 23: | public function __construct( |
| 24: | string $text, |
| 25: | int $startLine = -1, int $startFilePos = -1, int $startTokenPos = -1, |
| 26: | int $endLine = -1, int $endFilePos = -1, int $endTokenPos = -1 |
| 27: | ) { |
| 28: | $this->text = $text; |
| 29: | $this->startLine = $startLine; |
| 30: | $this->startFilePos = $startFilePos; |
| 31: | $this->startTokenPos = $startTokenPos; |
| 32: | $this->endLine = $endLine; |
| 33: | $this->endFilePos = $endFilePos; |
| 34: | $this->endTokenPos = $endTokenPos; |
| 35: | } |
| 36: | |
| 37: | |
| 38: | |
| 39: | |
| 40: | |
| 41: | |
| 42: | public function getText() : string { |
| 43: | return $this->text; |
| 44: | } |
| 45: | |
| 46: | |
| 47: | |
| 48: | |
| 49: | |
| 50: | |
| 51: | public function getStartLine() : int { |
| 52: | return $this->startLine; |
| 53: | } |
| 54: | |
| 55: | |
| 56: | |
| 57: | |
| 58: | |
| 59: | |
| 60: | public function getStartFilePos() : int { |
| 61: | return $this->startFilePos; |
| 62: | } |
| 63: | |
| 64: | |
| 65: | |
| 66: | |
| 67: | |
| 68: | |
| 69: | public function getStartTokenPos() : int { |
| 70: | return $this->startTokenPos; |
| 71: | } |
| 72: | |
| 73: | |
| 74: | |
| 75: | |
| 76: | |
| 77: | |
| 78: | public function getEndLine() : int { |
| 79: | return $this->endLine; |
| 80: | } |
| 81: | |
| 82: | |
| 83: | |
| 84: | |
| 85: | |
| 86: | |
| 87: | public function getEndFilePos() : int { |
| 88: | return $this->endFilePos; |
| 89: | } |
| 90: | |
| 91: | |
| 92: | |
| 93: | |
| 94: | |
| 95: | |
| 96: | public function getEndTokenPos() : int { |
| 97: | return $this->endTokenPos; |
| 98: | } |
| 99: | |
| 100: | |
| 101: | |
| 102: | |
| 103: | |
| 104: | |
| 105: | |
| 106: | |
| 107: | public function getLine() : int { |
| 108: | return $this->startLine; |
| 109: | } |
| 110: | |
| 111: | |
| 112: | |
| 113: | |
| 114: | |
| 115: | |
| 116: | |
| 117: | |
| 118: | public function getFilePos() : int { |
| 119: | return $this->startFilePos; |
| 120: | } |
| 121: | |
| 122: | |
| 123: | |
| 124: | |
| 125: | |
| 126: | |
| 127: | |
| 128: | |
| 129: | public function getTokenPos() : int { |
| 130: | return $this->startTokenPos; |
| 131: | } |
| 132: | |
| 133: | |
| 134: | |
| 135: | |
| 136: | |
| 137: | |
| 138: | public function __toString() : string { |
| 139: | return $this->text; |
| 140: | } |
| 141: | |
| 142: | |
| 143: | |
| 144: | |
| 145: | |
| 146: | |
| 147: | |
| 148: | |
| 149: | |
| 150: | |
| 151: | |
| 152: | public function getReformattedText() { |
| 153: | $text = trim($this->text); |
| 154: | $newlinePos = strpos($text, "\n"); |
| 155: | if (false === $newlinePos) { |
| 156: | |
| 157: | return $text; |
| 158: | } elseif (preg_match('((*BSR_ANYCRLF)(*ANYCRLF)^.*(?:\R\s+\*.*)+$)', $text)) { |
| 159: | |
| 160: | |
| 161: | |
| 162: | |
| 163: | |
| 164: | |
| 165: | |
| 166: | |
| 167: | return preg_replace('(^\s+\*)m', ' *', $this->text); |
| 168: | } elseif (preg_match('(^/\*\*?\s*[\r\n])', $text) && preg_match('(\n(\s*)\*/$)', $text, $matches)) { |
| 169: | |
| 170: | |
| 171: | |
| 172: | |
| 173: | |
| 174: | |
| 175: | |
| 176: | |
| 177: | |
| 178: | |
| 179: | return preg_replace('(^' . preg_quote($matches[1]) . ')m', '', $text); |
| 180: | } elseif (preg_match('(^/\*\*?\s*(?!\s))', $text, $matches)) { |
| 181: | |
| 182: | |
| 183: | |
| 184: | |
| 185: | |
| 186: | |
| 187: | |
| 188: | |
| 189: | |
| 190: | $prefixLen = $this->getShortestWhitespacePrefixLen(substr($text, $newlinePos + 1)); |
| 191: | $removeLen = $prefixLen - strlen($matches[0]); |
| 192: | return preg_replace('(^\s{' . $removeLen . '})m', '', $text); |
| 193: | } |
| 194: | |
| 195: | |
| 196: | return $text; |
| 197: | } |
| 198: | |
| 199: | |
| 200: | |
| 201: | |
| 202: | |
| 203: | |
| 204: | |
| 205: | |
| 206: | |
| 207: | private function getShortestWhitespacePrefixLen(string $str) : int { |
| 208: | $lines = explode("\n", $str); |
| 209: | $shortestPrefixLen = \INF; |
| 210: | foreach ($lines as $line) { |
| 211: | preg_match('(^\s*)', $line, $matches); |
| 212: | $prefixLen = strlen($matches[0]); |
| 213: | if ($prefixLen < $shortestPrefixLen) { |
| 214: | $shortestPrefixLen = $prefixLen; |
| 215: | } |
| 216: | } |
| 217: | return $shortestPrefixLen; |
| 218: | } |
| 219: | |
| 220: | |
| 221: | |
| 222: | |
| 223: | |
| 224: | public function jsonSerialize() : array { |
| 225: | |
| 226: | $type = $this instanceof Comment\Doc ? 'Comment_Doc' : 'Comment'; |
| 227: | return [ |
| 228: | 'nodeType' => $type, |
| 229: | 'text' => $this->text, |
| 230: | |
| 231: | 'line' => $this->startLine, |
| 232: | 'filePos' => $this->startFilePos, |
| 233: | 'tokenPos' => $this->startTokenPos, |
| 234: | 'endLine' => $this->endLine, |
| 235: | 'endFilePos' => $this->endFilePos, |
| 236: | 'endTokenPos' => $this->endTokenPos, |
| 237: | ]; |
| 238: | } |
| 239: | } |
| 240: | |