PluginProbe
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot / 4.5.5
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot v4.5.5
4.9.1 4.9.0 4.8.2 4.8.1 4.8.0 4.7.0 4.6.2 4.6.1 4.6.0 4.5.6 4.5.5 4.5.4 4.5.3 4.5.2 4.5.1 4.5.0 4.4.1 4.4.0 3.3.4 3.4.0 3.4.1 3.4.2 3.5.0 3.5.1 3.5.2 All 199 releases
betterdocs / includes / Dependencies / PhpParser / Comment.php

Comment.php in BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot 4.5.5, at includes/Dependencies/PhpParser/Comment.php

140 lines 4.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace WPDeveloper\BetterDocs\Dependencies\PhpParser;
4
5 class Comment implements \JsonSerializable
6 {
7 protected $text;
8 protected $line;
9 protected $filePos;
10
11 /**
12 * Constructs a comment node.
13 *
14 * @param string $text Comment text (including comment delimiters like /*)
15 * @param int $startLine Line number the comment started on
16 * @param int $startFilePos File offset the comment started on
17 */
18 public function __construct($text, $startLine = -1, $startFilePos = -1) {
19 $this->text = $text;
20 $this->line = $startLine;
21 $this->filePos = $startFilePos;
22 }
23
24 /**
25 * Gets the comment text.
26 *
27 * @return string The comment text (including comment delimiters like /*)
28 */
29 public function getText() {
30 return $this->text;
31 }
32
33 /**
34 * Gets the line number the comment started on.
35 *
36 * @return int Line number
37 */
38 public function getLine() {
39 return $this->line;
40 }
41
42 /**
43 * Gets the file offset the comment started on.
44 *
45 * @return int File offset
46 */
47 public function getFilePos() {
48 return $this->filePos;
49 }
50
51 /**
52 * Gets the comment text.
53 *
54 * @return string The comment text (including comment delimiters like /*)
55 */
56 public function __toString() {
57 return $this->text;
58 }
59
60 /**
61 * Gets the reformatted comment text.
62 *
63 * "Reformatted" here means that we try to clean up the whitespace at the
64 * starts of the lines. This is necessary because we receive the comments
65 * without trailing whitespace on the first line, but with trailing whitespace
66 * on all subsequent lines.
67 *
68 * @return mixed|string
69 */
70 public function getReformattedText() {
71 $text = trim($this->text);
72 $newlinePos = strpos($text, "\n");
73 if (false === $newlinePos) {
74 // Single line comments don't need further processing
75 return $text;
76 } elseif (preg_match('((*BSR_ANYCRLF)(*ANYCRLF)^.*(?:\R\s+\*.*)+$)', $text)) {
77 // Multi line comment of the type
78 //
79 // /*
80 // * Some text.
81 // * Some more text.
82 // */
83 //
84 // is handled by replacing the whitespace sequences before the * by a single space
85 return preg_replace('(^\s+\*)m', ' *', $this->text);
86 } elseif (preg_match('(^/\*\*?\s*[\r\n])', $text) && preg_match('(\n(\s*)\*/$)', $text, $matches)) {
87 // Multi line comment of the type
88 //
89 // /*
90 // Some text.
91 // Some more text.
92 // */
93 //
94 // is handled by removing the whitespace sequence on the line before the closing
95 // */ on all lines. So if the last line is " */", then " " is removed at the
96 // start of all lines.
97 return preg_replace('(^' . preg_quote($matches[1]) . ')m', '', $text);
98 } elseif (preg_match('(^/\*\*?\s*(?!\s))', $text, $matches)) {
99 // Multi line comment of the type
100 //
101 // /* Some text.
102 // Some more text.
103 // Indented text.
104 // Even more text. */
105 //
106 // is handled by removing the difference between the shortest whitespace prefix on all
107 // lines and the length of the "/* " opening sequence.
108 $prefixLen = $this->getShortestWhitespacePrefixLen(substr($text, $newlinePos + 1));
109 $removeLen = $prefixLen - strlen($matches[0]);
110 return preg_replace('(^\s{' . $removeLen . '})m', '', $text);
111 }
112
113 // No idea how to format this comment, so simply return as is
114 return $text;
115 }
116
117 private function getShortestWhitespacePrefixLen($str) {
118 $lines = explode("\n", $str);
119 $shortestPrefixLen = INF;
120 foreach ($lines as $line) {
121 preg_match('(^\s*)', $line, $matches);
122 $prefixLen = strlen($matches[0]);
123 if ($prefixLen < $shortestPrefixLen) {
124 $shortestPrefixLen = $prefixLen;
125 }
126 }
127 return $shortestPrefixLen;
128 }
129
130 public function jsonSerialize() {
131 // Technically not a node, but we make it look like one anyway
132 $type = $this instanceof Comment\Doc ? 'Comment_Doc' : 'Comment';
133 return [
134 'nodeType' => $type,
135 'text' => $this->text,
136 'line' => $this->line,
137 'filePos' => $this->filePos,
138 ];
139 }
140 }