annotate vendor/drupal/coder/coder_sniffer/Drupal/Sniffs/Commenting/HookCommentSniff.php @ 12:7a779792577d

Update Drupal core to v8.4.5 (via Composer)
author Chris Cannam
date Fri, 23 Feb 2018 15:52:07 +0000
parents 4c8ae668cc8c
children 129ea1e6d783
rev   line source
Chris@0 1 <?php
Chris@0 2 /**
Chris@0 3 * Ensures hook comments on function are correct.
Chris@0 4 *
Chris@0 5 * @category PHP
Chris@0 6 * @package PHP_CodeSniffer
Chris@0 7 * @link http://pear.php.net/package/PHP_CodeSniffer
Chris@0 8 */
Chris@0 9
Chris@0 10 /**
Chris@0 11 * Ensures hook comments on function are correct.
Chris@0 12 *
Chris@0 13 * @category PHP
Chris@0 14 * @package PHP_CodeSniffer
Chris@0 15 * @link http://pear.php.net/package/PHP_CodeSniffer
Chris@0 16 */
Chris@0 17 class Drupal_Sniffs_Commenting_HookCommentSniff implements PHP_CodeSniffer_Sniff
Chris@0 18 {
Chris@0 19
Chris@0 20
Chris@0 21 /**
Chris@0 22 * Returns an array of tokens this test wants to listen for.
Chris@0 23 *
Chris@0 24 * @return array
Chris@0 25 */
Chris@0 26 public function register()
Chris@0 27 {
Chris@0 28 return array(T_FUNCTION);
Chris@0 29
Chris@0 30 }//end register()
Chris@0 31
Chris@0 32
Chris@0 33 /**
Chris@0 34 * Processes this test, when one of its tokens is encountered.
Chris@0 35 *
Chris@0 36 * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
Chris@0 37 * @param int $stackPtr The position of the current token
Chris@0 38 * in the stack passed in $tokens.
Chris@0 39 *
Chris@0 40 * @return void
Chris@0 41 */
Chris@0 42 public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
Chris@0 43 {
Chris@0 44 $tokens = $phpcsFile->getTokens();
Chris@0 45 $find = PHP_CodeSniffer_Tokens::$methodPrefixes;
Chris@0 46 $find[] = T_WHITESPACE;
Chris@0 47
Chris@0 48 $commentEnd = $phpcsFile->findPrevious($find, ($stackPtr - 1), null, true);
Chris@0 49 if ($tokens[$commentEnd]['code'] !== T_DOC_COMMENT_CLOSE_TAG
Chris@0 50 && $tokens[$commentEnd]['code'] !== T_COMMENT
Chris@0 51 ) {
Chris@0 52 return;
Chris@0 53 }
Chris@0 54
Chris@0 55 if ($tokens[$commentEnd]['code'] === T_COMMENT) {
Chris@0 56 return;
Chris@0 57 }
Chris@0 58
Chris@0 59 $commentStart = $tokens[$commentEnd]['comment_opener'];
Chris@0 60
Chris@0 61 $empty = array(
Chris@0 62 T_DOC_COMMENT_WHITESPACE,
Chris@0 63 T_DOC_COMMENT_STAR,
Chris@0 64 );
Chris@0 65
Chris@0 66 $short = $phpcsFile->findNext($empty, ($commentStart + 1), $commentEnd, true);
Chris@0 67 if ($short === false) {
Chris@0 68 // No content at all.
Chris@0 69 return;
Chris@0 70 }
Chris@0 71
Chris@0 72 // Account for the fact that a short description might cover
Chris@0 73 // multiple lines.
Chris@0 74 $shortContent = $tokens[$short]['content'];
Chris@0 75 $shortEnd = $short;
Chris@0 76 for ($i = ($short + 1); $i < $commentEnd; $i++) {
Chris@0 77 if ($tokens[$i]['code'] === T_DOC_COMMENT_STRING) {
Chris@0 78 if ($tokens[$i]['line'] === ($tokens[$shortEnd]['line'] + 1)) {
Chris@0 79 $shortContent .= $tokens[$i]['content'];
Chris@0 80 $shortEnd = $i;
Chris@0 81 } else {
Chris@0 82 break;
Chris@0 83 }
Chris@0 84 }
Chris@0 85 }
Chris@0 86
Chris@0 87 // Check if a hook implementation doc block is formatted correctly.
Chris@0 88 if (preg_match('/^[\s]*Implement[^\n]+?hook_[^\n]+/i', $shortContent, $matches) === 1) {
Chris@0 89 if (strstr($matches[0], 'Implements ') === false || strstr($matches[0], 'Implements of') !== false
Chris@0 90 || preg_match('/ (drush_)?hook_[a-zA-Z0-9_]+\(\)( for .+)?\.$/', $matches[0]) !== 1
Chris@0 91 ) {
Chris@0 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);
Chris@0 93 } else {
Chris@0 94 // Check that a hook implementation does not duplicate @param and
Chris@0 95 // @return documentation.
Chris@0 96 foreach ($tokens[$commentStart]['comment_tags'] as $pos => $tag) {
Chris@0 97 if ($tokens[$tag]['content'] === '@param') {
Chris@0 98 $warn = 'Hook implementations should not duplicate @param documentation';
Chris@0 99 $phpcsFile->addWarning($warn, $tag, 'HookParamDoc');
Chris@0 100 }
Chris@0 101
Chris@0 102 if ($tokens[$tag]['content'] === '@return') {
Chris@0 103 $warn = 'Hook implementations should not duplicate @return documentation';
Chris@0 104 $phpcsFile->addWarning($warn, $tag, 'HookReturnDoc');
Chris@0 105 }
Chris@0 106 }
Chris@0 107 }//end if
Chris@0 108 }//end if
Chris@0 109
Chris@0 110 }//end process()
Chris@0 111
Chris@0 112
Chris@0 113 }//end class