forked from roodi/roodi
/
cyclomatic_complexity_method_check.rb
32 lines (28 loc) · 1.19 KB
/
cyclomatic_complexity_method_check.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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]
end
def evaluate(node)
complexity = count_complexity(node)
add_error "Method name \"#{node[1]}\" has a cyclomatic complexity is #{complexity}. It should be #{@complexity} or less." unless complexity <= @complexity
end
end
end
end