Subversion Repositories Applications.framework

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
5 aurelien 1
<?php
2
/**
3
 * This file is part of the CodeAnalysis addon for PHP_CodeSniffer.
4
 *
5
 * PHP version 5
6
 *
7
 * @category  PHP
8
 * @package   PHP_CodeSniffer
9
 * @author    Greg Sherwood <gsherwood@squiz.net>
10
 * @author    Manuel Pichler <mapi@manuel-pichler.de>
11
 * @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
12
 * @license   http://www.opensource.org/licenses/bsd-license.php BSD License
13
 * @version   CVS: $Id: UnusedFunctionParameterSniff.php,v 1.1 2008/02/06 02:38:36 squiz Exp $
14
 * @link      http://pear.php.net/package/PHP_CodeSniffer
15
 */
16
 
17
/**
18
 * Checks the for unused function parameters.
19
 *
20
 * This sniff checks that all function parameters are used in the function body.
21
 * One exception is made for empty function bodies or function bodies that only
22
 * contain comments. This could be usefull for the classes that implement an
23
 * interface that defines multiple methods but the implementation only needs some
24
 * of them.
25
 *
26
 * @category  PHP
27
 * @package   PHP_CodeSniffer
28
 * @author    Manuel Pichler <mapi@manuel-pichler.de>
29
 * @copyright 2007-2008 Manuel Pichler. All rights reserved.
30
 * @license   http://www.opensource.org/licenses/bsd-license.php BSD License
31
 * @version   Release: 1.2.0RC1
32
 * @link      http://pear.php.net/package/PHP_CodeSniffer
33
 */
34
class Generic_Sniffs_CodeAnalysis_UnusedFunctionParameterSniff implements PHP_CodeSniffer_Sniff
35
{
36
 
37
 
38
    /**
39
     * Returns an array of tokens this test wants to listen for.
40
     *
41
     * @return array
42
     */
43
    public function register()
44
    {
45
        return array(T_FUNCTION);
46
 
47
    }//end register()
48
 
49
 
50
    /**
51
     * Processes this test, when one of its tokens is encountered.
52
     *
53
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
54
     * @param int                  $stackPtr  The position of the current token
55
     *                                        in the stack passed in $tokens.
56
     *
57
     * @return void
58
     */
59
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
60
    {
61
        $tokens = $phpcsFile->getTokens();
62
        $token  = $tokens[$stackPtr];
63
 
64
        // Skip broken function declarations.
65
        if (isset($token['scope_opener']) === false || isset($token['parenthesis_opener']) === false) {
66
            return;
67
        }
68
 
69
        $params = array();
70
        foreach ($phpcsFile->getMethodParameters($stackPtr) as $param) {
71
            $params[$param['name']] = $stackPtr;
72
        }
73
 
74
        $next = ++$token['scope_opener'];
75
        $end  = --$token['scope_closer'];
76
 
77
        $emptyBody = true;
78
 
79
        for (; $next <= $end; ++$next) {
80
            $token = $tokens[$next];
81
            $code  = $token['code'];
82
 
83
            // Ingorable tokens.
84
            if (in_array($code, PHP_CodeSniffer_Tokens::$emptyTokens) === true) {
85
                continue;
86
            } else if ($code === T_THROW && $emptyBody === true) {
87
                // Throw statement and an empty body indicate an interface method.
88
                return;
89
            } else if ($code === T_RETURN && $emptyBody === true) {
90
                // Return statement and an empty body indicate an interface method.
91
                $tmp = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($next + 1), null, true);
92
                if ($tmp === false) {
93
                    return;
94
                }
95
 
96
                // There is a return.
97
                if ($tokens[$tmp] === T_SEMICOLON) {
98
                    return;
99
                }
100
 
101
                $tmp = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($tmp + 1), null, true);
102
 
103
                // There is a return <token>.
104
                if ($tmp !== false && $tokens[$tmp] === T_SEMICOLON) {
105
                     return;
106
                }
107
            }//end if
108
 
109
            $emptyBody = false;
110
 
111
            if ($code === T_VARIABLE && isset($params[$token['content']]) === true) {
112
                unset($params[$token['content']]);
113
            } else if ($code === T_DOUBLE_QUOTED_STRING) {
114
                // Tokenize double quote string.
115
                $strTokens = token_get_all(sprintf('<?php %s;?>', $token['content']));
116
 
117
                foreach ($strTokens as $tok) {
118
                    if (is_array($tok) === false || $tok[0] !== T_VARIABLE ) {
119
                        continue;
120
                    }
121
 
122
                    if (isset($params[$tok[1]]) === true) {
123
                        unset($params[$tok[1]]);
124
                    }
125
                }
126
            }//end if
127
        }//end for
128
 
129
        if ($emptyBody === false && count($params) > 0) {
130
            foreach ($params as $paramName => $position) {
131
                $error = 'The method parameter '.$paramName.' is never used';
132
                $phpcsFile->addWarning($error, $position);
133
            }
134
        }
135
 
136
    }//end process()
137
 
138
 
139
}//end class
140
 
141
?>