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@17
|
10 namespace Drupal\Sniffs\Commenting;
|
Chris@17
|
11
|
Chris@17
|
12 use PHP_CodeSniffer\Files\File;
|
Chris@17
|
13 use PHP_CodeSniffer\Sniffs\Sniff;
|
Chris@17
|
14 use PHP_CodeSniffer\Util\Tokens;
|
Chris@17
|
15
|
Chris@0
|
16 /**
|
Chris@0
|
17 * Ensures hook comments on function are correct.
|
Chris@0
|
18 *
|
Chris@0
|
19 * @category PHP
|
Chris@0
|
20 * @package PHP_CodeSniffer
|
Chris@0
|
21 * @link http://pear.php.net/package/PHP_CodeSniffer
|
Chris@0
|
22 */
|
Chris@17
|
23 class HookCommentSniff implements Sniff
|
Chris@0
|
24 {
|
Chris@0
|
25
|
Chris@0
|
26
|
Chris@0
|
27 /**
|
Chris@0
|
28 * Returns an array of tokens this test wants to listen for.
|
Chris@0
|
29 *
|
Chris@0
|
30 * @return array
|
Chris@0
|
31 */
|
Chris@0
|
32 public function register()
|
Chris@0
|
33 {
|
Chris@0
|
34 return array(T_FUNCTION);
|
Chris@0
|
35
|
Chris@0
|
36 }//end register()
|
Chris@0
|
37
|
Chris@0
|
38
|
Chris@0
|
39 /**
|
Chris@0
|
40 * Processes this test, when one of its tokens is encountered.
|
Chris@0
|
41 *
|
Chris@17
|
42 * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
|
Chris@17
|
43 * @param int $stackPtr The position of the current token
|
Chris@17
|
44 * in the stack passed in $tokens.
|
Chris@0
|
45 *
|
Chris@0
|
46 * @return void
|
Chris@0
|
47 */
|
Chris@17
|
48 public function process(File $phpcsFile, $stackPtr)
|
Chris@0
|
49 {
|
Chris@0
|
50 $tokens = $phpcsFile->getTokens();
|
Chris@17
|
51 $find = Tokens::$methodPrefixes;
|
Chris@0
|
52 $find[] = T_WHITESPACE;
|
Chris@0
|
53
|
Chris@0
|
54 $commentEnd = $phpcsFile->findPrevious($find, ($stackPtr - 1), null, true);
|
Chris@0
|
55 if ($tokens[$commentEnd]['code'] !== T_DOC_COMMENT_CLOSE_TAG
|
Chris@0
|
56 && $tokens[$commentEnd]['code'] !== T_COMMENT
|
Chris@0
|
57 ) {
|
Chris@0
|
58 return;
|
Chris@0
|
59 }
|
Chris@0
|
60
|
Chris@0
|
61 if ($tokens[$commentEnd]['code'] === T_COMMENT) {
|
Chris@0
|
62 return;
|
Chris@0
|
63 }
|
Chris@0
|
64
|
Chris@0
|
65 $commentStart = $tokens[$commentEnd]['comment_opener'];
|
Chris@0
|
66
|
Chris@0
|
67 $empty = array(
|
Chris@0
|
68 T_DOC_COMMENT_WHITESPACE,
|
Chris@0
|
69 T_DOC_COMMENT_STAR,
|
Chris@0
|
70 );
|
Chris@0
|
71
|
Chris@0
|
72 $short = $phpcsFile->findNext($empty, ($commentStart + 1), $commentEnd, true);
|
Chris@0
|
73 if ($short === false) {
|
Chris@0
|
74 // No content at all.
|
Chris@0
|
75 return;
|
Chris@0
|
76 }
|
Chris@0
|
77
|
Chris@0
|
78 // Account for the fact that a short description might cover
|
Chris@0
|
79 // multiple lines.
|
Chris@0
|
80 $shortContent = $tokens[$short]['content'];
|
Chris@0
|
81 $shortEnd = $short;
|
Chris@0
|
82 for ($i = ($short + 1); $i < $commentEnd; $i++) {
|
Chris@0
|
83 if ($tokens[$i]['code'] === T_DOC_COMMENT_STRING) {
|
Chris@0
|
84 if ($tokens[$i]['line'] === ($tokens[$shortEnd]['line'] + 1)) {
|
Chris@0
|
85 $shortContent .= $tokens[$i]['content'];
|
Chris@0
|
86 $shortEnd = $i;
|
Chris@0
|
87 } else {
|
Chris@0
|
88 break;
|
Chris@0
|
89 }
|
Chris@0
|
90 }
|
Chris@0
|
91 }
|
Chris@0
|
92
|
Chris@0
|
93 // Check if a hook implementation doc block is formatted correctly.
|
Chris@0
|
94 if (preg_match('/^[\s]*Implement[^\n]+?hook_[^\n]+/i', $shortContent, $matches) === 1) {
|
Chris@0
|
95 if (strstr($matches[0], 'Implements ') === false || strstr($matches[0], 'Implements of') !== false
|
Chris@0
|
96 || preg_match('/ (drush_)?hook_[a-zA-Z0-9_]+\(\)( for .+)?\.$/', $matches[0]) !== 1
|
Chris@0
|
97 ) {
|
Chris@17
|
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');
|
Chris@0
|
99 } else {
|
Chris@0
|
100 // Check that a hook implementation does not duplicate @param and
|
Chris@0
|
101 // @return documentation.
|
Chris@0
|
102 foreach ($tokens[$commentStart]['comment_tags'] as $pos => $tag) {
|
Chris@0
|
103 if ($tokens[$tag]['content'] === '@param') {
|
Chris@0
|
104 $warn = 'Hook implementations should not duplicate @param documentation';
|
Chris@0
|
105 $phpcsFile->addWarning($warn, $tag, 'HookParamDoc');
|
Chris@0
|
106 }
|
Chris@0
|
107
|
Chris@0
|
108 if ($tokens[$tag]['content'] === '@return') {
|
Chris@0
|
109 $warn = 'Hook implementations should not duplicate @return documentation';
|
Chris@0
|
110 $phpcsFile->addWarning($warn, $tag, 'HookReturnDoc');
|
Chris@0
|
111 }
|
Chris@0
|
112 }
|
Chris@0
|
113 }//end if
|
Chris@18
|
114
|
Chris@18
|
115 return;
|
Chris@0
|
116 }//end if
|
Chris@0
|
117
|
Chris@18
|
118 // Check if the doc block just repeats the function name with
|
Chris@18
|
119 // "Implements example_hook_name()".
|
Chris@18
|
120 $functionName = $phpcsFile->getDeclarationName($stackPtr);
|
Chris@18
|
121 if ($functionName !== null && preg_match("/^[\s]*Implements $functionName\(\)\.$/i", $shortContent) === 1) {
|
Chris@18
|
122 $error = 'Hook implementations must be documented with "Implements hook_example()."';
|
Chris@18
|
123 $fix = $phpcsFile->addFixableError($error, $short, 'HookRepeat');
|
Chris@18
|
124 if ($fix === true) {
|
Chris@18
|
125 $newComment = preg_replace('/Implements [^_]+/', 'Implements hook', $shortContent);
|
Chris@18
|
126 $phpcsFile->fixer->replaceToken($short, $newComment);
|
Chris@18
|
127 }
|
Chris@18
|
128 }
|
Chris@18
|
129
|
Chris@0
|
130 }//end process()
|
Chris@0
|
131
|
Chris@0
|
132
|
Chris@0
|
133 }//end class
|