3

The Pear CodeSniffer seems to come with a Cyclomatic Complexity Sniff (CyclomaticComplexitySniff.php). Does anyone know how to use it? I'd like to calculate the complexity of the code I'm analyzing. I'd like to use the Zend coding standard, but no matter what standard I use though, I don't see complexity information being output.

If anyone has advice regarding this, please let me know.

Thanks.

1 Answers1

10

I have managed to make it work by using the following command:

phpcs <dir|file> -p -s 
  --sniffs=Generic.Metrics.CyclomaticComplexity --standard=Squiz

If you want to see the cyclomatic complexity for all functions instead of only the ones that exceed the default threshold, you have to set the value for complexity to 0 in the file "PEAR\PHP\CodeSniffer\Standards\Squiz\ruleset.xml":

 <rule ref="Generic.Metrics.CyclomaticComplexity">
   <properties>
     <property name="complexity" value="0"/>
Paul Jansen
  • 1,216
  • 1
  • 13
  • 35
  • I have found the default settings (warning for complexity > 10, error for complexity > 20) to be remarkably useful. My initial reaction was always "But...!" when I got a warning/error from this sniff, but I invariably find my code is cleaner when I spend some time figuring out how to refactor the code until it no longer "smells". – Peter Oct 18 '11 at 01:14