Sha256: 123e4bb1b845c9da1420cfec6469c3c6e5511e9c2b6580e88967760a1d685867

Contents?: true

Size: 1.48 KB

Versions: 11

Compression:

Stored size: 1.48 KB

Contents

require 'reek/smells/smell_detector'
require 'reek/smell_warning'
require 'reek/sexp_formatter'

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.
    # 
    # Currently +Duplication+ 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 Duplication < SmellDetector

      # The name of the config field that sets the maximum number of
      # identical calls to be permitted within any single method.
      MAX_ALLOWED_CALLS_KEY = 'max_calls'

      DEFAULT_MAX_CALLS = 1

      def self.default_config
        super.adopt(MAX_ALLOWED_CALLS_KEY => DEFAULT_MAX_CALLS)
      end

      def initialize(config = Duplication.default_config)
        super(config)
      end

      def examine_context(method)
        smelly_calls(method).each do |call_data|
          num = call_data[1]
          multiple = num == 2 ? 'twice' : "#{num} times"
          found(method, "calls #{SexpFormatter.format(call_data[0])} #{multiple}")
        end
      end
      
      def smelly_calls(method)   # :nodoc:
        method.calls.select do |key,val|
          val > value(MAX_ALLOWED_CALLS_KEY, method, DEFAULT_MAX_CALLS) and key[2] != :new
        end
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 2 rubygems

Version Path
kevinrutherford-reek-1.1.3.14 lib/reek/smells/duplication.rb
kevinrutherford-reek-1.1.3.15 lib/reek/smells/duplication.rb
kevinrutherford-reek-1.1.3.16 lib/reek/smells/duplication.rb
kevinrutherford-reek-1.2.0 lib/reek/smells/duplication.rb
reek-1.2.6 lib/reek/smells/duplication.rb
reek-1.2.5 lib/reek/smells/duplication.rb
reek-1.2.4 lib/reek/smells/duplication.rb
reek-1.2.3 lib/reek/smells/duplication.rb
reek-1.2.2 lib/reek/smells/duplication.rb
reek-1.2.1 lib/reek/smells/duplication.rb
reek-1.2.0 lib/reek/smells/duplication.rb