Class: Reek::SmellDetectors::DuplicateMethodCall::CallCollector

Inherits:
Object
  • Object
show all
Defined in:
lib/reek/smell_detectors/duplicate_method_call.rb

Overview

Collects all calls in a given context

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context, max_allowed_calls, allow_calls) ⇒ CallCollector

Returns a new instance of CallCollector.



100
101
102
103
104
# File 'lib/reek/smell_detectors/duplicate_method_call.rb', line 100

def initialize(context, max_allowed_calls, allow_calls)
  @context = context
  @max_allowed_calls = max_allowed_calls
  @allow_calls = allow_calls
end

Instance Attribute Details

#allow_callsObject (readonly, private)

Returns the value of attribute allow_calls.



118
119
120
# File 'lib/reek/smell_detectors/duplicate_method_call.rb', line 118

def allow_calls
  @allow_calls
end

#contextObject (readonly)

Returns the value of attribute context.



98
99
100
# File 'lib/reek/smell_detectors/duplicate_method_call.rb', line 98

def context
  @context
end

#max_allowed_callsObject (readonly, private)

Returns the value of attribute max_allowed_calls.



118
119
120
# File 'lib/reek/smell_detectors/duplicate_method_call.rb', line 118

def max_allowed_calls
  @max_allowed_calls
end

Instance Method Details

#allow_calls?(method) ⇒ Boolean (private)

Returns:

  • (Boolean)


143
144
145
# File 'lib/reek/smell_detectors/duplicate_method_call.rb', line 143

def allow_calls?(method)
  allow_calls.any? { |allow| /#{allow}/ =~ method }
end

#callsObject



106
107
108
109
110
# File 'lib/reek/smell_detectors/duplicate_method_call.rb', line 106

def calls
  result = Hash.new { |hash, key| hash[key] = FoundCall.new(key) }
  collect_calls(result)
  result.values.sort_by(&:call)
end

#collect_calls(result) ⇒ Object (private)



122
123
124
125
126
127
128
129
130
131
132
# File 'lib/reek/smell_detectors/duplicate_method_call.rb', line 122

def collect_calls(result)
  context.local_nodes(:send, [:mlhs]) do |call_node|
    next if call_node.object_creation_call?
    next if simple_method_call? call_node

    result[call_node].record(call_node)
  end
  context.local_nodes(:block) do |call_node|
    result[call_node].record(call_node)
  end
end

#simple_method_call?(call_node) ⇒ Boolean (private)

Returns:

  • (Boolean)


139
140
141
# File 'lib/reek/smell_detectors/duplicate_method_call.rb', line 139

def simple_method_call?(call_node)
  !call_node.receiver && call_node.args.empty?
end

#smelly_call?(found_call) ⇒ Boolean (private)

Returns:

  • (Boolean)


134
135
136
# File 'lib/reek/smell_detectors/duplicate_method_call.rb', line 134

def smelly_call?(found_call)
  found_call.occurs > max_allowed_calls && !allow_calls?(found_call.call)
end

#smelly_callsObject



112
113
114
# File 'lib/reek/smell_detectors/duplicate_method_call.rb', line 112

def smelly_calls
  calls.select { |found_call| smelly_call? found_call }
end