lib/reek/smells/simulated_polymorphism.rb in reek-1.2.7.3 vs lib/reek/smells/simulated_polymorphism.rb in reek-1.2.8

- old
+ new

@@ -21,10 +21,13 @@ # In the current implementation, Reek only checks for multiple conditionals # testing the same value throughout a single class. # class SimulatedPolymorphism < SmellDetector + SMELL_CLASS = self.name.split(/::/)[-1] + SMELL_SUBCLASS = 'RepeatedConditional' + def self.contexts # :nodoc: [:class] end # The name of the config field that sets the maximum number of @@ -41,28 +44,33 @@ super(source, config) end # # Checks the given class for multiple identical conditional tests. - # Remembers any smells found. # - def examine_context(klass) - conditional_counts(klass).each do |key, lines| + # @return [Array<SmellWarning>] + # + def examine_context(ctx) + @max_identical_ifs = value(MAX_IDENTICAL_IFS_KEY, ctx, DEFAULT_MAX_IFS) + conditional_counts(ctx).select do |key, lines| + lines.length > @max_identical_ifs + end.map do |key, lines| occurs = lines.length - next unless occurs > value(MAX_IDENTICAL_IFS_KEY, klass, DEFAULT_MAX_IFS) - expr = key.format - found(klass, "tests #{expr} at least #{occurs} times", - 'RepeatedConditional', {'expression' => expr, 'occurrences' => occurs}, lines) + expr = key.format_ruby + SmellWarning.new(SMELL_CLASS, ctx.full_name, lines, + "tests #{expr} at least #{occurs} times", + @source, SMELL_SUBCLASS, + {'expression' => expr, 'occurrences' => occurs}) end end # # Returns a Hash listing all of the conditional expressions in # the given syntax tree together with the number of times each # occurs. Ignores nested classes and modules. # def conditional_counts(sexp) - result = Hash.new {|hash,key| hash[key] = []} + result = Hash.new {|hash, key| hash[key] = []} collector = proc { |node| condition = node.condition next if condition.nil? or condition == s(:call, nil, :block_given?, s(:arglist)) result[condition].push(condition.line) }