Class: Roodi::Checks::CyclomaticComplexityBlockCheck

Inherits:
CyclomaticComplexityCheck show all
Defined in:
lib/roodi/checks/cyclomatic_complexity_block_check.rb

Overview

Checks cyclomatic complexity of a block 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 block, 1-2 is considered good, 3-4 ok, 5-8 consider re-factoring, and 8+ re-factor now!

Constant Summary collapse

DEFAULT_COMPLEXITY =
4

Constants inherited from CyclomaticComplexityCheck

Roodi::Checks::CyclomaticComplexityCheck::COMPLEXITY_NODE_TYPES

Constants inherited from Check

Roodi::Checks::Check::NODE_TYPES

Instance Attribute Summary

Attributes inherited from CyclomaticComplexityCheck

#complexity

Instance Method Summary collapse

Methods inherited from Check

#add_error, #end_file, #errors, #evaluate_end, #evaluate_node, #evaluate_node_end, #evaluate_node_start, #evaluate_start, make, #position, #start_file

Constructor Details

#initializeCyclomaticComplexityBlockCheck

Returns a new instance of CyclomaticComplexityBlockCheck.



18
19
20
21
# File 'lib/roodi/checks/cyclomatic_complexity_block_check.rb', line 18

def initialize
  super()
  self.complexity = DEFAULT_COMPLEXITY
end

Instance Method Details

#evaluate_end_iter(node) ⇒ Object



31
32
33
# File 'lib/roodi/checks/cyclomatic_complexity_block_check.rb', line 31

def evaluate_end_iter(node)
  decrease_depth
end

#evaluate_matching_endObject



35
36
37
# File 'lib/roodi/checks/cyclomatic_complexity_block_check.rb', line 35

def evaluate_matching_end
  add_error "Block cyclomatic complexity is #{@count}.  It should be #{@complexity} or less." unless @count <= @complexity
end

#evaluate_start_iter(node) ⇒ Object



27
28
29
# File 'lib/roodi/checks/cyclomatic_complexity_block_check.rb', line 27

def evaluate_start_iter(node)
  increase_depth
end

#interesting_nodesObject



23
24
25
# File 'lib/roodi/checks/cyclomatic_complexity_block_check.rb', line 23

def interesting_nodes
  [:iter] + COMPLEXITY_NODE_TYPES
end