3
* This file is part of the CodeAnalysis addon for PHP_CodeSniffer.
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
17
* Detects unnecessary overriden methods that simply call their parent.
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.
25
* public function __construct($a, $b) {
26
* parent::__construct($a, $b);
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
39
class Generic_Sniffs_CodeAnalysis_UselessOverridingMethodSniff implements PHP_CodeSniffer_Sniff
44
* Registers the tokens that this sniff wants to listen for.
46
* @return array(integer)
48
public function register()
50
return array(T_FUNCTION);
56
* Processes this test, when one of its tokens is encountered.
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.
64
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
66
$tokens = $phpcsFile->getTokens();
67
$token = $tokens[$stackPtr];
69
// Skip function without body.
70
if (isset($token['scope_opener']) === false) {
75
$methodName = $phpcsFile->getDeclarationName($stackPtr);
77
// Get all parameters from method signature.
79
foreach ($phpcsFile->getMethodParameters($stackPtr) as $param) {
80
$signature[] = $param['name'];
83
$next = ++$token['scope_opener'];
84
$end = --$token['scope_closer'];
86
for (; $next <= $end; ++$next) {
87
$code = $tokens[$next]['code'];
89
if (in_array($code, PHP_CodeSniffer_Tokens::$emptyTokens) === true) {
91
} else if ($code === T_RETURN) {
98
// Any token except 'parent' indicates correct code.
99
if ($tokens[$next]['code'] !== T_PARENT) {
103
// Find next non empty token index, should be double colon.
104
$next = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($next + 1), null, true);
106
// Skip for invalid code.
107
if ($next === false || $tokens[$next]['code'] !== T_DOUBLE_COLON) {
111
// Find next non empty token index, should be the function name.
112
$next = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($next + 1), null, true);
114
// Skip for invalid code or other method.
115
if ($next === false || $tokens[$next]['content'] !== $methodName) {
119
// Find next non empty token index, should be the open parenthesis.
120
$next = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($next + 1), null, true);
122
// Skip for invalid code.
123
if ($next === false || $tokens[$next]['code'] !== T_OPEN_PARENTHESIS) {
127
$validParameterTypes = array(
130
T_CONSTANT_ENCAPSED_STRING,
133
$parameters = array('');
134
$parenthesisCount = 1;
135
$count = count($tokens);
136
for (++$next; $next < $count; ++$next) {
137
$code = $tokens[$next]['code'];
139
if ($code === T_OPEN_PARENTHESIS) {
141
} else if ($code === T_CLOSE_PARENTHESIS) {
143
} else if ($parenthesisCount === 1 && $code === T_COMMA) {
145
} else if (in_array($code, PHP_CodeSniffer_Tokens::$emptyTokens) === false) {
146
$parameters[(count($parameters) - 1)] .= $tokens[$next]['content'];
149
if ($parenthesisCount === 0) {
154
$next = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($next + 1), null, true);
155
if ($next === false || $tokens[$next]['code'] !== T_SEMICOLON) {
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) {
168
$parameters = array_map('trim', $parameters);
169
$parameters = array_filter($parameters);
171
if (count($parameters) === count($signature) && $parameters === $signature) {
172
$phpcsFile->addWarning('Useless method overriding detected', $stackPtr, 'Found');
b'\\ No newline at end of file'