Class: Reek::Smells::DuplicateMethodCall Private

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

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

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 =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

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 =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

1
ALLOW_CALLS_KEY =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

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 =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

[]

Constants inherited from SmellDetector

SmellDetector::DEFAULT_EXCLUDE_SET, SmellDetector::EXCLUDE_KEY

Instance Attribute Summary

Attributes inherited from SmellDetector

#smells_found, #source

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from SmellDetector

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

Constructor Details

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

Class Method Details

.default_configObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



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

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

.smell_categoryObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



33
34
35
# File 'lib/reek/smells/duplicate_method_call.rb', line 33

def self.smell_category
  'Duplication'
end

Instance Method Details

#examine_context(ctx) ⇒ Array<SmellWarning>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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

Returns:



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

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