Class: Reek::SmellDetectors::DuplicateMethodCall::CallCollector
- Inherits:
-
Object
- Object
- Reek::SmellDetectors::DuplicateMethodCall::CallCollector
- Defined in:
- lib/reek/smell_detectors/duplicate_method_call.rb
Overview
Collects all calls in a given context
Instance Attribute Summary collapse
-
#allow_calls ⇒ Object
readonly
private
Returns the value of attribute allow_calls.
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#max_allowed_calls ⇒ Object
readonly
private
Returns the value of attribute max_allowed_calls.
Instance Method Summary collapse
- #allow_calls?(method) ⇒ Boolean private
- #calls ⇒ Object
- #collect_calls(result) ⇒ Object private
-
#initialize(context, max_allowed_calls, allow_calls) ⇒ CallCollector
constructor
A new instance of CallCollector.
- #simple_method_call?(call_node) ⇒ Boolean private
- #smelly_call?(found_call) ⇒ Boolean private
- #smelly_calls ⇒ Object
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_calls ⇒ Object (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 |
#context ⇒ Object (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_calls ⇒ Object (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)
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 |
#calls ⇒ Object
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)
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)
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_calls ⇒ Object
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 |