Sha256: 4a91cb3900fb908a0f9f296c0ff8146f25cb75245b5fe17cf6e8f4e3a0fc2dd1

Contents?: true

Size: 1.98 KB

Versions: 7

Compression:

Stored size: 1.98 KB

Contents

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

module Reek
  module Smells

    #
    # Feature Envy occurs when a code fragment references another object
    # more often than it references itself, or when several clients do
    # the same series of manipulations on a particular type of object.
    # 
    # A simple example would be the following method, which "belongs"
    # on the Item class and not on the Cart class:
    # 
    #  class Cart
    #    def price
    #      @item.price + @item.tax
    #    end
    #  end
    #
    # Feature Envy reduces the code's ability to communicate intent:
    # code that "belongs" on one class but which is located in another
    # can be hard to find, and may upset the "System of Names"
    # in the host class.
    # 
    # Feature Envy also affects the design's flexibility: A code fragment
    # that is in the wrong class creates couplings that may not be natural
    # within the application's domain, and creates a loss of cohesion
    # in the unwilling host class.
    # 
    # Currently +FeatureEnvy+ reports any method that refers to self less
    # often than it refers to (ie. send messages to) some other object.
    #
    class FeatureEnvy < SmellDetector
      include ExcludeInitialize

      SMELL_CLASS = 'LowCohesion'
      SMELL_SUBCLASS = self.name.split(/::/)[-1]

      RECEIVER_KEY = 'receiver'
      REFERENCES_KEY = 'references'

      #
      # Checks whether the given +context+ includes any code fragment that
      # might "belong" on another class.
      #
      # @return [Array<SmellWarning>]
      #
      def examine_context(method_ctx)
        method_ctx.envious_receivers.map do |ref, occurs|
          target = ref.format_ruby
          SmellWarning.new(SMELL_CLASS, method_ctx.full_name, [method_ctx.exp.line],
                           "refers to #{target} more than self",
                           @source, SMELL_SUBCLASS, {RECEIVER_KEY => target, REFERENCES_KEY => occurs})
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
reek-1.3.8 lib/reek/smells/feature_envy.rb
reek-1.3.7 lib/reek/smells/feature_envy.rb
reek-1.3.6 lib/reek/smells/feature_envy.rb
reek-1.3.5 lib/reek/smells/feature_envy.rb
reek-1.3.4 lib/reek/smells/feature_envy.rb
reek-1.3.3 lib/reek/smells/feature_envy.rb
reek-1.3.2 lib/reek/smells/feature_envy.rb