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

Source for file UnnecessaryFinalModifierSniff.php

Documentation is available at UnnecessaryFinalModifierSniff.php

  1. <?php
  2. /**
  3.  * Detects unnecessary final modifiers inside of final classes.
  4.  *
  5.  * This rule is based on the PMD rule catalog. The Unnecessary Final Modifier
  6.  * sniff detects the use of the final modifier inside of a final class which
  7.  * is unnecessary.
  8.  *
  9.  * <code>
  10.  * final class Foo
  11.  * {
  12.  *     public final function bar()
  13.  *     {
  14.  *     }
  15.  * }
  16.  * </code>
  17.  *
  18.  * @author    Manuel Pichler <mapi@manuel-pichler.de>
  19.  * @copyright 2007-2014 Manuel Pichler. All rights reserved.
  20.  * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
  21.  */
  22.  
  23. namespace PHP_CodeSniffer\Standards\Generic\Sniffs\CodeAnalysis;
  24.  
  25. use PHP_CodeSniffer\Sniffs\Sniff;
  26. use PHP_CodeSniffer\Files\File;
  27. use PHP_CodeSniffer\Util\Tokens;
  28.  
  29. class UnnecessaryFinalModifierSniff implements Sniff
  30. {
  31.  
  32.  
  33.     /**
  34.      * Registers the tokens that this sniff wants to listen for.
  35.      *
  36.      * @return int[] 
  37.      */
  38.     public function register()
  39.     {
  40.         return array(T_CLASS);
  41.  
  42.     }//end register()
  43.  
  44.  
  45.     /**
  46.      * Processes this test, when one of its tokens is encountered.
  47.      *
  48.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
  49.      * @param int                         $stackPtr  The position of the current token
  50.      *                                                in the stack passed in $tokens.
  51.      *
  52.      * @return void 
  53.      */
  54.     public function process(File $phpcsFile$stackPtr)
  55.     {
  56.         $tokens $phpcsFile->getTokens();
  57.         $token  $tokens[$stackPtr];
  58.  
  59.         // Skip for-statements without body.
  60.         if (isset($token['scope_opener']=== false{
  61.             return;
  62.         }
  63.  
  64.         // Fetch previous token.
  65.         $prev $phpcsFile->findPrevious(Tokens::$emptyTokens($stackPtr - 1)nulltrue);
  66.  
  67.         // Skip for non final class.
  68.         if ($prev === false || $tokens[$prev]['code'!== T_FINAL{
  69.             return;
  70.         }
  71.  
  72.         $next = ++$token['scope_opener'];
  73.         $end  = --$token['scope_closer'];
  74.  
  75.         for ($next <= $end; ++$next{
  76.             if ($tokens[$next]['code'=== T_FINAL{
  77.                 $error 'Unnecessary FINAL modifier in FINAL class';
  78.                 $phpcsFile->addWarning($error$next'Found');
  79.             }
  80.         }
  81.  
  82.     }//end process()
  83.  
  84.  
  85. }//end class

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