Class: Reek::Smells::FeatureEnvy Private

Inherits:
SmellDetector show all
Defined in:
lib/reek/smells/feature_envy.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

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.

If the method doesn’t reference self at all, UtilityFunction is reported instead.

See Feature-Envy for details.

Constant Summary

Constants inherited from SmellDetector

SmellDetector::DEFAULT_EXCLUDE_SET, SmellDetector::EXCLUDE_KEY

Instance Attribute Summary

Attributes inherited from SmellDetector

#smells_found, #source

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from SmellDetector

#config_for, #configure_with, contexts, default_config, default_smell_category, descendants, #enabled?, #enabled_for?, #examine, #exception?, #initialize, #register, #report_on, #smell_category, smell_type, #smell_type, #value

Constructor Details

This class inherits a constructor from Reek::Smells::SmellDetector

Class Method Details

.smell_categoryObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



39
40
41
# File 'lib/reek/smells/feature_envy.rb', line 39

def self.smell_category
  'LowCohesion'
end

Instance Method Details

#examine_context(method_ctx) ⇒ Array<SmellWarning>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Checks whether the given context includes any code fragment that might “belong” on another class.

Returns:



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/reek/smells/feature_envy.rb', line 49

def examine_context(method_ctx)
  return [] unless method_ctx.references_self?
  method_ctx.envious_receivers.map do |ref, occurs|
    target = ref.to_s
    SmellWarning.new self,
                     context: method_ctx.full_name,
                     lines: [method_ctx.exp.line],
                     message: "refers to #{target} more than self",
                     parameters: { name: target, count: occurs }
  end
end