Parent

Class/Module Index [+]

Quicksearch

Reek::Smells::Duplication

Duplication occurs when two fragments of code look nearly identical, or when two fragments of code have nearly identical effects at some conceptual level.

Currently Duplication checks for repeated identical method calls within any one method definition. For example, the following method will report a warning:

def double_thing()
  @other.thing + @other.thing
end

Constants

ALLOW_CALLS_KEY

The name of the config field that sets the names of any methods for which identical calls should be to be permitted within any single method.

CALL_KEY
DEFAULT_ALLOW_CALLS
DEFAULT_MAX_CALLS
MAX_ALLOWED_CALLS_KEY

The name of the config field that sets the maximum number of identical calls to be permitted within any single method.

OCCURRENCES_KEY
SMELL_CLASS
SMELL_SUBCLASS

Public Class Methods

default_config() click to toggle source
# File lib/reek/smells/duplication.rb, line 40
def self.default_config
  super.adopt(
    MAX_ALLOWED_CALLS_KEY => DEFAULT_MAX_CALLS,
    ALLOW_CALLS_KEY => DEFAULT_ALLOW_CALLS
  )
end
new(source, config = Duplication.default_config) click to toggle source
# File lib/reek/smells/duplication.rb, line 47
def initialize(source, config = Duplication.default_config)
  super(source, config)
end

Public Instance Methods

examine_context(ctx) click to toggle source

Looks for duplicate calls within the body of the method ctx.

@return [Array<SmellWarning>]

# File lib/reek/smells/duplication.rb, line 56
def examine_context(ctx)
  @max_allowed_calls = value(MAX_ALLOWED_CALLS_KEY, ctx, DEFAULT_MAX_CALLS)
  @allow_calls = value(ALLOW_CALLS_KEY, ctx, DEFAULT_ALLOW_CALLS)
  calls(ctx).select do |call_exp, copies|
    copies.length > @max_allowed_calls and not allow_calls?(call_exp.format_ruby)
  end.map do |call_exp, copies|
    occurs = copies.length
    call = call_exp.format_ruby
    multiple = occurs == 2 ? 'twice' : "#{occurs} times"
    smell = SmellWarning.new(SMELL_CLASS, ctx.full_name, copies.map {|exp| exp.line},
      "calls #{call} #{multiple}",
      @source, SMELL_SUBCLASS,
      {CALL_KEY => call, OCCURRENCES_KEY => occurs})
    smell
  end
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.