Sha256: fe318d97872f535d9179ed089623cce8821e99a51856672945292aa2c4161ac2

Contents?: true

Size: 1.98 KB

Versions: 1

Compression:

Stored size: 1.98 KB

Contents

$:.unshift File.dirname(__FILE__)

require 'reek/smells/smell'

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 < Smell

      #
      # Checks whether the given +method+ includes any code fragment that
      # might "belong" on another class.
      # Any smells found are added to the +report+; returns true in that case,
      # and false otherwise.
      #
      def self.examine(method, report)
        return false if method.name == 'initialize'
        return false if method.refs.self_is_max?
        smell_found = false
        method.refs.max_keys.each do |r|
          report << new(method, Printer.print(r))
          smell_found = true
        end
        smell_found
      end
      
      def initialize(context, receiver)
        super(context)
        @receiver = receiver
      end

      def detailed_report
        "#{@context} refers to #{@receiver} more than self"
      end
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
reek-0.3.1 lib/reek/smells/feature_envy.rb