lib/reek/smells/duplicate_method_call.rb in reek-1.3.8 vs lib/reek/smells/duplicate_method_call.rb in reek-1.4.0
- old
+ new
@@ -1,27 +1,26 @@
require 'reek/smells/smell_detector'
require 'reek/smell_warning'
module Reek
module Smells
-
#
# 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
#
class DuplicateMethodCall < SmellDetector
SMELL_CLASS = 'Duplication'
- SMELL_SUBCLASS = self.name.split(/::/)[-1]
+ SMELL_SUBCLASS = name.split(/::/)[-1]
CALL_KEY = 'call'
OCCURRENCES_KEY = 'occurrences'
# The name of the config field that sets the maximum number of
@@ -55,11 +54,11 @@
CallCollector.new(ctx, max_allowed_calls, allow_calls).smelly_calls.map do |found_call|
SmellWarning.new(SMELL_CLASS, ctx.full_name, found_call.lines,
found_call.smell_message,
@source, SMELL_SUBCLASS,
- {CALL_KEY => found_call.call, OCCURRENCES_KEY => found_call.occurs})
+ CALL_KEY => found_call.call, OCCURRENCES_KEY => found_call.occurs)
end
end
# Collects information about a single found call
class FoundCall
@@ -84,11 +83,11 @@
def occurs
@occurences.length
end
def lines
- @occurences.map {|exp| exp.line}
+ @occurences.map(&:line)
end
end
# Collects all calls in a given context
class CallCollector
@@ -99,18 +98,18 @@
@max_allowed_calls = max_allowed_calls
@allow_calls = allow_calls
end
def calls
- result = Hash.new {|hash,key| hash[key] = FoundCall.new(key)}
+ result = Hash.new { |hash, key| hash[key] = FoundCall.new(key) }
collect_calls(result)
collect_assignments(result)
- result.values.sort_by {|found_call| found_call.call}
+ result.values.sort_by(&:call)
end
def smelly_calls
- calls.select {|found_call| smelly_call? found_call }
+ calls.select { |found_call| smelly_call? found_call }
end
private
def collect_assignments(result)
@@ -123,17 +122,20 @@
context.local_nodes(:call) do |call_node|
next if call_node.method_name == :new
next if !call_node.receiver && call_node.args.empty?
result[call_node].record(call_node)
end
+ context.local_nodes(:iter) do |call_node|
+ result[call_node].record(call_node)
+ end
end
def smelly_call?(found_call)
- found_call.occurs > @max_allowed_calls and not allow_calls?(found_call.call)
+ found_call.occurs > @max_allowed_calls && !allow_calls?(found_call.call)
end
def allow_calls?(method)
- @allow_calls.any? { |allow| /#{allow}/ === method }
+ @allow_calls.any? { |allow| /#{allow}/ =~ method }
end
end
end
end
end