Skip to content

Commit

Permalink
Detectors now collect smells and report them later
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinrutherford committed Jun 14, 2009
1 parent 42abc7e commit 30551ff
Show file tree
Hide file tree
Showing 13 changed files with 29 additions and 15 deletions.
7 changes: 6 additions & 1 deletion lib/reek/report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@ module Reek
class Report
include Enumerable

def initialize # :nodoc:
def initialize(detectors = nil) # :nodoc:
@report = SortedSet.new
if detectors
detectors.each_value do |group|
group.each {|smell| smell.report_on(self)}
end
end
end

#
Expand Down
2 changes: 1 addition & 1 deletion lib/reek/smells/control_couple.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def initialize(config = ControlCouple.default_config)
#
def examine_context(cond, report)
return unless cond.tests_a_parameter?
report << found(cond, "is controlled by argument #{SexpFormatter.format(cond.if_expr)}")
found(cond, "is controlled by argument #{SexpFormatter.format(cond.if_expr)}")
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/reek/smells/duplication.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def initialize(config = Duplication.default_config)

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

Expand Down
2 changes: 1 addition & 1 deletion lib/reek/smells/feature_envy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def initialize(config = FeatureEnvy.default_config)
#
def examine_context(context, report)
context.envious_receivers.each do |ref|
report << found(context, "refers to #{SexpFormatter.format(ref)} more than self")
found(context, "refers to #{SexpFormatter.format(ref)} more than self")
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/reek/smells/large_class.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +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 << found(klass, "has at least #{count} methods")
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 << found(klass, "has at least #{count} instance variables")
found(klass, "has at least #{count} instance variables")
end

#
Expand Down
2 changes: 1 addition & 1 deletion lib/reek/smells/long_method.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def initialize(config = LongMethod.default_config)
def examine_context(method, report)
num = method.num_statements
return false if num <= @max_statements
report << found(method, "has approx #{num} statements")
found(method, "has approx #{num} statements")
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/reek/smells/long_parameter_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def initialize(config)
def examine_context(ctx, report)
num_params = ctx.parameters.length
return false if num_params <= @max_params
report << found(ctx, "#{@action} #{num_params} parameters")
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 << found(block, 'is nested')
found(block, 'is nested')
end
end
end
Expand Down
8 changes: 6 additions & 2 deletions lib/reek/smells/smell_detector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ def initialize(config)
end

def examine(context, report)
before = report.size
before = @smells_found.size
examine_context(context, report) if @enabled and !exception?(context)
report.length > before
@smells_found.size > before
end

def examine_context(context, report)
Expand All @@ -65,6 +65,10 @@ def found(scope, warning)
smell
end

def report_on(report)
@smells_found.each {|smell| report << smell}
end

def smell_name
self.class.name_words.join(' ')
end
Expand Down
4 changes: 2 additions & 2 deletions lib/reek/smells/uncommunicative_name.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +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 << found(context, "has the variable name '#{name}'")
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 << found(context, "has the name '#{name}'")
found(context, "has the name '#{name}'")
end

def is_bad_name?(name) # :nodoc:
Expand Down
2 changes: 1 addition & 1 deletion lib/reek/smells/utility_function.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +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 << found(method, "doesn't depend on instance state")
found(method, "doesn't depend on instance state")
end
end
end
Expand Down
4 changes: 3 additions & 1 deletion lib/reek/source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,10 @@ def generate_syntax_tree
def report
unless @report
@report = Report.new
parser = CodeParser.new(@report, @cf.smell_listeners)
detectors = @cf.smell_listeners
parser = CodeParser.new(@report, detectors)
parser.process(generate_syntax_tree)
@report = Report.new(detectors)
end
@report
end
Expand Down
3 changes: 3 additions & 0 deletions spec/reek/smells/large_class_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,20 +65,23 @@ def method
@config[LargeClass::EXCLUDE_KEY] = ['Humungous']
lc = LargeClass.new(@config)
lc.examine(@ctx, @rpt).should == false
lc.report_on(@rpt)
@rpt.length.should == 0
end

it 'should ignore second excepted name' do
@config[LargeClass::EXCLUDE_KEY] = ['Oversized', 'Humungous']
lc = LargeClass.new(@config)
lc.examine(@ctx, @rpt).should == false
lc.report_on(@rpt)
@rpt.length.should == 0
end

it 'should report non-excepted name' do
@config[LargeClass::EXCLUDE_KEY] = ['SmellMe']
lc = LargeClass.new(@config)
lc.examine(@ctx, @rpt).should == true
lc.report_on(@rpt)
@rpt.length.should == 1
end
end
Expand Down

0 comments on commit 30551ff

Please sign in to comment.