forked from troessner/reek
-
Notifications
You must be signed in to change notification settings - Fork 0
/
smell_detector.rb
77 lines (62 loc) · 1.81 KB
/
smell_detector.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
class Class
def name_words
class_name = name.split(/::/)[-1]
class_name.gsub(/([a-z])([A-Z])/) { |sub| "#{$1} #{$2}"}.split
end
end
module Reek
module Smells
class SmellDetector
# The name of the config field that lists the names of code contexts
# that should not be checked. Add this field to the config for each
# smell that should ignore this code element.
EXCLUDE_KEY = 'exclude'
# The name fo the config field that specifies whether a smell is
# enabled. Set to +true+ or +false+.
ENABLED_KEY = 'enabled'
def self.class_name
self.name.split(/::/)[-1]
end
def self.contexts # :nodoc:
[:defn, :defs]
end
def self.default_config
{
ENABLED_KEY => true,
EXCLUDE_KEY => []
}
end
def self.listen(hooks, config)
detector = new(config[class_name])
contexts.each { |ctx| hooks[ctx] << detector }
end
def initialize(config)
@enabled = config[ENABLED_KEY]
@exceptions = config[EXCLUDE_KEY]
@smells_found = []
end
def examine(context)
before = @smells_found.size
examine_context(context) if @enabled and !exception?(context)
@smells_found.length > before
end
def examine_context(context)
end
def exception?(context)
return false if @exceptions.nil? or @exceptions.length == 0
context.matches?(@exceptions)
end
def found(scope, warning)
smell = SmellWarning.new(self, scope, warning)
@smells_found << smell
smell
end
def report_on(report)
@smells_found.each {|smell| report << smell}
end
def smell_name
self.class.name_words.join(' ')
end
end
end
end