Class: Reek::SmellDetectors::DuplicateMethodCall

Inherits:
BaseDetector
  • Object
show all
Defined in:
lib/reek/smell_detectors/duplicate_method_call.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.

DuplicateMethodCall 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

See Duplicate-Method-Call for details.

Defined Under Namespace

Classes: CallCollector, FoundCall

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'
DEFAULT_MAX_CALLS =
1
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.

'allow_calls'
DEFAULT_ALLOW_CALLS =
[].freeze

Constants inherited from BaseDetector

BaseDetector::DEFAULT_EXCLUDE_SET, BaseDetector::EXCLUDE_KEY

Instance Attribute Summary

Attributes inherited from BaseDetector

#config, #context

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseDetector

#config_for, configuration_keys, contexts, descendants, #enabled?, #exception?, #expression, inherited, #initialize, #run, #smell_type, smell_type, #smell_warning, #source_line, to_detector, todo_configuration_for, #value

Constructor Details

This class inherits a constructor from Reek::SmellDetectors::BaseDetector

Class Method Details

.default_configObject



33
34
35
36
37
# File 'lib/reek/smell_detectors/duplicate_method_call.rb', line 33

def self.default_config
  super.merge(
    MAX_ALLOWED_CALLS_KEY => DEFAULT_MAX_CALLS,
    ALLOW_CALLS_KEY => DEFAULT_ALLOW_CALLS)
end

Instance Method Details

#allow_callsObject (private)



62
63
64
# File 'lib/reek/smell_detectors/duplicate_method_call.rb', line 62

def allow_calls
  value(ALLOW_CALLS_KEY, context)
end

#max_allowed_callsObject (private)



58
59
60
# File 'lib/reek/smell_detectors/duplicate_method_call.rb', line 58

def max_allowed_calls
  value(MAX_ALLOWED_CALLS_KEY, context)
end

#sniffArray<SmellWarning>

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

Returns:



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/reek/smell_detectors/duplicate_method_call.rb', line 44

def sniff
  collector = CallCollector.new(context, max_allowed_calls, allow_calls)
  collector.smelly_calls.map do |found_call|
    call = found_call.call
    occurs = found_call.occurs
    smell_warning(
      lines: found_call.lines,
      message: "calls '#{call}' #{occurs} times",
      parameters: { name: call, count: occurs })
  end
end