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

Source for file NestingLevelSniff.php

Documentation is available at NestingLevelSniff.php

  1. <?php
  2. /**
  3.  * Checks the nesting level for methods.
  4.  *
  5.  * @author    Johann-Peter Hartmann <hartmann@mayflower.de>
  6.  * @author    Greg Sherwood <gsherwood@squiz.net>
  7.  * @copyright 2007-2014 Mayflower GmbH
  8.  * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
  9.  */
  10.  
  11. namespace PHP_CodeSniffer\Standards\Generic\Sniffs\Metrics;
  12.  
  13. use PHP_CodeSniffer\Sniffs\Sniff;
  14. use PHP_CodeSniffer\Files\File;
  15.  
  16. class NestingLevelSniff implements Sniff
  17. {
  18.  
  19.     /**
  20.      * A nesting level higher than this value will throw a warning.
  21.      *
  22.      * @var integer 
  23.      */
  24.     public $nestingLevel = 5;
  25.  
  26.     /**
  27.      * A nesting level higher than this value will throw an error.
  28.      *
  29.      * @var integer 
  30.      */
  31.     public $absoluteNestingLevel = 10;
  32.  
  33.  
  34.     /**
  35.      * Returns an array of tokens this test wants to listen for.
  36.      *
  37.      * @return array 
  38.      */
  39.     public function register()
  40.     {
  41.         return array(T_FUNCTION);
  42.  
  43.     }//end register()
  44.  
  45.  
  46.     /**
  47.      * Processes this test, when one of its tokens is encountered.
  48.      *
  49.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
  50.      * @param int                         $stackPtr  The position of the current token
  51.      *                                                in the stack passed in $tokens.
  52.      *
  53.      * @return void 
  54.      */
  55.     public function process(File $phpcsFile$stackPtr)
  56.     {
  57.         $tokens $phpcsFile->getTokens();
  58.  
  59.         // Ignore abstract methods.
  60.         if (isset($tokens[$stackPtr]['scope_opener']=== false{
  61.             return;
  62.         }
  63.  
  64.         // Detect start and end of this function definition.
  65.         $start $tokens[$stackPtr]['scope_opener'];
  66.         $end   $tokens[$stackPtr]['scope_closer'];
  67.  
  68.         $nestingLevel = 0;
  69.  
  70.         // Find the maximum nesting level of any token in the function.
  71.         for ($i ($start + 1)$i $end$i++{
  72.             $level $tokens[$i]['level'];
  73.             if ($nestingLevel $level{
  74.                 $nestingLevel $level;
  75.             }
  76.         }
  77.  
  78.         // We subtract the nesting level of the function itself.
  79.         $nestingLevel ($nestingLevel $tokens[$stackPtr]['level'- 1);
  80.  
  81.         if ($nestingLevel $this->absoluteNestingLevel{
  82.             $error 'Function\'s nesting level (%s) exceeds allowed maximum of %s';
  83.             $data  = array(
  84.                       $nestingLevel,
  85.                       $this->absoluteNestingLevel,
  86.                      );
  87.             $phpcsFile->addError($error$stackPtr'MaxExceeded'$data);
  88.         else if ($nestingLevel $this->nestingLevel{
  89.             $warning 'Function\'s nesting level (%s) exceeds %s; consider refactoring the function';
  90.             $data    = array(
  91.                         $nestingLevel,
  92.                         $this->nestingLevel,
  93.                        );
  94.             $phpcsFile->addWarning($warning$stackPtr'TooHigh'$data);
  95.         }
  96.  
  97.     }//end process()
  98.  
  99.  
  100. }//end class

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