Sha256: 1cfd93cc03a3f7ddaed4f4303fb8f271d1682f4322e773952daa5e406ea7ad77

Contents?: true

Size: 1.43 KB

Versions: 15

Compression:

Stored size: 1.43 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'

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

      def initialize(config = Duplication.default_config)
        super
        @max_calls = config[MAX_ALLOWED_CALLS_KEY]
      end

      def examine_context(method, report)
        smelly_calls(method).each do |call|
          report << SmellWarning.new(self, method,
                      "calls #{SexpFormatter.format(call)} multiple times")
        end
      end
      
      def smelly_calls(method)   # :nodoc:
        method.calls.select do |key,val|
          val > @max_calls and key[2] != :new
        end.map { |call_exp| call_exp[0] }
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 3 rubygems

Version Path
kevinrutherford-reek-0.3.1.4 lib/reek/smells/duplication.rb
kevinrutherford-reek-0.3.1.5 lib/reek/smells/duplication.rb
kevinrutherford-reek-0.3.1.6 lib/reek/smells/duplication.rb
kevinrutherford-reek-1.0.0 lib/reek/smells/duplication.rb
kevinrutherford-reek-1.0.1 lib/reek/smells/duplication.rb
kevinrutherford-reek-1.1.1 lib/reek/smells/duplication.rb
kevinrutherford-reek-1.1.2.1 lib/reek/smells/duplication.rb
kevinrutherford-reek-1.1.2 lib/reek/smells/duplication.rb
kevinrutherford-reek-1.1.3.1 lib/reek/smells/duplication.rb
kevinrutherford-reek-1.1.3.2 lib/reek/smells/duplication.rb
kevinrutherford-reek-1.1.3 lib/reek/smells/duplication.rb
teksymmetry-reek-1.1.3.1 lib/reek/smells/duplication.rb
teksymmetry-reek-1.1.3.2 lib/reek/smells/duplication.rb
reek-1.1.3 lib/reek/smells/duplication.rb
reek-1.0.0 lib/reek/smells/duplication.rb