1: | <?php declare(strict_types=1); |
2: | |
3: | namespace PhpParser; |
4: | |
5: | class Comment implements \JsonSerializable { |
6: | protected string $text; |
7: | protected int $startLine; |
8: | protected int $startFilePos; |
9: | protected int $startTokenPos; |
10: | protected int $endLine; |
11: | protected int $endFilePos; |
12: | protected int $endTokenPos; |
13: | |
14: | |
15: | |
16: | |
17: | |
18: | |
19: | |
20: | |
21: | |
22: | public function __construct( |
23: | string $text, |
24: | int $startLine = -1, int $startFilePos = -1, int $startTokenPos = -1, |
25: | int $endLine = -1, int $endFilePos = -1, int $endTokenPos = -1 |
26: | ) { |
27: | $this->text = $text; |
28: | $this->startLine = $startLine; |
29: | $this->startFilePos = $startFilePos; |
30: | $this->startTokenPos = $startTokenPos; |
31: | $this->endLine = $endLine; |
32: | $this->endFilePos = $endFilePos; |
33: | $this->endTokenPos = $endTokenPos; |
34: | } |
35: | |
36: | |
37: | |
38: | |
39: | |
40: | |
41: | public function getText(): string { |
42: | return $this->text; |
43: | } |
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: | |
79: | public function getEndLine(): int { |
80: | return $this->endLine; |
81: | } |
82: | |
83: | |
84: | |
85: | |
86: | |
87: | |
88: | public function getEndFilePos(): int { |
89: | return $this->endFilePos; |
90: | } |
91: | |
92: | |
93: | |
94: | |
95: | |
96: | |
97: | public function getEndTokenPos(): int { |
98: | return $this->endTokenPos; |
99: | } |
100: | |
101: | |
102: | |
103: | |
104: | |
105: | |
106: | public function __toString(): string { |
107: | return $this->text; |
108: | } |
109: | |
110: | |
111: | |
112: | |
113: | |
114: | |
115: | |
116: | |
117: | |
118: | |
119: | |
120: | public function getReformattedText(): string { |
121: | $text = str_replace("\r\n", "\n", $this->text); |
122: | $newlinePos = strpos($text, "\n"); |
123: | if (false === $newlinePos) { |
124: | |
125: | return $text; |
126: | } |
127: | if (preg_match('(^.*(?:\n\s+\*.*)+$)', $text)) { |
128: | |
129: | |
130: | |
131: | |
132: | |
133: | |
134: | |
135: | |
136: | return preg_replace('(^\s+\*)m', ' *', $text); |
137: | } |
138: | if (preg_match('(^/\*\*?\s*\n)', $text) && preg_match('(\n(\s*)\*/$)', $text, $matches)) { |
139: | |
140: | |
141: | |
142: | |
143: | |
144: | |
145: | |
146: | |
147: | |
148: | |
149: | return preg_replace('(^' . preg_quote($matches[1]) . ')m', '', $text); |
150: | } |
151: | if (preg_match('(^/\*\*?\s*(?!\s))', $text, $matches)) { |
152: | |
153: | |
154: | |
155: | |
156: | |
157: | |
158: | |
159: | |
160: | |
161: | $prefixLen = $this->getShortestWhitespacePrefixLen(substr($text, $newlinePos + 1)); |
162: | $removeLen = $prefixLen - strlen($matches[0]); |
163: | return preg_replace('(^\s{' . $removeLen . '})m', '', $text); |
164: | } |
165: | |
166: | |
167: | return $text; |
168: | } |
169: | |
170: | |
171: | |
172: | |
173: | |
174: | |
175: | |
176: | |
177: | |
178: | private function getShortestWhitespacePrefixLen(string $str): int { |
179: | $lines = explode("\n", $str); |
180: | $shortestPrefixLen = \PHP_INT_MAX; |
181: | foreach ($lines as $line) { |
182: | preg_match('(^\s*)', $line, $matches); |
183: | $prefixLen = strlen($matches[0]); |
184: | if ($prefixLen < $shortestPrefixLen) { |
185: | $shortestPrefixLen = $prefixLen; |
186: | } |
187: | } |
188: | return $shortestPrefixLen; |
189: | } |
190: | |
191: | |
192: | |
193: | |
194: | public function jsonSerialize(): array { |
195: | |
196: | $type = $this instanceof Comment\Doc ? 'Comment_Doc' : 'Comment'; |
197: | return [ |
198: | 'nodeType' => $type, |
199: | 'text' => $this->text, |
200: | |
201: | 'line' => $this->startLine, |
202: | 'filePos' => $this->startFilePos, |
203: | 'tokenPos' => $this->startTokenPos, |
204: | 'endLine' => $this->endLine, |
205: | 'endFilePos' => $this->endFilePos, |
206: | 'endTokenPos' => $this->endTokenPos, |
207: | ]; |
208: | } |
209: | } |
210: | |