Sha256: 08daace60413e673f3ddcf04df631e11568cbc450612c72291379e277facd3e8

Contents?: true

Size: 1.78 KB

Versions: 18

Compression:

Stored size: 1.78 KB

Contents

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

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

      def self.default_config
        super.adopt(EXCLUDE_KEY => ['initialize'])
      end

      def initialize(config = FeatureEnvy.default_config)
        super
      end

      #
      # Checks whether the given +context+ includes any code fragment that
      # might "belong" on another class.
      # Remembers any smells found.
      #
      def examine_context(context)
        context.envious_receivers.each do |ref|
          found(context, "refers to #{SexpFormatter.format(ref)} more than self")
        end
      end
    end
  end
end

Version data entries

18 entries across 18 versions & 2 rubygems

Version Path
kevinrutherford-reek-1.1.3.10 lib/reek/smells/feature_envy.rb
kevinrutherford-reek-1.1.3.11 lib/reek/smells/feature_envy.rb
kevinrutherford-reek-1.1.3.12 lib/reek/smells/feature_envy.rb
kevinrutherford-reek-1.1.3.13 lib/reek/smells/feature_envy.rb
kevinrutherford-reek-1.1.3.14 lib/reek/smells/feature_envy.rb
kevinrutherford-reek-1.1.3.15 lib/reek/smells/feature_envy.rb
kevinrutherford-reek-1.1.3.16 lib/reek/smells/feature_envy.rb
kevinrutherford-reek-1.1.3.3 lib/reek/smells/feature_envy.rb
kevinrutherford-reek-1.1.3.4 lib/reek/smells/feature_envy.rb
kevinrutherford-reek-1.1.3.5 lib/reek/smells/feature_envy.rb
kevinrutherford-reek-1.1.3.6 lib/reek/smells/feature_envy.rb
kevinrutherford-reek-1.1.3.7 lib/reek/smells/feature_envy.rb
kevinrutherford-reek-1.1.3.8 lib/reek/smells/feature_envy.rb
kevinrutherford-reek-1.1.3.9 lib/reek/smells/feature_envy.rb
kevinrutherford-reek-1.2.0 lib/reek/smells/feature_envy.rb
reek-1.2.2 lib/reek/smells/feature_envy.rb
reek-1.2.1 lib/reek/smells/feature_envy.rb
reek-1.2.0 lib/reek/smells/feature_envy.rb