PHP_CodeSniffer
[ class tree: PHP_CodeSniffer ] [ index: PHP_CodeSniffer ] [ all elements ]

Source for file UselessOverridingMethodSniff.php

Documentation is available at UselessOverridingMethodSniff.php

  1. <?php
  2. /**
  3.  * Detects unnecessary overridden methods that simply call their parent.
  4.  *
  5.  * This rule is based on the PMD rule catalog. The Useless Overriding Method
  6.  * sniff detects the use of methods that only call their parent classes's method
  7.  * with the same name and arguments. These methods are not required.
  8.  *
  9.  * <code>
  10.  * class FooBar {
  11.  *   public function __construct($a, $b) {
  12.  *     parent::__construct($a, $b);
  13.  *   }
  14.  * }
  15.  * </code>
  16.  *
  17.  * @author    Manuel Pichler <mapi@manuel-pichler.de>
  18.  * @copyright 2007-2014 Manuel Pichler. All rights reserved.
  19.  * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
  20.  */
  21.  
  22. namespace PHP_CodeSniffer\Standards\Generic\Sniffs\CodeAnalysis;
  23.  
  24. use PHP_CodeSniffer\Sniffs\Sniff;
  25. use PHP_CodeSniffer\Files\File;
  26. use PHP_CodeSniffer\Util\Tokens;
  27.  
  28. class UselessOverridingMethodSniff implements Sniff
  29. {
  30.  
  31.  
  32.     /**
  33.      * Registers the tokens that this sniff wants to listen for.
  34.      *
  35.      * @return int[] 
  36.      */
  37.     public function register()
  38.     {
  39.         return array(T_FUNCTION);
  40.  
  41.     }//end register()
  42.  
  43.  
  44.     /**
  45.      * Processes this test, when one of its tokens is encountered.
  46.      *
  47.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
  48.      * @param int                         $stackPtr  The position of the current token
  49.      *                                                in the stack passed in $tokens.
  50.      *
  51.      * @return void 
  52.      */
  53.     public function process(File $phpcsFile$stackPtr)
  54.     {
  55.         $tokens $phpcsFile->getTokens();
  56.         $token  $tokens[$stackPtr];
  57.  
  58.         // Skip function without body.
  59.         if (isset($token['scope_opener']=== false{
  60.             return;
  61.         }
  62.  
  63.         // Get function name.
  64.         $methodName $phpcsFile->getDeclarationName($stackPtr);
  65.  
  66.         // Get all parameters from method signature.
  67.         $signature = array();
  68.         foreach ($phpcsFile->getMethodParameters($stackPtras $param{
  69.             $signature[$param['name'];
  70.         }
  71.  
  72.         $next = ++$token['scope_opener'];
  73.         $end  = --$token['scope_closer'];
  74.  
  75.         for ($next <= $end; ++$next{
  76.             $code $tokens[$next]['code'];
  77.  
  78.             if (isset(Tokens::$emptyTokens[$code]=== true{
  79.                 continue;
  80.             else if ($code === T_RETURN{
  81.                 continue;
  82.             }
  83.  
  84.             break;
  85.         }
  86.  
  87.         // Any token except 'parent' indicates correct code.
  88.         if ($tokens[$next]['code'!== T_PARENT{
  89.             return;
  90.         }
  91.  
  92.         // Find next non empty token index, should be double colon.
  93.         $next $phpcsFile->findNext(Tokens::$emptyTokens($next + 1)nulltrue);
  94.  
  95.         // Skip for invalid code.
  96.         if ($next === false || $tokens[$next]['code'!== T_DOUBLE_COLON{
  97.             return;
  98.         }
  99.  
  100.         // Find next non empty token index, should be the function name.
  101.         $next $phpcsFile->findNext(Tokens::$emptyTokens($next + 1)nulltrue);
  102.  
  103.         // Skip for invalid code or other method.
  104.         if ($next === false || $tokens[$next]['content'!== $methodName{
  105.             return;
  106.         }
  107.  
  108.         // Find next non empty token index, should be the open parenthesis.
  109.         $next $phpcsFile->findNext(Tokens::$emptyTokens($next + 1)nulltrue);
  110.  
  111.         // Skip for invalid code.
  112.         if ($next === false || $tokens[$next]['code'!== T_OPEN_PARENTHESIS{
  113.             return;
  114.         }
  115.  
  116.         $parameters       = array('');
  117.         $parenthesisCount = 1;
  118.         $count            count($tokens);
  119.         for (++$next$next $count; ++$next{
  120.             $code $tokens[$next]['code'];
  121.  
  122.             if ($code === T_OPEN_PARENTHESIS{
  123.                 ++$parenthesisCount;
  124.             else if ($code === T_CLOSE_PARENTHESIS{
  125.                 --$parenthesisCount;
  126.             else if ($parenthesisCount === 1 && $code === T_COMMA{
  127.                 $parameters['';
  128.             else if (isset(Tokens::$emptyTokens[$code]=== false{
  129.                 $parameters[(count($parameters- 1).= $tokens[$next]['content'];
  130.             }
  131.  
  132.             if ($parenthesisCount === 0{
  133.                 break;
  134.             }
  135.         }//end for
  136.  
  137.         $next $phpcsFile->findNext(Tokens::$emptyTokens($next + 1)nulltrue);
  138.         if ($next === false || $tokens[$next]['code'!== T_SEMICOLON{
  139.             return;
  140.         }
  141.  
  142.         // Check rest of the scope.
  143.         for (++$next$next <= $end; ++$next{
  144.             $code $tokens[$next]['code'];
  145.             // Skip for any other content.
  146.             if (isset(Tokens::$emptyTokens[$code]=== false{
  147.                 return;
  148.             }
  149.         }
  150.  
  151.         $parameters array_map('trim'$parameters);
  152.         $parameters array_filter($parameters);
  153.  
  154.         if (count($parameters=== count($signature&& $parameters === $signature{
  155.             $phpcsFile->addWarning('Possible useless method overriding detected'$stackPtr'Found');
  156.         }
  157.  
  158.     }//end process()
  159.  
  160.  
  161. }//end class

Documentation generated on Mon, 11 Mar 2019 15:27:51 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.