Class: Reek::Smells::DuplicateMethodCall

Inherits:
SmellDetector show all
Defined in:
lib/reek/smells/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

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 =
[]

Constants inherited from SmellDetector

SmellDetector::DEFAULT_EXCLUDE_SET, SmellDetector::EXCLUDE_KEY

Instance Attribute Summary

Attributes inherited from SmellDetector

#smell_category, #smell_type, #smells_found, #source

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from SmellDetector

#config_for, #configure_with, contexts, default_smell_category, #enabled?, #enabled_for?, #examine, #exception?, #initialize, #register, #report_on, smell_type, #value

Constructor Details

This class inherits a constructor from Reek::Smells::SmellDetector

Class Method Details

.default_configObject


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

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

.smell_categoryObject


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

def self.smell_category
  'Duplication'
end

Instance Method Details

#examine_context(ctx) ⇒ Array<SmellWarning>

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


47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/reek/smells/duplicate_method_call.rb', line 47

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)

  collector = CallCollector.new(ctx, max_allowed_calls, allow_calls)
  collector.smelly_calls.map do |found_call|
    SmellWarning.new self,
                     context: ctx.full_name,
                     lines: found_call.lines,
                     message: "calls #{found_call.call} #{found_call.occurs} times",
                     parameters: { name: found_call.call, count: found_call.occurs }
  end
end