lib/reek/smells.rb in reek-0.1.0 vs lib/reek/smells.rb in reek-0.1.1

- old
+ new

@@ -3,10 +3,12 @@ require 'reek/printer' module Reek class Smell + include Comparable + def self.convert_camel_case(class_name) class_name.gsub(/([a-z])([A-Z])/) { |s| "#{$1} #{$2}"} end def initialize(context, arg=nil) @@ -16,13 +18,21 @@ def self.check(exp, context, arg=nil) smell = new(context, arg) context.report(smell) if smell.recognise?(exp) end - def ==(other) - self.report == other.report + def hash # :nodoc: + report.hash end + + def <=>(other) # :nodoc: + self.report <=> other.report + end + + def eql?(other) # :nodoc: + self.report.eql?(other.report) + end def name self.class.convert_camel_case(self.class.name.split(/::/)[1]) end @@ -60,53 +70,30 @@ end class LongMethod < Smell MAX_ALLOWED = 5 - def count_statements(exp) - result = exp.length - 1 - result -= 1 if Array === exp[1] and exp[1][0] == :args - result + def recognise?(num_stmts) + @num_stmts = num_stmts + num_stmts > MAX_ALLOWED end - def recognise?(exp) - count_statements(exp) > MAX_ALLOWED - end - def detailed_report - "#{@context} has > #{MAX_ALLOWED} statements" + "#{@context} has approx #{@num_stmts} statements" end end - class LongBlock < Smell - MAX_ALLOWED = 5 - - def count_statements(exp) - result = exp.length - 1 - result -= 1 if Array === exp[1] and exp[1][0] == :args - result - end - - def recognise?(exp) - count_statements(exp) > MAX_ALLOWED - end - - def detailed_report - "#{@context} has a block with > #{MAX_ALLOWED} statements" - end - end - class FeatureEnvy < Smell def initialize(context, receiver) super @receiver = receiver end def recognise?(calls) max = calls.empty? ? 0 : calls.values.max return false unless max > calls[:self] receivers = calls.keys.select { |key| calls[key] == max } - @receiver = receivers.map {|r| Printer.print(r)}.sort.join(' or ') + @receiver = receivers.map {|r| Printer.print(r)}.sort.join(' and ') return true end def detailed_report "#{@context} uses #{@receiver} more than self"