-
Notifications
You must be signed in to change notification settings - Fork 101
/
FunctionCommentSniff.php
149 lines (133 loc) · 4.48 KB
/
FunctionCommentSniff.php
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
<?php
/**
* This file is part of the Symfony-coding-standard (phpcs standard)
*
* PHP version 5
*
* @category PHP
* @package Symfony-coding-standard
* @author Authors <Symfony-coding-standard@djoos.github.com>
* @license http://spdx.org/licenses/MIT MIT License
* @link https://github.com/djoos/Symfony-coding-standard
*/
namespace Symfony\Sniffs\Commenting;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Standards\PEAR\Sniffs\Commenting\FunctionCommentSniff as Sniff;
/**
* Symfony standard customization to PEARs FunctionCommentSniff.
*
* Verifies that :
* <ul>
* <li>
* There is a @return tag if a return statement exists inside the method
* </li>
* </ul>
*
* PHP version 5
*
* @category PHP
* @package Symfony-coding-standard
* @author Authors <Symfony-coding-standard@djoos.github.com>
* @license http://spdx.org/licenses/MIT MIT License
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
class FunctionCommentSniff extends Sniff
{
/**
* Process the return comment of this function comment.
*
* @param 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 processReturn(
File $phpcsFile,
$stackPtr,
$commentStart
) {
if ($this->isInheritDoc($phpcsFile, $stackPtr)) {
return;
}
$tokens = $phpcsFile->getTokens();
// Only check for a return comment if a non-void return statement exists
if (isset($tokens[$stackPtr]['scope_opener'])) {
// Start inside the function
$start = $phpcsFile->findNext(
T_OPEN_CURLY_BRACKET,
$stackPtr,
$tokens[$stackPtr]['scope_closer']
);
for ($i = $start; $i < $tokens[$stackPtr]['scope_closer']; ++$i) {
// Skip closures
if ($tokens[$i]['code'] === T_CLOSURE) {
$i = $tokens[$i]['scope_closer'];
continue;
}
// Found a return not in a closure statement
// Run the check on the first which is not only 'return;'
if ($tokens[$i]['code'] === T_RETURN
&& $this->isMatchingReturn($tokens, $i)
) {
parent::processReturn($phpcsFile, $stackPtr, $commentStart);
break;
}
}
}
}
/**
* Is the comment an inheritdoc?
*
* @param File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token
* in the stack passed in $tokens.
*
* @return boolean True if the comment is an inheritdoc
*/
protected function isInheritDoc(File $phpcsFile, $stackPtr)
{
$start = $phpcsFile->findPrevious(T_DOC_COMMENT_OPEN_TAG, $stackPtr - 1);
$end = $phpcsFile->findNext(T_DOC_COMMENT_CLOSE_TAG, $start);
$content = $phpcsFile->getTokensAsString($start, ($end - $start));
return preg_match('#{@inheritdoc}#i', $content) === 1;
}
/**
* Process the function parameter comments.
*
* @param 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 processParams(
File $phpcsFile,
$stackPtr,
$commentStart
) {
if ($this->isInheritDoc($phpcsFile, $stackPtr)) {
return;
}
parent::processParams($phpcsFile, $stackPtr, $commentStart);
}
/**
* Is the return statement matching?
*
* @param array $tokens Array of tokens
* @param int $returnPos Stack position of the T_RETURN token to process
*
* @return boolean True if the return does not return anything
*/
protected function isMatchingReturn($tokens, $returnPos)
{
do {
$returnPos++;
} while ($tokens[$returnPos]['code'] === T_WHITESPACE);
return $tokens[$returnPos]['code'] !== T_SEMICOLON;
}
}