reek 1.1.3 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +44 -4
- data/License.txt +20 -0
- data/README.rdoc +83 -0
- data/Rakefile +0 -1
- data/bin/reek +3 -11
- data/config/defaults.reek +20 -1
- data/features/masking_smells.feature +111 -0
- data/features/options.feature +49 -0
- data/features/reports.feature +90 -0
- data/features/samples.feature +284 -0
- data/features/stdin.feature +43 -0
- data/features/step_definitions/reek_steps.rb +35 -0
- data/features/support/env.rb +38 -0
- data/lib/reek.rb +1 -1
- data/lib/reek/adapters/application.rb +47 -0
- data/lib/reek/adapters/config_file.rb +31 -0
- data/lib/reek/adapters/core_extras.rb +72 -0
- data/lib/reek/{object_source.rb → adapters/object_source.rb} +15 -19
- data/lib/reek/{rake_task.rb → adapters/rake_task.rb} +2 -2
- data/lib/reek/adapters/report.rb +91 -0
- data/lib/reek/adapters/source.rb +53 -0
- data/lib/reek/{spec.rb → adapters/spec.rb} +45 -60
- data/lib/reek/block_context.rb +1 -1
- data/lib/reek/class_context.rb +26 -6
- data/lib/reek/code_context.rb +8 -0
- data/lib/reek/code_parser.rb +82 -39
- data/lib/reek/command_line.rb +85 -0
- data/lib/reek/configuration.rb +51 -0
- data/lib/reek/detector_stack.rb +39 -0
- data/lib/reek/exceptions.reek +8 -1
- data/lib/reek/method_context.rb +53 -11
- data/lib/reek/module_context.rb +1 -2
- data/lib/reek/name.rb +8 -2
- data/lib/reek/sexp_formatter.rb +2 -0
- data/lib/reek/smell_warning.rb +26 -8
- data/lib/reek/smells/control_couple.rb +8 -4
- data/lib/reek/smells/data_clump.rb +88 -0
- data/lib/reek/smells/duplication.rb +11 -9
- data/lib/reek/smells/feature_envy.rb +3 -4
- data/lib/reek/smells/large_class.rb +17 -17
- data/lib/reek/smells/long_method.rb +10 -8
- data/lib/reek/smells/long_parameter_list.rb +16 -10
- data/lib/reek/smells/long_yield_list.rb +1 -1
- data/lib/reek/smells/nested_iterators.rb +3 -3
- data/lib/reek/smells/simulated_polymorphism.rb +58 -0
- data/lib/reek/smells/smell_detector.rb +94 -27
- data/lib/reek/smells/uncommunicative_name.rb +23 -23
- data/lib/reek/smells/utility_function.rb +27 -11
- data/lib/reek/sniffer.rb +183 -0
- data/reek.gemspec +5 -5
- data/spec/quality/reek_source_spec.rb +15 -0
- data/spec/reek/adapters/report_spec.rb +49 -0
- data/spec/reek/adapters/should_reek_of_spec.rb +108 -0
- data/spec/reek/adapters/should_reek_only_of_spec.rb +87 -0
- data/spec/reek/adapters/should_reek_spec.rb +92 -0
- data/spec/reek/block_context_spec.rb +7 -1
- data/spec/reek/class_context_spec.rb +39 -16
- data/spec/reek/code_context_spec.rb +7 -7
- data/spec/reek/code_parser_spec.rb +6 -1
- data/spec/reek/config_spec.rb +3 -3
- data/spec/reek/configuration_spec.rb +12 -0
- data/spec/reek/method_context_spec.rb +2 -2
- data/spec/reek/name_spec.rb +24 -0
- data/spec/reek/object_source_spec.rb +23 -0
- data/spec/reek/singleton_method_context_spec.rb +2 -2
- data/spec/reek/smell_warning_spec.rb +53 -0
- data/spec/reek/smells/data_clump_spec.rb +87 -0
- data/spec/reek/smells/duplication_spec.rb +13 -17
- data/spec/reek/smells/feature_envy_spec.rb +23 -28
- data/spec/reek/smells/large_class_spec.rb +109 -34
- data/spec/reek/smells/long_method_spec.rb +140 -3
- data/spec/reek/smells/long_parameter_list_spec.rb +1 -2
- data/spec/reek/smells/simulated_polymorphism_spec.rb +50 -0
- data/spec/reek/smells/smell_detector_spec.rb +53 -0
- data/spec/reek/smells/uncommunicative_name_spec.rb +20 -7
- data/spec/reek/smells/utility_function_spec.rb +76 -67
- data/spec/reek/sniffer_spec.rb +10 -0
- data/spec/samples/all_but_one_masked/clean_one.rb +6 -0
- data/spec/samples/all_but_one_masked/dirty.rb +7 -0
- data/spec/samples/all_but_one_masked/masked.reek +5 -0
- data/spec/samples/clean_due_to_masking/clean_one.rb +6 -0
- data/spec/samples/clean_due_to_masking/clean_three.rb +6 -0
- data/spec/samples/clean_due_to_masking/clean_two.rb +6 -0
- data/spec/samples/clean_due_to_masking/dirty_one.rb +7 -0
- data/spec/samples/clean_due_to_masking/dirty_two.rb +7 -0
- data/spec/samples/clean_due_to_masking/masked.reek +7 -0
- data/spec/samples/corrupt_config_file/corrupt.reek +1 -0
- data/spec/samples/corrupt_config_file/dirty.rb +7 -0
- data/spec/samples/empty_config_file/dirty.rb +7 -0
- data/spec/samples/empty_config_file/empty.reek +0 -0
- data/spec/samples/exceptions.reek +4 -0
- data/spec/{slow/samples → samples}/inline.rb +0 -0
- data/spec/samples/masked/dirty.rb +7 -0
- data/spec/samples/masked/masked.reek +3 -0
- data/spec/samples/mixed_results/clean_one.rb +6 -0
- data/spec/samples/mixed_results/clean_three.rb +6 -0
- data/spec/samples/mixed_results/clean_two.rb +6 -0
- data/spec/samples/mixed_results/dirty_one.rb +7 -0
- data/spec/samples/mixed_results/dirty_two.rb +7 -0
- data/spec/samples/not_quite_masked/dirty.rb +8 -0
- data/spec/samples/not_quite_masked/masked.reek +5 -0
- data/spec/{slow/samples → samples}/optparse.rb +0 -0
- data/spec/samples/overrides/masked/dirty.rb +7 -0
- data/spec/samples/overrides/masked/lower.reek +5 -0
- data/spec/samples/overrides/upper.reek +5 -0
- data/spec/{slow/samples → samples}/redcloth.rb +0 -0
- data/spec/samples/three_clean_files/clean_one.rb +6 -0
- data/spec/samples/three_clean_files/clean_three.rb +6 -0
- data/spec/samples/three_clean_files/clean_two.rb +6 -0
- data/spec/samples/two_smelly_files/dirty_one.rb +7 -0
- data/spec/samples/two_smelly_files/dirty_two.rb +7 -0
- data/spec/spec.opts +1 -1
- data/spec/spec_helper.rb +4 -4
- data/tasks/reek.rake +8 -5
- data/tasks/test.rake +51 -0
- metadata +75 -25
- data/README.txt +0 -6
- data/lib/reek/options.rb +0 -92
- data/lib/reek/report.rb +0 -81
- data/lib/reek/smells/smells.rb +0 -81
- data/lib/reek/source.rb +0 -127
- data/spec/reek/options_spec.rb +0 -13
- data/spec/reek/report_spec.rb +0 -48
- data/spec/reek/smells/smell_spec.rb +0 -24
- data/spec/slow/inline_spec.rb +0 -43
- data/spec/slow/optparse_spec.rb +0 -108
- data/spec/slow/redcloth_spec.rb +0 -101
- data/spec/slow/reek_source_spec.rb +0 -20
- data/spec/slow/script_spec.rb +0 -55
- data/spec/slow/source_list_spec.rb +0 -40
- data/tasks/rspec.rake +0 -21
@@ -16,27 +16,29 @@ module Reek
|
|
16
16
|
# statements permitted in any method.
|
17
17
|
MAX_ALLOWED_STATEMENTS_KEY = 'max_statements'
|
18
18
|
|
19
|
+
DEFAULT_MAX_STATEMENTS = 5
|
20
|
+
|
19
21
|
def self.default_config
|
20
22
|
super.adopt(
|
21
|
-
MAX_ALLOWED_STATEMENTS_KEY =>
|
23
|
+
MAX_ALLOWED_STATEMENTS_KEY => DEFAULT_MAX_STATEMENTS,
|
22
24
|
EXCLUDE_KEY => ['initialize']
|
23
25
|
)
|
24
26
|
end
|
25
27
|
|
28
|
+
attr_reader :max_statements
|
29
|
+
|
26
30
|
def initialize(config = LongMethod.default_config)
|
27
|
-
super
|
28
|
-
@max_statements = config[MAX_ALLOWED_STATEMENTS_KEY]
|
31
|
+
super(config)
|
29
32
|
end
|
30
33
|
|
31
34
|
#
|
32
35
|
# Checks the length of the given +method+.
|
33
|
-
#
|
36
|
+
# Remembers any smells found.
|
34
37
|
#
|
35
|
-
def examine_context(method
|
38
|
+
def examine_context(method)
|
36
39
|
num = method.num_statements
|
37
|
-
return false if num <=
|
38
|
-
|
39
|
-
"has approx #{num} statements")
|
40
|
+
return false if num <= value(MAX_ALLOWED_STATEMENTS_KEY, method, DEFAULT_MAX_STATEMENTS)
|
41
|
+
found(method, "has approx #{num} statements")
|
40
42
|
end
|
41
43
|
end
|
42
44
|
end
|
@@ -1,5 +1,4 @@
|
|
1
1
|
require 'reek/smells/smell_detector'
|
2
|
-
require 'reek/smell_warning'
|
3
2
|
|
4
3
|
module Reek
|
5
4
|
module Smells
|
@@ -18,25 +17,32 @@ module Reek
|
|
18
17
|
# parameters permitted in any method or block.
|
19
18
|
MAX_ALLOWED_PARAMS_KEY = 'max_params'
|
20
19
|
|
20
|
+
# The default value of the +MAX_ALLOWED_PARAMS_KEY+ configuration
|
21
|
+
# value.
|
22
|
+
DEFAULT_MAX_ALLOWED_PARAMS = 3
|
23
|
+
|
21
24
|
def self.default_config
|
22
|
-
super.adopt(
|
25
|
+
super.adopt(
|
26
|
+
MAX_ALLOWED_PARAMS_KEY => DEFAULT_MAX_ALLOWED_PARAMS,
|
27
|
+
SmellConfiguration::OVERRIDES_KEY => {
|
28
|
+
"initialize" => {MAX_ALLOWED_PARAMS_KEY => 5}
|
29
|
+
}
|
30
|
+
)
|
23
31
|
end
|
24
32
|
|
25
|
-
def initialize(config)
|
26
|
-
super
|
27
|
-
@max_params = config['max_params']
|
33
|
+
def initialize(config = LongParameterList.default_config)
|
34
|
+
super(config)
|
28
35
|
@action = 'has'
|
29
36
|
end
|
30
37
|
|
31
38
|
#
|
32
39
|
# Checks the number of parameters in the given scope.
|
33
|
-
#
|
40
|
+
# Remembers any smells found.
|
34
41
|
#
|
35
|
-
def examine_context(ctx
|
42
|
+
def examine_context(ctx)
|
36
43
|
num_params = ctx.parameters.length
|
37
|
-
return false if num_params <=
|
38
|
-
|
39
|
-
"#{@action} #{num_params} parameters")
|
44
|
+
return false if num_params <= value(MAX_ALLOWED_PARAMS_KEY, ctx, DEFAULT_MAX_ALLOWED_PARAMS)
|
45
|
+
found(ctx, "#{@action} #{num_params} parameters")
|
40
46
|
end
|
41
47
|
end
|
42
48
|
end
|
@@ -17,11 +17,11 @@ module Reek
|
|
17
17
|
|
18
18
|
#
|
19
19
|
# Checks whether the given +block+ is inside another.
|
20
|
-
#
|
20
|
+
# Remembers any smells found.
|
21
21
|
#
|
22
|
-
def examine_context(block
|
22
|
+
def examine_context(block)
|
23
23
|
return false unless block.nested_block?
|
24
|
-
|
24
|
+
found(block, 'is nested')
|
25
25
|
end
|
26
26
|
end
|
27
27
|
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'reek/smells/smell_detector'
|
2
|
+
require 'reek/smell_warning'
|
3
|
+
require 'reek/sexp_formatter'
|
4
|
+
|
5
|
+
module Reek
|
6
|
+
module Smells
|
7
|
+
|
8
|
+
#
|
9
|
+
# Simulated Polymorphism occurs when
|
10
|
+
# * code uses a case statement (especially on a type field);
|
11
|
+
# * or code has several if statements in a row (especially if they're comparing against the same value);
|
12
|
+
# * or code uses instance_of?, kind_of?, is_a?, or === to decide what type it's working with;
|
13
|
+
# * or multiple conditionals in different places test the same value.
|
14
|
+
#
|
15
|
+
# Conditional code is hard to read and understand, because the reader must
|
16
|
+
# hold more state in his head.
|
17
|
+
# When the same value is tested in multiple places throughout an application,
|
18
|
+
# any change to the set of possible values will require many methods and
|
19
|
+
# classes to change. Tests for the type of an object may indicate that the
|
20
|
+
# abstraction represented by that type is not completely defined (or understood).
|
21
|
+
#
|
22
|
+
# In the current implementation, Reek only checks for multiple conditionals
|
23
|
+
# testing the same value throughout a single class.
|
24
|
+
#
|
25
|
+
class SimulatedPolymorphism < SmellDetector
|
26
|
+
|
27
|
+
def self.contexts # :nodoc:
|
28
|
+
[:class]
|
29
|
+
end
|
30
|
+
|
31
|
+
# The name of the config field that sets the maximum number of
|
32
|
+
# identical conditionals permitted within any single class.
|
33
|
+
MAX_IDENTICAL_IFS_KEY = 'max_ifs'
|
34
|
+
|
35
|
+
DEFAULT_MAX_IFS = 2
|
36
|
+
|
37
|
+
def self.default_config
|
38
|
+
super.adopt(MAX_IDENTICAL_IFS_KEY => DEFAULT_MAX_IFS)
|
39
|
+
end
|
40
|
+
|
41
|
+
def initialize(config = SimulatedPolymorphism.default_config)
|
42
|
+
super(config)
|
43
|
+
end
|
44
|
+
|
45
|
+
#
|
46
|
+
# Checks the given ClassContext for multiple identical conditional tests.
|
47
|
+
# Remembers any smells found.
|
48
|
+
#
|
49
|
+
def examine_context(klass)
|
50
|
+
counts = Hash.new(0)
|
51
|
+
klass.conditionals.each {|cond| counts[cond] += 1}
|
52
|
+
counts.each do |key, val|
|
53
|
+
found(klass, "tests #{SexpFormatter.format(key)} at least #{val} times") if val > value(MAX_IDENTICAL_IFS_KEY, klass, DEFAULT_MAX_IFS)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'reek/configuration'
|
2
|
+
|
1
3
|
class Class
|
2
4
|
def name_words
|
3
5
|
class_name = name.split(/::/)[-1]
|
@@ -15,52 +17,117 @@ module Reek
|
|
15
17
|
# smell that should ignore this code element.
|
16
18
|
EXCLUDE_KEY = 'exclude'
|
17
19
|
|
18
|
-
# The
|
19
|
-
#
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
20
|
+
# The default value for the +EXCLUDE_KEY+ if it isn't specified
|
21
|
+
# in any configuration file.
|
22
|
+
DEFAULT_EXCLUDE_SET = []
|
23
|
+
|
24
|
+
class << self
|
25
|
+
def class_name
|
26
|
+
self.name.split(/::/)[-1]
|
27
|
+
end
|
28
|
+
|
29
|
+
def contexts # :nodoc:
|
30
|
+
[:defn, :defs]
|
31
|
+
end
|
32
|
+
|
33
|
+
def default_config
|
34
|
+
{
|
35
|
+
SmellConfiguration::ENABLED_KEY => true,
|
36
|
+
EXCLUDE_KEY => DEFAULT_EXCLUDE_SET
|
37
|
+
}
|
38
|
+
end
|
39
|
+
|
40
|
+
def create(config)
|
41
|
+
new(config[class_name])
|
42
|
+
end
|
43
|
+
|
44
|
+
def listen(hooks, config)
|
45
|
+
detector = create(config)
|
46
|
+
detector.listen_to(hooks)
|
47
|
+
detector
|
48
|
+
end
|
24
49
|
end
|
25
50
|
|
26
|
-
def self.
|
27
|
-
|
51
|
+
def initialize(config = self.class.default_config)
|
52
|
+
@config = SmellConfiguration.new(config)
|
53
|
+
@smells_found = Set.new
|
54
|
+
@masked = false
|
28
55
|
end
|
29
|
-
|
30
|
-
def
|
31
|
-
{
|
32
|
-
|
33
|
-
|
34
|
-
|
56
|
+
|
57
|
+
def listen_to(hooks)
|
58
|
+
self.class.contexts.each { |ctx| hooks[ctx] << self }
|
59
|
+
end
|
60
|
+
|
61
|
+
# SMELL: Getter (only used in 1 test)
|
62
|
+
def enabled?
|
63
|
+
@config.enabled?
|
35
64
|
end
|
36
65
|
|
37
|
-
def
|
38
|
-
|
39
|
-
|
66
|
+
def configure(config)
|
67
|
+
my_part = config[self.class.name.split(/::/)[-1]]
|
68
|
+
return unless my_part
|
69
|
+
configure_with(my_part)
|
40
70
|
end
|
41
71
|
|
42
|
-
def
|
43
|
-
@
|
44
|
-
@exceptions = config[EXCLUDE_KEY]
|
72
|
+
def configure_with(config)
|
73
|
+
@config.hash.adopt!(config)
|
45
74
|
end
|
46
75
|
|
47
|
-
def
|
48
|
-
|
49
|
-
examine_context(context, report) if @enabled and !exception?(context)
|
50
|
-
report.length > before
|
76
|
+
def copy
|
77
|
+
self.class.new(@config.hash.deep_copy)
|
51
78
|
end
|
52
79
|
|
53
|
-
def
|
80
|
+
def supersede_with(config)
|
81
|
+
clone = self.copy
|
82
|
+
@masked = true
|
83
|
+
clone.configure_with(config)
|
84
|
+
clone
|
85
|
+
end
|
86
|
+
|
87
|
+
def examine(context)
|
88
|
+
before = @smells_found.size
|
89
|
+
examine_context(context) if @config.enabled? and !exception?(context)
|
90
|
+
@smells_found.length > before
|
91
|
+
end
|
92
|
+
|
93
|
+
def examine_context(context)
|
54
94
|
end
|
55
95
|
|
56
96
|
def exception?(context)
|
57
|
-
|
58
|
-
|
97
|
+
context.matches?(value(EXCLUDE_KEY, context, DEFAULT_EXCLUDE_SET))
|
98
|
+
end
|
99
|
+
|
100
|
+
def found(scope, warning)
|
101
|
+
smell = SmellWarning.new(self, scope, warning, @masked)
|
102
|
+
@smells_found << smell
|
103
|
+
smell
|
104
|
+
end
|
105
|
+
|
106
|
+
def has_smell?(patterns)
|
107
|
+
return false if @masked
|
108
|
+
@smells_found.each { |warning| return true if warning.contains_all?(patterns) }
|
109
|
+
false
|
110
|
+
end
|
111
|
+
|
112
|
+
def report_on(report)
|
113
|
+
@smells_found.each { |smell| smell.report_on(report) }
|
114
|
+
end
|
115
|
+
|
116
|
+
def num_smells
|
117
|
+
@masked ? 0 : @smells_found.length
|
118
|
+
end
|
119
|
+
|
120
|
+
def smelly?
|
121
|
+
(not @masked) and (@smells_found.length > 0)
|
59
122
|
end
|
60
123
|
|
61
124
|
def smell_name
|
62
125
|
self.class.name_words.join(' ')
|
63
126
|
end
|
127
|
+
|
128
|
+
def value(key, ctx, fall_back)
|
129
|
+
@config.value(key, ctx, fall_back)
|
130
|
+
end
|
64
131
|
end
|
65
132
|
end
|
66
133
|
end
|
@@ -15,23 +15,27 @@ module Reek
|
|
15
15
|
#
|
16
16
|
# Currently +UncommunicativeName+ checks for
|
17
17
|
# * 1-character names
|
18
|
-
# * names
|
18
|
+
# * names ending with a number
|
19
19
|
#
|
20
20
|
class UncommunicativeName < SmellDetector
|
21
21
|
|
22
22
|
# The name of the config field that lists the regexps of
|
23
|
-
# smelly names to be
|
23
|
+
# smelly names to be reported.
|
24
24
|
REJECT_KEY = 'reject'
|
25
|
+
|
26
|
+
DEFAULT_REJECT_SET = [/^.$/, /[0-9]$/]
|
25
27
|
|
26
28
|
# The name of the config field that lists the specific names that are
|
27
29
|
# to be treated as exceptions; these names will not be reported as
|
28
30
|
# uncommunicative.
|
29
31
|
ACCEPT_KEY = 'accept'
|
30
32
|
|
33
|
+
DEFAULT_ACCEPT_SET = ['Inline::C']
|
34
|
+
|
31
35
|
def self.default_config
|
32
36
|
super.adopt(
|
33
|
-
REJECT_KEY =>
|
34
|
-
ACCEPT_KEY =>
|
37
|
+
REJECT_KEY => DEFAULT_REJECT_SET,
|
38
|
+
ACCEPT_KEY => DEFAULT_ACCEPT_SET
|
35
39
|
)
|
36
40
|
end
|
37
41
|
|
@@ -40,40 +44,36 @@ module Reek
|
|
40
44
|
end
|
41
45
|
|
42
46
|
def initialize(config = UncommunicativeName.default_config)
|
43
|
-
super
|
44
|
-
@reject = config[REJECT_KEY]
|
45
|
-
@accept = config[ACCEPT_KEY]
|
47
|
+
super(config)
|
46
48
|
end
|
47
49
|
|
48
50
|
#
|
49
51
|
# Checks the given +context+ for uncommunicative names.
|
50
|
-
#
|
52
|
+
# Remembers any smells found.
|
51
53
|
#
|
52
|
-
def examine_context(context
|
53
|
-
consider_name(context
|
54
|
-
consider_variables(context
|
54
|
+
def examine_context(context)
|
55
|
+
consider_name(context)
|
56
|
+
consider_variables(context)
|
55
57
|
end
|
56
58
|
|
57
|
-
def consider_variables(context
|
59
|
+
def consider_variables(context) # :nodoc:
|
58
60
|
context.variable_names.each do |name|
|
59
|
-
next unless is_bad_name?(name)
|
60
|
-
|
61
|
-
"has the variable name '#{name}'")
|
61
|
+
next unless is_bad_name?(name, context)
|
62
|
+
found(context, "has the variable name '#{name}'")
|
62
63
|
end
|
63
64
|
end
|
64
65
|
|
65
|
-
def consider_name(context
|
66
|
+
def consider_name(context) # :nodoc:
|
66
67
|
name = context.name
|
67
|
-
return false if
|
68
|
-
return false unless is_bad_name?(name)
|
69
|
-
|
70
|
-
"has the name '#{name}'")
|
68
|
+
return false if value(ACCEPT_KEY, context, DEFAULT_ACCEPT_SET).include?(context.to_s) # TODO: fq_name() ?
|
69
|
+
return false unless is_bad_name?(name, context)
|
70
|
+
found(context, "has the name '#{name}'")
|
71
71
|
end
|
72
72
|
|
73
|
-
def is_bad_name?(name) # :nodoc:
|
73
|
+
def is_bad_name?(name, context) # :nodoc:
|
74
74
|
var = name.effective_name
|
75
|
-
return false if var == '*' or
|
76
|
-
|
75
|
+
return false if var == '*' or value(ACCEPT_KEY, context, DEFAULT_ACCEPT_SET).include?(var)
|
76
|
+
value(REJECT_KEY, context, DEFAULT_REJECT_SET).detect {|patt| patt === var}
|
77
77
|
end
|
78
78
|
end
|
79
79
|
end
|
@@ -10,24 +10,40 @@ module Reek
|
|
10
10
|
#
|
11
11
|
# Currently +UtilityFunction+ will warn about any method that:
|
12
12
|
#
|
13
|
-
# * is non-empty
|
14
|
-
# * does not override an inherited method
|
15
|
-
# * calls at least one method on another object
|
16
|
-
# * doesn't use any of self's instance variables
|
13
|
+
# * is non-empty, and
|
14
|
+
# * does not override an inherited method, and
|
15
|
+
# * calls at least one method on another object, and
|
16
|
+
# * doesn't use any of self's instance variables, and
|
17
17
|
# * doesn't use any of self's methods
|
18
18
|
#
|
19
19
|
class UtilityFunction < SmellDetector
|
20
20
|
|
21
|
+
# The name of the config field that sets the maximum number of
|
22
|
+
# calls permitted within a helper method. Any method with more than
|
23
|
+
# this number of method calls on other objects will be considered a
|
24
|
+
# candidate Utility Function.
|
25
|
+
HELPER_CALLS_LIMIT_KEY = 'max_helper_calls'
|
26
|
+
|
27
|
+
DEFAULT_HELPER_CALLS_LIMIT = 1
|
28
|
+
|
29
|
+
def self.default_config
|
30
|
+
super.adopt(HELPER_CALLS_LIMIT_KEY => DEFAULT_HELPER_CALLS_LIMIT)
|
31
|
+
end
|
32
|
+
|
33
|
+
def initialize(config = Duplication.default_config)
|
34
|
+
super(config)
|
35
|
+
end
|
36
|
+
|
21
37
|
#
|
22
38
|
# Checks whether the given +method+ is a utility function.
|
23
|
-
#
|
39
|
+
# Remembers any smells found.
|
24
40
|
#
|
25
|
-
def examine_context(method
|
26
|
-
return false if method.
|
27
|
-
method.
|
28
|
-
method.
|
29
|
-
|
30
|
-
|
41
|
+
def examine_context(method)
|
42
|
+
return false if method.num_statements == 0 or
|
43
|
+
method.depends_on_instance? or
|
44
|
+
method.calls.keys.length <= value(HELPER_CALLS_LIMIT_KEY, method, DEFAULT_HELPER_CALLS_LIMIT)
|
45
|
+
# SMELL: loads of calls to value{} with the above pattern
|
46
|
+
found(method, "doesn't depend on instance state")
|
31
47
|
end
|
32
48
|
end
|
33
49
|
end
|