Class: Roodi::Checks::CyclomaticComplexityMethodCheck
- Inherits:
-
CyclomaticComplexityCheck
- Object
- Check
- CyclomaticComplexityCheck
- Roodi::Checks::CyclomaticComplexityMethodCheck
- Defined in:
- lib/roodi/checks/cyclomatic_complexity_method_check.rb
Overview
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!
Constant Summary collapse
- DEFAULT_COMPLEXITY =
8
Constants inherited from CyclomaticComplexityCheck
Roodi::Checks::CyclomaticComplexityCheck::COMPLEXITY_NODE_TYPES
Constants inherited from Check
Roodi::Checks::Check::NODE_TYPES
Instance Method Summary collapse
- #evaluate_end_defn(node) ⇒ Object
- #evaluate_matching_end ⇒ Object
- #evaluate_start_defn(node) ⇒ Object
-
#initialize(options = {}) ⇒ CyclomaticComplexityMethodCheck
constructor
A new instance of CyclomaticComplexityMethodCheck.
- #interesting_nodes ⇒ Object
Methods inherited from Check
#add_error, #end_file, #errors, #evaluate_end, #evaluate_node, #evaluate_node_end, #evaluate_node_start, #evaluate_start, #position, #start_file
Constructor Details
#initialize(options = {}) ⇒ CyclomaticComplexityMethodCheck
Returns a new instance of CyclomaticComplexityMethodCheck.
17 18 19 20 |
# File 'lib/roodi/checks/cyclomatic_complexity_method_check.rb', line 17 def initialize( = {}) complexity = ['complexity'] || DEFAULT_COMPLEXITY super(complexity) end |
Instance Method Details
#evaluate_end_defn(node) ⇒ Object
31 32 33 |
# File 'lib/roodi/checks/cyclomatic_complexity_method_check.rb', line 31 def evaluate_end_defn(node) decrease_depth end |
#evaluate_matching_end ⇒ Object
35 36 37 |
# File 'lib/roodi/checks/cyclomatic_complexity_method_check.rb', line 35 def evaluate_matching_end add_error "Method name \"#{@method_name}\" cyclomatic complexity is #{@count}. It should be #{@complexity} or less." unless @count <= @complexity end |
#evaluate_start_defn(node) ⇒ Object
26 27 28 29 |
# File 'lib/roodi/checks/cyclomatic_complexity_method_check.rb', line 26 def evaluate_start_defn(node) @method_name = @node[1] increase_depth end |
#interesting_nodes ⇒ Object
22 23 24 |
# File 'lib/roodi/checks/cyclomatic_complexity_method_check.rb', line 22 def interesting_nodes [:defn] + COMPLEXITY_NODE_TYPES end |