~ubuntu-branches/ubuntu/utopic/php-codesniffer/utopic-proposed

« back to all changes in this revision

Viewing changes to PHP_CodeSniffer-1.3.4/CodeSniffer/Standards/Generic/Sniffs/CodeAnalysis/UselessOverridingMethodSniff.php

  • Committer: Package Import Robot
  • Author(s): Thomas Goirand
  • Date: 2012-05-31 16:37:24 UTC
  • mfrom: (1.1.2)
  • Revision ID: package-import@ubuntu.com-20120531163724-u6aiaubu8ks5dh5z
Tags: 1.3.4-0.1
* Non-maintainer upload.
* New upstream release (Closes: #599617, #634825).
* Swtiched debian/copyright to format 1.0 (rewrite was needed anyway, as the
upstream license changed).
* Switched package to pkg-php-tools and debhelper 8 sequencer.
* Now running unit tests at build time (so depends on phpunit (>= 3.6)).
* Section is now PHP.
* Added missing Build-Depends-Indep: php-pear.
* Added missing ${misc:Depends}.
* Added Vcs fields.
* Added homepage field.
* Reviewed short and long description.
* Added dependency on php-timer.
* Standards-Version: is now 3.9.3 (lots of changes, see above...).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 2007-2008 Manuel Pichler. All rights reserved.
 
12
 * @license   http://www.opensource.org/licenses/bsd-license.php BSD License
 
13
 * @link      http://pear.php.net/package/PHP_CodeSniffer
 
14
 */
 
15
 
 
16
/**
 
17
 * Detects unnecessary overriden methods that simply call their parent.
 
18
 *
 
19
 * This rule is based on the PMD rule catalog. The Useless Overriding Method
 
20
 * sniff detects the use of methods that only call their parent classes's method
 
21
 * with the same name and arguments. These methods are not required.
 
22
 *
 
23
 * <code>
 
24
 * class FooBar {
 
25
 *   public function __construct($a, $b) {
 
26
 *     parent::__construct($a, $b);
 
27
 *   }
 
28
 * }
 
29
 * </code>
 
30
 *
 
31
 * @category  PHP
 
32
 * @package   PHP_CodeSniffer
 
33
 * @author    Manuel Pichler <mapi@manuel-pichler.de>
 
34
 * @copyright 2007-2008 Manuel Pichler. All rights reserved.
 
35
 * @license   http://www.opensource.org/licenses/bsd-license.php BSD License
 
36
 * @version   Release: 1.3.4
 
37
 * @link      http://pear.php.net/package/PHP_CodeSniffer
 
38
 */
 
39
class Generic_Sniffs_CodeAnalysis_UselessOverridingMethodSniff implements PHP_CodeSniffer_Sniff
 
40
{
 
41
 
 
42
 
 
43
    /**
 
44
     * Registers the tokens that this sniff wants to listen for.
 
45
     *
 
46
     * @return array(integer)
 
47
     */
 
48
    public function register()
 
49
    {
 
50
        return array(T_FUNCTION);
 
51
 
 
52
    }//end register()
 
53
 
 
54
 
 
55
    /**
 
56
     * Processes this test, when one of its tokens is encountered.
 
57
     *
 
58
     * @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
 
59
     * @param int                  $stackPtr  The position of the current token
 
60
     *                                        in the stack passed in $tokens.
 
61
     *
 
62
     * @return void
 
63
     */
 
64
    public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
 
65
    {
 
66
        $tokens = $phpcsFile->getTokens();
 
67
        $token  = $tokens[$stackPtr];
 
68
 
 
69
        // Skip function without body.
 
70
        if (isset($token['scope_opener']) === false) {
 
71
            return;
 
72
        }
 
73
 
 
74
        // Get function name.
 
75
        $methodName = $phpcsFile->getDeclarationName($stackPtr);
 
76
 
 
77
        // Get all parameters from method signature.
 
78
        $signature = array();
 
79
        foreach ($phpcsFile->getMethodParameters($stackPtr) as $param) {
 
80
            $signature[] = $param['name'];
 
81
        }
 
82
 
 
83
        $next = ++$token['scope_opener'];
 
84
        $end  = --$token['scope_closer'];
 
85
 
 
86
        for (; $next <= $end; ++$next) {
 
87
            $code = $tokens[$next]['code'];
 
88
 
 
89
            if (in_array($code, PHP_CodeSniffer_Tokens::$emptyTokens) === true) {
 
90
                continue;
 
91
            } else if ($code === T_RETURN) {
 
92
                continue;
 
93
            }
 
94
 
 
95
            break;
 
96
        }
 
97
 
 
98
        // Any token except 'parent' indicates correct code.
 
99
        if ($tokens[$next]['code'] !== T_PARENT) {
 
100
            return;
 
101
        }
 
102
 
 
103
        // Find next non empty token index, should be double colon.
 
104
        $next = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($next + 1), null, true);
 
105
 
 
106
        // Skip for invalid code.
 
107
        if ($next === false || $tokens[$next]['code'] !== T_DOUBLE_COLON) {
 
108
            return;
 
109
        }
 
110
 
 
111
        // Find next non empty token index, should be the function name.
 
112
        $next = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($next + 1), null, true);
 
113
 
 
114
        // Skip for invalid code or other method.
 
115
        if ($next === false || $tokens[$next]['content'] !== $methodName) {
 
116
            return;
 
117
        }
 
118
 
 
119
        // Find next non empty token index, should be the open parenthesis.
 
120
        $next = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($next + 1), null, true);
 
121
 
 
122
        // Skip for invalid code.
 
123
        if ($next === false || $tokens[$next]['code'] !== T_OPEN_PARENTHESIS) {
 
124
            return;
 
125
        }
 
126
 
 
127
        $validParameterTypes = array(
 
128
                                T_VARIABLE,
 
129
                                T_LNUMBER,
 
130
                                T_CONSTANT_ENCAPSED_STRING,
 
131
                               );
 
132
 
 
133
        $parameters       = array('');
 
134
        $parenthesisCount = 1;
 
135
        $count            = count($tokens);
 
136
        for (++$next; $next < $count; ++$next) {
 
137
            $code = $tokens[$next]['code'];
 
138
 
 
139
            if ($code === T_OPEN_PARENTHESIS) {
 
140
                ++$parenthesisCount;
 
141
            } else if ($code === T_CLOSE_PARENTHESIS) {
 
142
                --$parenthesisCount;
 
143
            } else if ($parenthesisCount === 1 && $code === T_COMMA) {
 
144
                $parameters[] = '';
 
145
            } else if (in_array($code, PHP_CodeSniffer_Tokens::$emptyTokens) === false) {
 
146
                $parameters[(count($parameters) - 1)] .= $tokens[$next]['content'];
 
147
            }
 
148
 
 
149
            if ($parenthesisCount === 0) {
 
150
                break;
 
151
            }
 
152
        }//end for
 
153
 
 
154
        $next = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($next + 1), null, true);
 
155
        if ($next === false || $tokens[$next]['code'] !== T_SEMICOLON) {
 
156
            return;
 
157
        }
 
158
 
 
159
        // Check rest of the scope.
 
160
        for (++$next; $next <= $end; ++$next) {
 
161
            $code = $tokens[$next]['code'];
 
162
            // Skip for any other content.
 
163
            if (in_array($code, PHP_CodeSniffer_Tokens::$emptyTokens) === false) {
 
164
                return;
 
165
            }
 
166
        }
 
167
 
 
168
        $parameters = array_map('trim', $parameters);
 
169
        $parameters = array_filter($parameters);
 
170
 
 
171
        if (count($parameters) === count($signature) && $parameters === $signature) {
 
172
            $phpcsFile->addWarning('Useless method overriding detected', $stackPtr, 'Found');
 
173
        }
 
174
 
 
175
    }//end process()
 
176
 
 
177
 
 
178
}//end class
 
179
 
 
180
?>
 
 
b'\\ No newline at end of file'