comparison vendor/drupal/coder/coder_sniffer/Drupal/Sniffs/Commenting/HookCommentSniff.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 af1871eacc83
comparison
equal deleted inserted replaced
16:c2387f117808 17:129ea1e6d783
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 * Ensures hook comments on function are correct. 17 * Ensures hook comments on function are correct.
12 * 18 *
13 * @category PHP 19 * @category PHP
14 * @package PHP_CodeSniffer 20 * @package PHP_CodeSniffer
15 * @link http://pear.php.net/package/PHP_CodeSniffer 21 * @link http://pear.php.net/package/PHP_CodeSniffer
16 */ 22 */
17 class Drupal_Sniffs_Commenting_HookCommentSniff implements PHP_CodeSniffer_Sniff 23 class HookCommentSniff implements Sniff
18 { 24 {
19 25
20 26
21 /** 27 /**
22 * Returns an array of tokens this test wants to listen for. 28 * Returns an array of tokens this test wants to listen for.
31 37
32 38
33 /** 39 /**
34 * Processes this test, when one of its tokens is encountered. 40 * Processes this test, when one of its tokens is encountered.
35 * 41 *
36 * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. 42 * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
37 * @param int $stackPtr The position of the current token 43 * @param int $stackPtr The position of the current token
38 * in the stack passed in $tokens. 44 * in the stack passed in $tokens.
39 * 45 *
40 * @return void 46 * @return void
41 */ 47 */
42 public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) 48 public function process(File $phpcsFile, $stackPtr)
43 { 49 {
44 $tokens = $phpcsFile->getTokens(); 50 $tokens = $phpcsFile->getTokens();
45 $find = PHP_CodeSniffer_Tokens::$methodPrefixes; 51 $find = Tokens::$methodPrefixes;
46 $find[] = T_WHITESPACE; 52 $find[] = T_WHITESPACE;
47 53
48 $commentEnd = $phpcsFile->findPrevious($find, ($stackPtr - 1), null, true); 54 $commentEnd = $phpcsFile->findPrevious($find, ($stackPtr - 1), null, true);
49 if ($tokens[$commentEnd]['code'] !== T_DOC_COMMENT_CLOSE_TAG 55 if ($tokens[$commentEnd]['code'] !== T_DOC_COMMENT_CLOSE_TAG
50 && $tokens[$commentEnd]['code'] !== T_COMMENT 56 && $tokens[$commentEnd]['code'] !== T_COMMENT
87 // Check if a hook implementation doc block is formatted correctly. 93 // Check if a hook implementation doc block is formatted correctly.
88 if (preg_match('/^[\s]*Implement[^\n]+?hook_[^\n]+/i', $shortContent, $matches) === 1) { 94 if (preg_match('/^[\s]*Implement[^\n]+?hook_[^\n]+/i', $shortContent, $matches) === 1) {
89 if (strstr($matches[0], 'Implements ') === false || strstr($matches[0], 'Implements of') !== false 95 if (strstr($matches[0], 'Implements ') === false || strstr($matches[0], 'Implements of') !== false
90 || preg_match('/ (drush_)?hook_[a-zA-Z0-9_]+\(\)( for .+)?\.$/', $matches[0]) !== 1 96 || preg_match('/ (drush_)?hook_[a-zA-Z0-9_]+\(\)( for .+)?\.$/', $matches[0]) !== 1
91 ) { 97 ) {
92 $phpcsFile->addWarning('Format should be "* Implements hook_foo().", "* Implements hook_foo_BAR_ID_bar() for xyz_bar().",, "* Implements hook_foo_BAR_ID_bar() for xyz-bar.html.twig.", "* Implements hook_foo_BAR_ID_bar() for xyz-bar.tpl.php.", or "* Implements hook_foo_BAR_ID_bar() for block templates."', $short); 98 $phpcsFile->addWarning('Format should be "* Implements hook_foo().", "* Implements hook_foo_BAR_ID_bar() for xyz_bar().",, "* Implements hook_foo_BAR_ID_bar() for xyz-bar.html.twig.", "* Implements hook_foo_BAR_ID_bar() for xyz-bar.tpl.php.", or "* Implements hook_foo_BAR_ID_bar() for block templates."', $short, 'HookCommentFormat');
93 } else { 99 } else {
94 // Check that a hook implementation does not duplicate @param and 100 // Check that a hook implementation does not duplicate @param and
95 // @return documentation. 101 // @return documentation.
96 foreach ($tokens[$commentStart]['comment_tags'] as $pos => $tag) { 102 foreach ($tokens[$commentStart]['comment_tags'] as $pos => $tag) {
97 if ($tokens[$tag]['content'] === '@param') { 103 if ($tokens[$tag]['content'] === '@param') {