PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 6.3.0
Defender Security – Malware Scanner, Login Security & Firewall v6.3.0
6.3.0 6.2.3 6.2.4 6.2.0 6.2.1 6.2.2 6.1.0 5.3.1 5.4.0 5.4.1 5.5.0 5.5.1 5.6.0 5.6.1 5.6.2 5.7.0 5.7.1 5.7.2 5.8.0 5.8.1 5.9.0 6.0.0 6.0.1 3.0.1 3.1.0 All 141 releases
← All changes | src/extra/php-codesniffer-php-token/Tokenizers/Comment.php +211 -215 3.0.1 → 6.3.0 View file →
@@ -10,268 +10,264 @@
10 10 namespace PHP_CodeSniffer\Tokenizers;
11 11
12 12 use PHP_CodeSniffer\Util;
13 13
14 -class Comment
15 -{
14 +class Comment {
16 15
17 16
18 - /**
19 - * Creates an array of tokens when given some PHP code.
20 - *
21 - * Starts by using token_get_all() but does a lot of extra processing
22 - * to insert information about the context of the token.
23 - *
24 - * @param string $string The string to tokenize.
25 - * @param string $eolChar The EOL character to use for splitting strings.
26 - * @param int $stackPtr The position of the first token in the file.
27 - *
28 - * @return array
29 - */
30 - public function tokenizeString($string, $eolChar, $stackPtr)
31 - {
32 - if (PHP_CODESNIFFER_VERBOSITY > 1) {
33 - echo "\t\t*** START COMMENT TOKENIZING ***".PHP_EOL;
34 - }
17 + /**
18 + * Creates an array of tokens when given some PHP code.
19 + *
20 + * Starts by using token_get_all() but does a lot of extra processing
21 + * to insert information about the context of the token.
22 + *
23 + * @param string $string The string to tokenize.
24 + * @param string $eolChar The EOL character to use for splitting strings.
25 + * @param int $stackPtr The position of the first token in the file.
26 + *
27 + * @return array
28 + */
29 + public function tokenizeString( $string, $eolChar, $stackPtr ) {
30 + if ( PHP_CODESNIFFER_VERBOSITY > 1 ) {
31 + echo "\t\t*** START COMMENT TOKENIZING ***" . PHP_EOL;
32 + }
35 33
36 - $tokens = [];
37 - $numChars = strlen($string);
34 + $tokens = [];
35 + $numChars = strlen( $string );
38 36
39 - /*
40 - Doc block comments start with /*, but typically contain an
41 - extra star when they are used for function and class comments.
42 - */
37 + /*
38 + Doc block comments start with /*, but typically contain an
39 + extra star when they are used for function and class comments.
40 + */
43 41
44 - $char = ($numChars - strlen(ltrim($string, '/*')));
45 - $openTag = substr($string, 0, $char);
46 - $string = ltrim($string, '/*');
42 + $char = ( $numChars - strlen( ltrim( $string, '/*' ) ) );
43 + $openTag = substr( $string, 0, $char );
44 + $string = ltrim( $string, '/*' );
47 45
48 - $tokens[$stackPtr] = [
49 - 'content' => $openTag,
50 - 'code' => T_DOC_COMMENT_OPEN_TAG,
51 - 'type' => 'T_DOC_COMMENT_OPEN_TAG',
52 - 'comment_tags' => [],
53 - ];
46 + $tokens[ $stackPtr ] = [
47 + 'content' => $openTag,
48 + 'code' => T_DOC_COMMENT_OPEN_TAG,
49 + 'type' => 'T_DOC_COMMENT_OPEN_TAG',
50 + 'comment_tags' => [],
51 + ];
54 52
55 - $openPtr = $stackPtr;
56 - $stackPtr++;
53 + $openPtr = $stackPtr;
54 + $stackPtr ++;
57 55
58 - if (PHP_CODESNIFFER_VERBOSITY > 1) {
59 - $content = Util\Common::prepareForOutput($openTag);
60 - echo "\t\tCreate comment token: T_DOC_COMMENT_OPEN_TAG => $content".PHP_EOL;
61 - }
56 + if ( PHP_CODESNIFFER_VERBOSITY > 1 ) {
57 + $content = Util\Common::prepareForOutput( $openTag );
58 + echo "\t\tCreate comment token: T_DOC_COMMENT_OPEN_TAG => $content" . PHP_EOL;
59 + }
62 60
63 - /*
64 - Strip off the close tag so it doesn't interfere with any
65 - of our comment line processing. The token will be added to the
66 - stack just before we return it.
67 - */
61 + /*
62 + Strip off the close tag so it doesn't interfere with any
63 + of our comment line processing. The token will be added to the
64 + stack just before we return it.
65 + */
68 66
69 - $closeTag = [
70 - 'content' => substr($string, strlen(rtrim($string, '/*'))),
71 - 'code' => T_DOC_COMMENT_CLOSE_TAG,
72 - 'type' => 'T_DOC_COMMENT_CLOSE_TAG',
73 - 'comment_opener' => $openPtr,
74 - ];
67 + $closeTag = [
68 + 'content' => substr( $string, strlen( rtrim( $string, '/*' ) ) ),
69 + 'code' => T_DOC_COMMENT_CLOSE_TAG,
70 + 'type' => 'T_DOC_COMMENT_CLOSE_TAG',
71 + 'comment_opener' => $openPtr,
72 + ];
75 73
76 - if ($closeTag['content'] === false) {
77 - $closeTag['content'] = '';
78 - }
74 + if ( $closeTag['content'] === false ) {
75 + $closeTag['content'] = '';
76 + }
79 77
80 - $string = rtrim($string, '/*');
78 + $string = rtrim( $string, '/*' );
81 79
82 - /*
83 - Process each line of the comment.
84 - */
80 + /*
81 + Process each line of the comment.
82 + */
85 83
86 - $lines = explode($eolChar, $string);
87 - $numLines = count($lines);
88 - foreach ($lines as $lineNum => $string) {
89 - if ($lineNum !== ($numLines - 1)) {
90 - $string .= $eolChar;
91 - }
84 + $lines = explode( $eolChar, $string );
85 + $numLines = count( $lines );
86 + foreach ( $lines as $lineNum => $string ) {
87 + if ( $lineNum !== ( $numLines - 1 ) ) {
88 + $string .= $eolChar;
89 + }
92 90
93 - $char = 0;
94 - $numChars = strlen($string);
91 + $char = 0;
92 + $numChars = strlen( $string );
95 93
96 - // We've started a new line, so process the indent.
97 - $space = $this->collectWhitespace($string, $char, $numChars);
98 - if ($space !== null) {
99 - $tokens[$stackPtr] = $space;
100 - $stackPtr++;
101 - if (PHP_CODESNIFFER_VERBOSITY > 1) {
102 - $content = Util\Common::prepareForOutput($space['content']);
103 - echo "\t\tCreate comment token: T_DOC_COMMENT_WHITESPACE => $content".PHP_EOL;
104 - }
94 + // We've started a new line, so process the indent.
95 + $space = $this->collectWhitespace( $string, $char, $numChars );
96 + if ( $space !== null ) {
97 + $tokens[ $stackPtr ] = $space;
98 + $stackPtr ++;
99 + if ( PHP_CODESNIFFER_VERBOSITY > 1 ) {
100 + $content = Util\Common::prepareForOutput( $space['content'] );
101 + echo "\t\tCreate comment token: T_DOC_COMMENT_WHITESPACE => $content" . PHP_EOL;
102 + }
105 103
106 - $char += strlen($space['content']);
107 - if ($char === $numChars) {
108 - break;
109 - }
110 - }
104 + $char += strlen( $space['content'] );
105 + if ( $char === $numChars ) {
106 + break;
107 + }
108 + }
111 109
112 - if ($string === '') {
113 - continue;
114 - }
110 + if ( $string === '' ) {
111 + continue;
112 + }
115 113
116 - if ($lineNum > 0 && $string[$char] === '*') {
117 - // This is a function or class doc block line.
118 - $char++;
119 - $tokens[$stackPtr] = [
120 - 'content' => '*',
121 - 'code' => T_DOC_COMMENT_STAR,
122 - 'type' => 'T_DOC_COMMENT_STAR',
123 - ];
114 + if ( $lineNum > 0 && $string[ $char ] === '*' ) {
115 + // This is a function or class doc block line.
116 + $char ++;
117 + $tokens[ $stackPtr ] = [
118 + 'content' => '*',
119 + 'code' => T_DOC_COMMENT_STAR,
120 + 'type' => 'T_DOC_COMMENT_STAR',
121 + ];
124 122
125 - $stackPtr++;
123 + $stackPtr ++;
126 124
127 - if (PHP_CODESNIFFER_VERBOSITY > 1) {
128 - echo "\t\tCreate comment token: T_DOC_COMMENT_STAR => *".PHP_EOL;
129 - }
130 - }
125 + if ( PHP_CODESNIFFER_VERBOSITY > 1 ) {
126 + echo "\t\tCreate comment token: T_DOC_COMMENT_STAR => *" . PHP_EOL;
127 + }
128 + }
131 129
132 - // Now we are ready to process the actual content of the line.
133 - $lineTokens = $this->processLine($string, $eolChar, $char, $numChars);
134 - foreach ($lineTokens as $lineToken) {
135 - $tokens[$stackPtr] = $lineToken;
136 - if (PHP_CODESNIFFER_VERBOSITY > 1) {
137 - $content = Util\Common::prepareForOutput($lineToken['content']);
138 - $type = $lineToken['type'];
139 - echo "\t\tCreate comment token: $type => $content".PHP_EOL;
140 - }
130 + // Now we are ready to process the actual content of the line.
131 + $lineTokens = $this->processLine( $string, $eolChar, $char, $numChars );
132 + foreach ( $lineTokens as $lineToken ) {
133 + $tokens[ $stackPtr ] = $lineToken;
134 + if ( PHP_CODESNIFFER_VERBOSITY > 1 ) {
135 + $content = Util\Common::prepareForOutput( $lineToken['content'] );
136 + $type = $lineToken['type'];
137 + echo "\t\tCreate comment token: $type => $content" . PHP_EOL;
138 + }
141 139
142 - if ($lineToken['code'] === T_DOC_COMMENT_TAG) {
143 - $tokens[$openPtr]['comment_tags'][] = $stackPtr;
144 - }
140 + if ( $lineToken['code'] === T_DOC_COMMENT_TAG ) {
141 + $tokens[ $openPtr ]['comment_tags'][] = $stackPtr;
142 + }
145 143
146 - $stackPtr++;
147 - }
148 - }//end foreach
144 + $stackPtr ++;
145 + }
146 + }//end foreach
149 147
150 - $tokens[$stackPtr] = $closeTag;
151 - $tokens[$openPtr]['comment_closer'] = $stackPtr;
152 - if (PHP_CODESNIFFER_VERBOSITY > 1) {
153 - $content = Util\Common::prepareForOutput($closeTag['content']);
154 - echo "\t\tCreate comment token: T_DOC_COMMENT_CLOSE_TAG => $content".PHP_EOL;
155 - }
148 + $tokens[ $stackPtr ] = $closeTag;
149 + $tokens[ $openPtr ]['comment_closer'] = $stackPtr;
150 + if ( PHP_CODESNIFFER_VERBOSITY > 1 ) {
151 + $content = Util\Common::prepareForOutput( $closeTag['content'] );
152 + echo "\t\tCreate comment token: T_DOC_COMMENT_CLOSE_TAG => $content" . PHP_EOL;
153 + }
156 154
157 - if (PHP_CODESNIFFER_VERBOSITY > 1) {
158 - echo "\t\t*** END COMMENT TOKENIZING ***".PHP_EOL;
159 - }
155 + if ( PHP_CODESNIFFER_VERBOSITY > 1 ) {
156 + echo "\t\t*** END COMMENT TOKENIZING ***" . PHP_EOL;
157 + }
160 158
161 - return $tokens;
159 + return $tokens;
162 160
163 - }//end tokenizeString()
161 + }//end tokenizeString()
164 162
165 163
166 - /**
167 - * Process a single line of a comment.
168 - *
169 - * @param string $string The comment string being tokenized.
170 - * @param string $eolChar The EOL character to use for splitting strings.
171 - * @param int $start The position in the string to start processing.
172 - * @param int $end The position in the string to end processing.
173 - *
174 - * @return array
175 - */
176 - private function processLine($string, $eolChar, $start, $end)
177 - {
178 - $tokens = [];
164 + /**
165 + * Process a single line of a comment.
166 + *
167 + * @param string $string The comment string being tokenized.
168 + * @param string $eolChar The EOL character to use for splitting strings.
169 + * @param int $start The position in the string to start processing.
170 + * @param int $end The position in the string to end processing.
171 + *
172 + * @return array
173 + */
174 + private function processLine( $string, $eolChar, $start, $end ) {
175 + $tokens = [];
179 176
180 - // Collect content padding.
181 - $space = $this->collectWhitespace($string, $start, $end);
182 - if ($space !== null) {
183 - $tokens[] = $space;
184 - $start += strlen($space['content']);
185 - }
177 + // Collect content padding.
178 + $space = $this->collectWhitespace( $string, $start, $end );
179 + if ( $space !== null ) {
180 + $tokens[] = $space;
181 + $start += strlen( $space['content'] );
182 + }
186 183
187 - if (isset($string[$start]) === false) {
188 - return $tokens;
189 - }
184 + if ( isset( $string[ $start ] ) === false ) {
185 + return $tokens;
186 + }
190 187
191 - if ($string[$start] === '@') {
192 - // The content up until the first whitespace is the tag name.
193 - $matches = [];
194 - preg_match('/@[^\s]+/', $string, $matches, 0, $start);
195 - if (isset($matches[0]) === true
196 - && substr(strtolower($matches[0]), 0, 7) !== '@phpcs:'
197 - ) {
198 - $tagName = $matches[0];
199 - $start += strlen($tagName);
200 - $tokens[] = [
201 - 'content' => $tagName,
202 - 'code' => T_DOC_COMMENT_TAG,
203 - 'type' => 'T_DOC_COMMENT_TAG',
204 - ];
188 + if ( $string[ $start ] === '@' ) {
189 + // The content up until the first whitespace is the tag name.
190 + $matches = [];
191 + preg_match( '/@[^\s]+/', $string, $matches, 0, $start );
192 + if ( isset( $matches[0] ) === true
193 + && substr( strtolower( $matches[0] ), 0, 7 ) !== '@phpcs:'
194 + ) {
195 + $tagName = $matches[0];
196 + $start += strlen( $tagName );
197 + $tokens[] = [
198 + 'content' => $tagName,
199 + 'code' => T_DOC_COMMENT_TAG,
200 + 'type' => 'T_DOC_COMMENT_TAG',
201 + ];
205 202
206 - // Then there will be some whitespace.
207 - $space = $this->collectWhitespace($string, $start, $end);
208 - if ($space !== null) {
209 - $tokens[] = $space;
210 - $start += strlen($space['content']);
211 - }
212 - }
213 - }//end if
203 + // Then there will be some whitespace.
204 + $space = $this->collectWhitespace( $string, $start, $end );
205 + if ( $space !== null ) {
206 + $tokens[] = $space;
207 + $start += strlen( $space['content'] );
208 + }
209 + }
210 + }//end if
214 211
215 - // Process the rest of the line.
216 - $eol = strpos($string, $eolChar, $start);
217 - if ($eol === false) {
218 - $eol = $end;
219 - }
212 + // Process the rest of the line.
213 + $eol = strpos( $string, $eolChar, $start );
214 + if ( $eol === false ) {
215 + $eol = $end;
216 + }
220 217
221 - if ($eol > $start) {
222 - $tokens[] = [
223 - 'content' => substr($string, $start, ($eol - $start)),
224 - 'code' => T_DOC_COMMENT_STRING,
225 - 'type' => 'T_DOC_COMMENT_STRING',
226 - ];
227 - }
218 + if ( $eol > $start ) {
219 + $tokens[] = [
220 + 'content' => substr( $string, $start, ( $eol - $start ) ),
221 + 'code' => T_DOC_COMMENT_STRING,
222 + 'type' => 'T_DOC_COMMENT_STRING',
223 + ];
224 + }
228 225
229 - if ($eol !== $end) {
230 - $tokens[] = [
231 - 'content' => substr($string, $eol, strlen($eolChar)),
232 - 'code' => T_DOC_COMMENT_WHITESPACE,
233 - 'type' => 'T_DOC_COMMENT_WHITESPACE',
234 - ];
235 - }
226 + if ( $eol !== $end ) {
227 + $tokens[] = [
228 + 'content' => substr( $string, $eol, strlen( $eolChar ) ),
229 + 'code' => T_DOC_COMMENT_WHITESPACE,
230 + 'type' => 'T_DOC_COMMENT_WHITESPACE',
231 + ];
232 + }
236 233
237 - return $tokens;
234 + return $tokens;
238 235
239 - }//end processLine()
236 + }//end processLine()
240 237
241 238
242 - /**
243 - * Collect consecutive whitespace into a single token.
244 - *
245 - * @param string $string The comment string being tokenized.
246 - * @param int $start The position in the string to start processing.
247 - * @param int $end The position in the string to end processing.
248 - *
249 - * @return array|null
250 - */
251 - private function collectWhitespace($string, $start, $end)
252 - {
253 - $space = '';
254 - for ($start; $start < $end; $start++) {
255 - if ($string[$start] !== ' ' && $string[$start] !== "\t") {
256 - break;
257 - }
239 + /**
240 + * Collect consecutive whitespace into a single token.
241 + *
242 + * @param string $string The comment string being tokenized.
243 + * @param int $start The position in the string to start processing.
244 + * @param int $end The position in the string to end processing.
245 + *
246 + * @return array|null
247 + */
248 + private function collectWhitespace( $string, $start, $end ) {
249 + $space = '';
250 + for ( $start; $start < $end; $start ++ ) {
251 + if ( $string[ $start ] !== ' ' && $string[ $start ] !== "\t" ) {
252 + break;
253 + }
258 254
259 - $space .= $string[$start];
260 - }
255 + $space .= $string[ $start ];
256 + }
261 257
262 - if ($space === '') {
263 - return null;
264 - }
258 + if ( $space === '' ) {
259 + return null;
260 + }
265 261
266 - $token = [
267 - 'content' => $space,
268 - 'code' => T_DOC_COMMENT_WHITESPACE,
269 - 'type' => 'T_DOC_COMMENT_WHITESPACE',
270 - ];
262 + $token = [
263 + 'content' => $space,
264 + 'code' => T_DOC_COMMENT_WHITESPACE,
265 + 'type' => 'T_DOC_COMMENT_WHITESPACE',
266 + ];
271 267
272 - return $token;
268 + return $token;
273 269
274 - }//end collectWhitespace()
270 + }//end collectWhitespace()
275 271
276 272
277 273 }//end class