-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathFunctionCommentSniff.php
More file actions
164 lines (149 loc) · 5.7 KB
/
FunctionCommentSniff.php
File metadata and controls
164 lines (149 loc) · 5.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
/**
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @link https://github.com/cakephp/cakephp-codesniffer
* @since CakePHP CodeSniffer 0.1.24
* @license https://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace CakePHP\Sniffs\Commenting;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;
/**
* Parses and verifies the doc comments for functions.
*
* Verifies that :
* <ul>
* <li>A comment exists</li>
* <li>No spacing between doc comment and function</li>
* <li>Any throw tag must have a comment</li>
* </ul>
*
* @category PHP
* @package PHP_CodeSniffer
* @author Greg Sherwood <gsherwood@squiz.net>
* @author Marc McIntyre <mmcintyre@squiz.net>
* @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
* @version Release: @package_version@
* @link https://pear.php.net/package/PHP_CodeSniffer
*/
class FunctionCommentSniff implements Sniff
{
/**
* Returns an array of tokens this test wants to listen for.
*
* @return array
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingNativeTypeHint
*/
public function register()
{
return [T_FUNCTION];
}
/**
* @inheritDoc
*/
public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$docCommentEnd = $phpcsFile->findPrevious(
[T_DOC_COMMENT_CLOSE_TAG, T_SEMICOLON, T_CLOSE_CURLY_BRACKET, T_OPEN_CURLY_BRACKET],
$stackPtr - 1,
null,
);
if ($docCommentEnd === false || $tokens[$docCommentEnd]['code'] !== T_DOC_COMMENT_CLOSE_TAG) {
$phpcsFile->addError(
'Missing doc comment for function %s()',
$stackPtr,
'Missing',
[$phpcsFile->getDeclarationName($stackPtr)],
);
return;
}
$lastEndToken = $docCommentEnd;
do {
$attribute = $phpcsFile->findNext(
[T_ATTRIBUTE],
$lastEndToken + 1,
$stackPtr,
);
if ($attribute !== false) {
if ($tokens[$lastEndToken]['line'] !== $tokens[$attribute]['line'] - 1) {
$phpcsFile->addError(
'There must be no blank lines after the function comment or attribute',
$lastEndToken,
'SpacingAfter',
);
return;
}
$lastEndToken = $tokens[$attribute]['attribute_closer'];
}
} while ($attribute !== false);
if ($tokens[$lastEndToken]['line'] !== $tokens[$stackPtr]['line'] - 1) {
$phpcsFile->addError(
'There must be no blank lines after the function comment or attribute',
$lastEndToken,
'SpacingAfter',
);
}
$commentStart = $tokens[$docCommentEnd]['comment_opener'];
$this->processTagSpacing($phpcsFile, $stackPtr, $commentStart);
$this->processThrows($phpcsFile, $stackPtr, $commentStart);
}
/**
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token in the stack passed in $tokens.
* @return void
*/
protected function processTagSpacing(File $phpcsFile, int $stackPtr, int $commentStart): void
{
$tokens = $phpcsFile->getTokens();
$tags = $tokens[$commentStart]['comment_tags'];
foreach ($tags as $tag) {
if (
$tokens[$tag + 2]['code'] === T_DOC_COMMENT_STRING &&
$phpcsFile->fixer->getTokenContent($tag + 1) !== ' '
) {
$fix = $phpcsFile->addFixableWarning('Should be only one space after tag', $tag, 'TagAlignment');
if ($fix) {
$phpcsFile->fixer->beginChangeset();
$phpcsFile->fixer->replaceToken($tag + 1, ' ');
$phpcsFile->fixer->endChangeset();
}
}
}
}
/**
* Process any throw tags that this function comment has.
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token in the stack passed in $tokens.
* @param int $commentStart The position in the stack where the comment started.
* @return void
*/
protected function processThrows(File $phpcsFile, int $stackPtr, int $commentStart): void
{
$tokens = $phpcsFile->getTokens();
foreach ($tokens[$commentStart]['comment_tags'] as $tag) {
if ($tokens[$tag]['content'] !== '@throws') {
continue;
}
$exception = null;
if ($tokens[$tag + 2]['code'] === T_DOC_COMMENT_STRING) {
$matches = [];
preg_match('/([^\s]+)(?:\s+(.*))?/', $tokens[$tag + 2]['content'], $matches);
$exception = $matches[1] ?? null;
}
if ($exception === null) {
$error = 'Exception type and comment missing for @throws tag in function comment';
$phpcsFile->addWarning($error, $tag, 'InvalidThrows');
}
}
}
}