comparison vendor/drupal/coder/coder_sniffer/Drupal/Sniffs/Commenting/InlineCommentSniff.php @ 17:129ea1e6d783

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:21:36 +0000
parents 4c8ae668cc8c
children
comparison
equal deleted inserted replaced
16:c2387f117808 17:129ea1e6d783
1 <?php 1 <?php
2 /** 2 /**
3 * PHP_CodeSniffer_Sniffs_Drupal_Commenting_InlineCommentSniff. 3 * \Drupal\Sniffs\Commenting\InlineCommentSniff.
4 * 4 *
5 * @category PHP 5 * @category PHP
6 * @package PHP_CodeSniffer 6 * @package PHP_CodeSniffer
7 * @link http://pear.php.net/package/PHP_CodeSniffer 7 * @link http://pear.php.net/package/PHP_CodeSniffer
8 */ 8 */
9 9
10 namespace Drupal\Sniffs\Commenting;
11
12 use PHP_CodeSniffer\Files\File;
13 use PHP_CodeSniffer\Sniffs\Sniff;
14 use PHP_CodeSniffer\Util\Tokens;
15
10 /** 16 /**
11 * PHP_CodeSniffer_Sniffs_Drupal_Commenting_InlineCommentSniff. 17 * \Drupal\Sniffs\Commenting\InlineCommentSniff.
12 * 18 *
13 * Checks that no perl-style comments are used. Checks that inline comments ("//") 19 * Checks that no perl-style comments are used. Checks that inline comments ("//")
14 * have a space after //, start capitalized and end with proper punctuation. 20 * have a space after //, start capitalized and end with proper punctuation.
15 * Largely copied from Squiz_Sniffs_Commenting_InlineCommentSniff. 21 * Largely copied from
22 * \PHP_CodeSniffer\Standards\Squiz\Sniffs\Commenting\InlineCommentSniff.
16 * 23 *
17 * @category PHP 24 * @category PHP
18 * @package PHP_CodeSniffer 25 * @package PHP_CodeSniffer
19 * @link http://pear.php.net/package/PHP_CodeSniffer 26 * @link http://pear.php.net/package/PHP_CodeSniffer
20 */ 27 */
21 class Drupal_Sniffs_Commenting_InlineCommentSniff implements PHP_CodeSniffer_Sniff 28 class InlineCommentSniff implements Sniff
22 { 29 {
23 30
24 /** 31 /**
25 * A list of tokenizers this sniff supports. 32 * A list of tokenizers this sniff supports.
26 * 33 *
48 55
49 56
50 /** 57 /**
51 * Processes this test, when one of its tokens is encountered. 58 * Processes this test, when one of its tokens is encountered.
52 * 59 *
53 * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. 60 * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
54 * @param int $stackPtr The position of the current token in the 61 * @param int $stackPtr The position of the current token in the
55 * stack passed in $tokens. 62 * stack passed in $tokens.
56 * 63 *
57 * @return void 64 * @return void
58 */ 65 */
59 public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) 66 public function process(File $phpcsFile, $stackPtr)
60 { 67 {
61 $tokens = $phpcsFile->getTokens(); 68 $tokens = $phpcsFile->getTokens();
62 69
63 // If this is a function/class/interface doc block comment, skip it. 70 // If this is a function/class/interface doc block comment, skip it.
64 // We are only interested in inline doc block comments, which are 71 // We are only interested in inline doc block comments, which are
65 // not allowed. 72 // not allowed.
66 if ($tokens[$stackPtr]['code'] === T_DOC_COMMENT_OPEN_TAG) { 73 if ($tokens[$stackPtr]['code'] === T_DOC_COMMENT_OPEN_TAG) {
67 $nextToken = $phpcsFile->findNext( 74 $nextToken = $phpcsFile->findNext(
68 PHP_CodeSniffer_Tokens::$emptyTokens, 75 Tokens::$emptyTokens,
69 ($stackPtr + 1), 76 ($stackPtr + 1),
70 null, 77 null,
71 true 78 true
72 ); 79 );
73 80
97 } 104 }
98 105
99 if ($phpcsFile->tokenizerType === 'JS') { 106 if ($phpcsFile->tokenizerType === 'JS') {
100 // We allow block comments if a function or object 107 // We allow block comments if a function or object
101 // is being assigned to a variable. 108 // is being assigned to a variable.
102 $ignore = PHP_CodeSniffer_Tokens::$emptyTokens; 109 $ignore = Tokens::$emptyTokens;
103 $ignore[] = T_EQUAL; 110 $ignore[] = T_EQUAL;
104 $ignore[] = T_STRING; 111 $ignore[] = T_STRING;
105 $ignore[] = T_OBJECT_OPERATOR; 112 $ignore[] = T_OBJECT_OPERATOR;
106 $nextToken = $phpcsFile->findNext($ignore, ($nextToken + 1), null, true); 113 $nextToken = $phpcsFile->findNext($ignore, ($nextToken + 1), null, true);
107 if ($tokens[$nextToken]['code'] === T_FUNCTION 114 if ($tokens[$nextToken]['code'] === T_FUNCTION
112 return; 119 return;
113 } 120 }
114 } 121 }
115 122
116 $prevToken = $phpcsFile->findPrevious( 123 $prevToken = $phpcsFile->findPrevious(
117 PHP_CodeSniffer_Tokens::$emptyTokens, 124 Tokens::$emptyTokens,
118 ($stackPtr - 1), 125 ($stackPtr - 1),
119 null, 126 null,
120 true 127 true
121 ); 128 );
122 129
320 327
321 328
322 /** 329 /**
323 * Determines if a comment line is part of an @code/@endcode example. 330 * Determines if a comment line is part of an @code/@endcode example.
324 * 331 *
325 * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. 332 * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
326 * @param int $stackPtr The position of the current token 333 * @param int $stackPtr The position of the current token
327 * in the stack passed in $tokens. 334 * in the stack passed in $tokens.
328 * 335 *
329 * @return boolean Returns true if the comment line is within a @code block, 336 * @return boolean Returns true if the comment line is within a @code block,
330 * false otherwise. 337 * false otherwise.
331 */ 338 */
332 protected function isInCodeExample(PHP_CodeSniffer_File $phpcsFile, $stackPtr) 339 protected function isInCodeExample(File $phpcsFile, $stackPtr)
333 { 340 {
334 $tokens = $phpcsFile->getTokens(); 341 $tokens = $phpcsFile->getTokens();
335 $prevComment = $stackPtr; 342 $prevComment = $stackPtr;
336 $lastComment = $stackPtr; 343 $lastComment = $stackPtr;
337 while (($prevComment = $phpcsFile->findPrevious(array(T_COMMENT), ($lastComment - 1), null, false)) !== false) { 344 while (($prevComment = $phpcsFile->findPrevious(array(T_COMMENT), ($lastComment - 1), null, false)) !== false) {
356 363
357 364
358 /** 365 /**
359 * Checks the indentation level of the comment contents. 366 * Checks the indentation level of the comment contents.
360 * 367 *
361 * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. 368 * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
362 * @param int $stackPtr The position of the current token 369 * @param int $stackPtr The position of the current token
363 * in the stack passed in $tokens. 370 * in the stack passed in $tokens.
364 * 371 *
365 * @return void 372 * @return void
366 */ 373 */
367 protected function processIndentation(PHP_CodeSniffer_File $phpcsFile, $stackPtr) 374 protected function processIndentation(File $phpcsFile, $stackPtr)
368 { 375 {
369 $tokens = $phpcsFile->getTokens(); 376 $tokens = $phpcsFile->getTokens();
370 $comment = rtrim($tokens[$stackPtr]['content']); 377 $comment = rtrim($tokens[$stackPtr]['content']);
371 $spaceCount = 0; 378 $spaceCount = 0;
372 $tabFound = false; 379 $tabFound = false;