forked from troessner/reek
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sniffer.rb
85 lines (74 loc) · 1.96 KB
/
sniffer.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
78
79
80
81
82
83
84
85
require 'reek/smells/control_couple'
require 'reek/smells/duplication'
require 'reek/smells/feature_envy'
require 'reek/smells/large_class'
require 'reek/smells/long_method'
require 'reek/smells/long_parameter_list'
require 'reek/smells/long_yield_list'
require 'reek/smells/nested_iterators'
require 'reek/smells/uncommunicative_name'
require 'reek/smells/utility_function'
require 'yaml'
class Hash
def push_keys(hash)
keys.each {|key| hash[key].adopt!(self[key]) }
end
def adopt!(other)
other.keys.each do |key|
ov = other[key]
if Array === ov and has_key?(key)
self[key] += ov
else
self[key] = ov
end
end
self
end
def adopt(other)
self.deep_copy.adopt!(other)
end
def deep_copy
YAML::load(YAML::dump(self))
end
end
module Reek
class Sniffer
SMELL_CLASSES = [
Smells::ControlCouple,
Smells::Duplication,
Smells::FeatureEnvy,
Smells::LargeClass,
Smells::LongMethod,
Smells::LongParameterList,
Smells::LongYieldList,
Smells::NestedIterators,
Smells::UncommunicativeName,
Smells::UtilityFunction,
]
def initialize
defaults_file = File.join(File.dirname(__FILE__), '..', '..', 'config', 'defaults.reek')
@config = YAML.load_file(defaults_file)
end
def smell_listeners()
result = Hash.new {|hash,key| hash[key] = [] }
SMELL_CLASSES.each { |smell| smell.listen(result, @config) }
return result
end
def load_local(file)
path = File.expand_path(file)
all_reekfiles(path).each do |rfile|
YAML.load_file(rfile).push_keys(@config)
end
self
end
def all_reekfiles(path)
return [] unless File.exist?(path)
parent = File.dirname(path)
return [] if path == parent
all_reekfiles(parent) + Dir["#{path}/*.reek"]
end
def disable(smell)
@config[smell].adopt!({Reek::Smells::SmellDetector::ENABLED_KEY => false})
end
end
end