Parent

Class/Module Index [+]

Quicksearch

Reek::Smells::SimulatedPolymorphism

Simulated Polymorphism occurs when

Conditional code is hard to read and understand, because the reader must hold more state in his head. When the same value is tested in multiple places throughout an application, any change to the set of possible values will require many methods and classes to change. Tests for the type of an object may indicate that the abstraction represented by that type is not completely defined (or understood).

In the current implementation, Reek only checks for multiple conditionals testing the same value throughout a single class.

Constants

DEFAULT_MAX_IFS
MAX_IDENTICAL_IFS_KEY

The name of the config field that sets the maximum number of identical conditionals permitted within any single class.

SMELL_CLASS
SMELL_SUBCLASS

Public Class Methods

default_config() click to toggle source
# File lib/reek/smells/simulated_polymorphism.rb, line 39
def self.default_config
  super.adopt(MAX_IDENTICAL_IFS_KEY => DEFAULT_MAX_IFS)
end
new(source, config = SimulatedPolymorphism.default_config) click to toggle source
# File lib/reek/smells/simulated_polymorphism.rb, line 43
def initialize(source, config = SimulatedPolymorphism.default_config)
  super(source, config)
end

Public Instance Methods

conditional_counts(sexp) click to toggle source

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.

# File lib/reek/smells/simulated_polymorphism.rb, line 71
def conditional_counts(sexp)
  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)
  }
  [:if, :case].each {|stmt| sexp.local_nodes(stmt, &collector) }
  result
end
examine_context(ctx) click to toggle source

Checks the given class for multiple identical conditional tests.

@return [Array<SmellWarning>]

# File lib/reek/smells/simulated_polymorphism.rb, line 52
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
    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

[Validate]

Generated with the Darkfish Rdoc Generator 2.