3
* Warns when function values are returned directly.
8
* @package PHP_CodeSniffer_MySource
9
* @author Greg Sherwood <gsherwood@squiz.net>
10
* @copyright 2006-2012 Squiz Pty Ltd (ABN 77 084 670 600)
11
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
12
* @link http://pear.php.net/package/PHP_CodeSniffer
16
* Warns when function values are returned directly.
19
* @package PHP_CodeSniffer_MySource
20
* @author Greg Sherwood <gsherwood@squiz.net>
21
* @copyright 2006-2012 Squiz Pty Ltd (ABN 77 084 670 600)
22
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
23
* @version Release: 1.5.0RC2
24
* @link http://pear.php.net/package/PHP_CodeSniffer
26
class MySource_Sniffs_PHP_ReturnFunctionValueSniff implements PHP_CodeSniffer_Sniff
31
* Returns an array of tokens this test wants to listen for.
35
public function register()
37
return array(T_RETURN);
43
* Processes this sniff, when one of its tokens is encountered.
45
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
46
* @param int $stackPtr The position of the current token in
47
* the stack passed in $tokens.
51
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
53
$tokens = $phpcsFile->getTokens();
55
$functionName = $phpcsFile->findNext(T_STRING, ($stackPtr + 1), null, false, null, true);
57
while ($functionName !== false) {
58
// Check if this is really a function.
59
$bracket = $phpcsFile->findNext(T_WHITESPACE, ($functionName + 1), null, true);
60
if ($tokens[$bracket]['code'] !== T_OPEN_PARENTHESIS) {
61
// Not a function call.
62
$functionName = $phpcsFile->findNext(T_STRING, ($functionName + 1), null, false, null, true);
66
$error = 'The result of a function call should be assigned to a variable before being returned';
67
$phpcsFile->addWarning($error, $stackPtr, 'NotAssigned');