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