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

Source for file CyclomaticComplexitySniff.php

Documentation is available at CyclomaticComplexitySniff.php

  1. <?php
  2. /**
  3.  * Checks the cyclomatic complexity (McCabe) for functions.
  4.  *
  5.  * The cyclomatic complexity (also called McCabe code metrics)
  6.  * indicates the complexity within a function by counting
  7.  * the different paths the function includes.
  8.  *
  9.  * @author    Johann-Peter Hartmann <hartmann@mayflower.de>
  10.  * @author    Greg Sherwood <gsherwood@squiz.net>
  11.  * @copyright 2007-2014 Mayflower GmbH
  12.  * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
  13.  */
  14.  
  15. namespace PHP_CodeSniffer\Standards\Generic\Sniffs\Metrics;
  16.  
  17. use PHP_CodeSniffer\Sniffs\Sniff;
  18. use PHP_CodeSniffer\Files\File;
  19.  
  20. class CyclomaticComplexitySniff implements Sniff
  21. {
  22.  
  23.     /**
  24.      * A complexity higher than this value will throw a warning.
  25.      *
  26.      * @var integer 
  27.      */
  28.     public $complexity = 10;
  29.  
  30.     /**
  31.      * A complexity higher than this value will throw an error.
  32.      *
  33.      * @var integer 
  34.      */
  35.     public $absoluteComplexity = 20;
  36.  
  37.  
  38.     /**
  39.      * Returns an array of tokens this test wants to listen for.
  40.      *
  41.      * @return array 
  42.      */
  43.     public function register()
  44.     {
  45.         return array(T_FUNCTION);
  46.  
  47.     }//end register()
  48.  
  49.  
  50.     /**
  51.      * Processes this test, when one of its tokens is encountered.
  52.      *
  53.      * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
  54.      * @param int                         $stackPtr  The position of the current token
  55.      *                                                in the stack passed in $tokens.
  56.      *
  57.      * @return void 
  58.      */
  59.     public function process(File $phpcsFile$stackPtr)
  60.     {
  61.         $this->currentFile $phpcsFile;
  62.  
  63.         $tokens $phpcsFile->getTokens();
  64.  
  65.         // Ignore abstract methods.
  66.         if (isset($tokens[$stackPtr]['scope_opener']=== false{
  67.             return;
  68.         }
  69.  
  70.         // Detect start and end of this function definition.
  71.         $start $tokens[$stackPtr]['scope_opener'];
  72.         $end   $tokens[$stackPtr]['scope_closer'];
  73.  
  74.         // Predicate nodes for PHP.
  75.         $find = array(
  76.                  T_CASE    => true,
  77.                  T_DEFAULT => true,
  78.                  T_CATCH   => true,
  79.                  T_IF      => true,
  80.                  T_FOR     => true,
  81.                  T_FOREACH => true,
  82.                  T_WHILE   => true,
  83.                  T_DO      => true,
  84.                  T_ELSEIF  => true,
  85.                 );
  86.  
  87.         $complexity = 1;
  88.  
  89.         // Iterate from start to end and count predicate nodes.
  90.         for ($i ($start + 1)$i $end$i++{
  91.             if (isset($find[$tokens[$i]['code']]=== true{
  92.                 $complexity++;
  93.             }
  94.         }
  95.  
  96.         if ($complexity $this->absoluteComplexity{
  97.             $error 'Function\'s cyclomatic complexity (%s) exceeds allowed maximum of %s';
  98.             $data  = array(
  99.                       $complexity,
  100.                       $this->absoluteComplexity,
  101.                      );
  102.             $phpcsFile->addError($error$stackPtr'MaxExceeded'$data);
  103.         else if ($complexity $this->complexity{
  104.             $warning 'Function\'s cyclomatic complexity (%s) exceeds %s; consider refactoring the function';
  105.             $data    = array(
  106.                         $complexity,
  107.                         $this->complexity,
  108.                        );
  109.             $phpcsFile->addWarning($warning$stackPtr'TooHigh'$data);
  110.         }
  111.  
  112.     }//end process()
  113.  
  114.  
  115. }//end class

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