Skip to content

Commit

Permalink
Removed some duplication among the smell detectors
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinrutherford committed Jun 14, 2009
1 parent d58eea3 commit 42abc7e
Show file tree
Hide file tree
Showing 10 changed files with 18 additions and 21 deletions.
3 changes: 1 addition & 2 deletions lib/reek/smells/control_couple.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ def initialize(config = ControlCouple.default_config)
#
def examine_context(cond, report)
return unless cond.tests_a_parameter?
report << SmellWarning.new(self, cond,
"is controlled by argument #{SexpFormatter.format(cond.if_expr)}")
report << found(cond, "is controlled by argument #{SexpFormatter.format(cond.if_expr)}")
end
end
end
Expand Down
3 changes: 1 addition & 2 deletions lib/reek/smells/duplication.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ def initialize(config = Duplication.default_config)

def examine_context(method, report)
smelly_calls(method).each do |call|
report << SmellWarning.new(self, method,
"calls #{SexpFormatter.format(call)} multiple times")
report << found(method, "calls #{SexpFormatter.format(call)} multiple times")
end
end

Expand Down
3 changes: 1 addition & 2 deletions lib/reek/smells/feature_envy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ def initialize(config = FeatureEnvy.default_config)
#
def examine_context(context, report)
context.envious_receivers.each do |ref|
report << SmellWarning.new(self, context,
"refers to #{SexpFormatter.format(ref)} more than self")
report << found(context, "refers to #{SexpFormatter.format(ref)} more than self")
end
end
end
Expand Down
6 changes: 2 additions & 4 deletions lib/reek/smells/large_class.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,13 @@ def initialize(config = LargeClass.default_config)
def check_num_methods(klass, report) # :nodoc:
count = klass.num_methods
return if count <= @max_methods
report << SmellWarning.new(self, klass,
"has at least #{count} methods")
report << found(klass, "has at least #{count} methods")
end

def check_num_ivars(klass, report) # :nodoc:
count = klass.variable_names.length
return if count <= @max_instance_variables
report << SmellWarning.new(self, klass,
"has at least #{count} instance variables")
report << found(klass, "has at least #{count} instance variables")
end

#
Expand Down
3 changes: 1 addition & 2 deletions lib/reek/smells/long_method.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ def initialize(config = LongMethod.default_config)
def examine_context(method, report)
num = method.num_statements
return false if num <= @max_statements
report << SmellWarning.new(self, method,
"has approx #{num} statements")
report << found(method, "has approx #{num} statements")
end
end
end
Expand Down
3 changes: 1 addition & 2 deletions lib/reek/smells/long_parameter_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ def initialize(config)
def examine_context(ctx, report)
num_params = ctx.parameters.length
return false if num_params <= @max_params
report << SmellWarning.new(self, ctx,
"#{@action} #{num_params} parameters")
report << found(ctx, "#{@action} #{num_params} parameters")
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/reek/smells/nested_iterators.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def self.contexts # :nodoc:
#
def examine_context(block, report)
return false unless block.nested_block?
report << SmellWarning.new(self, block, 'is nested')
report << found(block, 'is nested')
end
end
end
Expand Down
7 changes: 7 additions & 0 deletions lib/reek/smells/smell_detector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ def self.listen(hooks, config)
def initialize(config)
@enabled = config[ENABLED_KEY]
@exceptions = config[EXCLUDE_KEY]
@smells_found = []
end

def examine(context, report)
Expand All @@ -58,6 +59,12 @@ def exception?(context)
context.matches?(@exceptions)
end

def found(scope, warning)
smell = SmellWarning.new(self, scope, warning)
@smells_found << smell
smell
end

def smell_name
self.class.name_words.join(' ')
end
Expand Down
6 changes: 2 additions & 4 deletions lib/reek/smells/uncommunicative_name.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,15 @@ def examine_context(context, report)
def consider_variables(context, report) # :nodoc:
context.variable_names.each do |name|
next unless is_bad_name?(name)
report << SmellWarning.new(self, context,
"has the variable name '#{name}'")
report << found(context, "has the variable name '#{name}'")
end
end

def consider_name(context, report) # :nodoc:
name = context.name
return false if @accept.include?(context.to_s) # TODO: fq_name() ?
return false unless is_bad_name?(name)
report << SmellWarning.new(self, context,
"has the name '#{name}'")
report << found(context, "has the name '#{name}'")
end

def is_bad_name?(name) # :nodoc:
Expand Down
3 changes: 1 addition & 2 deletions lib/reek/smells/utility_function.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ def examine_context(method, report)
return false if method.calls.keys.length == 0 or
method.num_statements == 0 or
method.depends_on_instance?
report << SmellWarning.new(self, method,
"doesn't depend on instance state")
report << found(method, "doesn't depend on instance state")
end
end
end
Expand Down

0 comments on commit 42abc7e

Please sign in to comment.