Sha256: a3584fd904fe3991e39977ff890461b19062cde0ecb8571b7c1d80a8864cce3d

Contents?: true

Size: 1.34 KB

Versions: 4

Compression:

Stored size: 1.34 KB

Contents

require 'roodi/checks/cyclomatic_complexity_check'

module Roodi
  module Checks
    # Checks cyclomatic complexity of a method against a specified limit. 
    # 
    # The cyclomatic 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, for a method, 1-4 is considered good, 5-8 ok, 9-10 consider re-factoring, and 
    # 11+ re-factor now!
    class CyclomaticComplexityMethodCheck < CyclomaticComplexityCheck
      DEFAULT_COMPLEXITY = 8
      
      def initialize(options = {})
        complexity = options['complexity'] || DEFAULT_COMPLEXITY
        super(complexity)
      end
      
      def interesting_nodes
        [:defn] + COMPLEXITY_NODE_TYPES
      end

      def evaluate_start_defn(node)
        @method_name = @node[1]
        increase_depth
      end

      def evaluate_end_defn(node)
        decrease_depth
      end
      
      def evaluate_matching_end
        add_error "Method name \"#{@method_name}\" cyclomatic complexity is #{@count}.  It should be #{@complexity} or less." unless @count <= @complexity
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
roodi1.9-2.0.1 lib/roodi/checks/cyclomatic_complexity_method_check.rb
roodi-2.1.0 lib/roodi/checks/cyclomatic_complexity_method_check.rb
roodi-2.0.1 lib/roodi/checks/cyclomatic_complexity_method_check.rb
roodi-2.0.0 lib/roodi/checks/cyclomatic_complexity_method_check.rb