Sha256: 27040e843f37c4e67e14b9a2c5b687a9fe4feaf1f15cd02aac5a828e80e9fe8d

Contents?: true

Size: 1.15 KB

Versions: 5

Compression:

Stored size: 1.15 KB

Contents

require 'roodi/checks/check'

module Roodi
  module Checks
    # Checks cyclomatic complexity against a specified limit. The complexity is
    # measured by the number of "if", "unless", "elsif", "?:", "while", "until", 
    # "for", "rescue", "case", "when", "&&", "and", "||" and "or" statements (plus 
    # one) in the body of the member. It is a measure of the minimum number of 
    # possible paths through the source and therefore the number of required tests. 
    # Generally 1-4 is considered good, 5-7 ok, 8-10 consider re-factoring, and 
    # 11+ re-factor now!
    class CyclomaticComplexityCheck < Check
      def initialize(complexity = 8)
        super()
        @complexity = complexity
      end
      
      protected
      
      def count_complexity(node)
        count_branches(node) + 1
      end

      private

      def count_branches(node)
        complexity_node_types = [:if, :while, :until, :for, :rescue, :case, :when, :and, :or]
        
        count = 0
        count = count + 1 if complexity_node_types.include? node.node_type
        node.children.each {|node| count += count_branches(node)}
        count
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
roodi-0.5 lib/roodi/checks/cyclomatic_complexity_check.rb
roodi-1.0.0 lib/roodi/checks/cyclomatic_complexity_check.rb
roodi-1.1.0 lib/roodi/checks/cyclomatic_complexity_check.rb
roodi-1.2.0 lib/roodi/checks/cyclomatic_complexity_check.rb
roodi-1.1.1 lib/roodi/checks/cyclomatic_complexity_check.rb