Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/**
3
 * Warns when function values are returned directly.
4
 *
5
 * PHP version 5
6
 *
7
 * @category  PHP
8
 * @package   PHP_CodeSniffer_MySource
9
 * @author    Greg Sherwood <gsherwood@squiz.net>
10
 * @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
11
 * @license   http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
12
 * @version   CVS: $Id: ReturnFunctionValueSniff.php 240175 2007-07-23 01:47:54Z squiz $
13
 * @link      http://pear.php.net/package/PHP_CodeSniffer
14
 */
15
 
16
/**
17
 * Warns when function values are returned directly.
18
 *
19
 * @category  PHP
20
 * @package   PHP_CodeSniffer_MySource
21
 * @author    Greg Sherwood <gsherwood@squiz.net>
22
 * @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
23
 * @license   http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
24
 * @version   Release: 1.2.1
25
 * @link      http://pear.php.net/package/PHP_CodeSniffer
26
 */
27
class MySource_Sniffs_PHP_ReturnFunctionValueSniff implements PHP_CodeSniffer_Sniff
28
{
29
 
30
 
31
    /**
32
     * Returns an array of tokens this test wants to listen for.
33
     *
34
     * @return array
35
     */
36
    public function register()
37
    {
38
        return array(T_RETURN);
39
 
40
    }//end register()
41
 
42
 
43
    /**
44
     * Processes this sniff, when one of its tokens is encountered.
45
     *
46
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
47
     * @param int                  $stackPtr  The position of the current token in
48
     *                                        the stack passed in $tokens.
49
     *
50
     * @return void
51
     */
52
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
53
    {
54
        $tokens = $phpcsFile->getTokens();
55
 
56
        $functionName = $phpcsFile->findNext(T_STRING, ($stackPtr + 1), null, false, null, true);
57
 
58
        while ($functionName !== false) {
59
            // Check if this is really a function.
60
            $bracket = $phpcsFile->findNext(T_WHITESPACE, ($functionName + 1), null, true);
61
            if ($tokens[$bracket]['code'] !== T_OPEN_PARENTHESIS) {
62
                // Not a function call.
63
                $functionName = $phpcsFile->findNext(T_STRING, ($functionName + 1), null, false, null, true);
64
                continue;
65
            }
66
 
67
            $error = 'The result of a function call should be assigned to a variable before being returned';
68
            $phpcsFile->addWarning($error, $stackPtr);
69
            break;
70
        }
71
 
72
    }//end process()
73
 
74
 
75
}//end class
76
 
77
?>