Class: Reek::Smells::Duplication

Inherits:
SmellDetector show all
Defined in:
lib/reek/smells/duplication.rb

Overview

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

Constant Summary collapse

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.

'max_calls'

Constants inherited from SmellDetector

SmellDetector::ENABLED_KEY, SmellDetector::EXCLUDE_KEY

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from SmellDetector

class_name, contexts, #examine, #exception?, listen, #smell_name

Constructor Details

#initialize(config = Duplication.default_config) ⇒ Duplication

Returns a new instance of Duplication.



31
32
33
34
# File 'lib/reek/smells/duplication.rb', line 31

def initialize(config = Duplication.default_config)
  super
  @max_calls = config[MAX_ALLOWED_CALLS_KEY]
end

Class Method Details

.default_configObject



27
28
29
# File 'lib/reek/smells/duplication.rb', line 27

def self.default_config
  super.adopt(MAX_ALLOWED_CALLS_KEY => 1)
end

Instance Method Details

#examine_context(method, report) ⇒ Object



36
37
38
39
40
41
# File 'lib/reek/smells/duplication.rb', line 36

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

#smelly_calls(method) ⇒ Object

:nodoc:



43
44
45
46
47
# File 'lib/reek/smells/duplication.rb', line 43

def smelly_calls(method)   # :nodoc:
  method.calls.select do |key,val|
    val > @max_calls and key[2] != :new
  end.map { |call_exp| call_exp[0] }
end