Source for file UselessOverridingMethodSniff.php
Documentation is available at UselessOverridingMethodSniff.php
* Detects unnecessary overridden methods that simply call their parent.
* This rule is based on the PMD rule catalog. The Useless Overriding Method
* sniff detects the use of methods that only call their parent classes's method
* with the same name and arguments. These methods are not required.
* public function __construct($a, $b) {
* parent::__construct($a, $b);
* @author Manuel Pichler <mapi@manuel-pichler.de>
* @copyright 2007-2014 Manuel Pichler. All rights reserved.
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
namespace PHP_CodeSniffer\Standards\Generic\Sniffs\CodeAnalysis;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Util\Tokens;
class UselessOverridingMethodSniff implements Sniff
* Registers the tokens that this sniff wants to listen for.
public function register ()
return array (T_FUNCTION );
* Processes this test, when one of its tokens is encountered.
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token
* in the stack passed in $tokens.
public function process (File $phpcsFile, $stackPtr)
$tokens = $phpcsFile->getTokens ();
$token = $tokens[$stackPtr];
// Skip function without body.
if (isset ($token['scope_opener']) === false ) {
$methodName = $phpcsFile->getDeclarationName ($stackPtr);
// Get all parameters from method signature.
foreach ($phpcsFile->getMethodParameters ($stackPtr) as $param) {
$signature[] = $param['name'];
$next = ++ $token['scope_opener'];
$end = -- $token['scope_closer'];
for (; $next <= $end; ++ $next) {
$code = $tokens[$next]['code'];
if (isset (Tokens ::$emptyTokens[$code]) === true ) {
} else if ($code === T_RETURN ) {
// Any token except 'parent' indicates correct code.
if ($tokens[$next]['code'] !== T_PARENT) {
// Find next non empty token index, should be double colon.
$next = $phpcsFile->findNext (Tokens ::$emptyTokens, ($next + 1 ), null , true );
// Skip for invalid code.
if ($next === false || $tokens[$next]['code'] !== T_DOUBLE_COLON ) {
// Find next non empty token index, should be the function name.
$next = $phpcsFile->findNext (Tokens ::$emptyTokens, ($next + 1 ), null , true );
// Skip for invalid code or other method.
if ($next === false || $tokens[$next]['content'] !== $methodName) {
// Find next non empty token index, should be the open parenthesis.
$next = $phpcsFile->findNext (Tokens ::$emptyTokens, ($next + 1 ), null , true );
// Skip for invalid code.
for (++ $next; $next < $count; ++ $next) {
$code = $tokens[$next]['code'];
} else if ($parenthesisCount === 1 && $code === T_COMMA) {
} else if (isset (Tokens ::$emptyTokens[$code]) === false ) {
$parameters[(count($parameters) - 1 )] .= $tokens[$next]['content'];
if ($parenthesisCount === 0 ) {
$next = $phpcsFile->findNext (Tokens ::$emptyTokens, ($next + 1 ), null , true );
if ($next === false || $tokens[$next]['code'] !== T_SEMICOLON) {
// Check rest of the scope.
for (++ $next; $next <= $end; ++ $next) {
$code = $tokens[$next]['code'];
// Skip for any other content.
if (isset (Tokens ::$emptyTokens[$code]) === false ) {
$parameters = array_map('trim', $parameters);
if (count($parameters) === count($signature) && $parameters === $signature) {
$phpcsFile->addWarning ('Possible useless method overriding detected', $stackPtr, 'Found');
Documentation generated on Mon, 11 Mar 2019 15:27:51 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|