Class: Reek::SmellDetectors::FeatureEnvy

Inherits:
BaseDetector show all
Defined in:
lib/reek/smell_detectors/feature_envy.rb

Overview

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 BaseDetector

BaseDetector::DEFAULT_EXCLUDE_SET, BaseDetector::EXCLUDE_KEY

Instance Attribute Summary

Attributes inherited from BaseDetector

#config, #context

Instance Method Summary collapse

Methods inherited from BaseDetector

#config_for, configuration_keys, contexts, default_config, descendants, #enabled?, #exception?, #expression, inherited, #initialize, #run, #smell_type, smell_type, #smell_warning, #source_line, to_detector, todo_configuration_for, #value

Constructor Details

This class inherits a constructor from Reek::SmellDetectors::BaseDetector

Instance Method Details

#envious_receiversObject (private)



63
64
65
66
67
# File 'lib/reek/smell_detectors/feature_envy.rb', line 63

def envious_receivers
  return {} if refs.self_is_max?

  refs.most_popular
end

#refsObject (private)



59
60
61
# File 'lib/reek/smell_detectors/feature_envy.rb', line 59

def refs
  @refs ||= context.refs
end

#sniffArray<SmellWarning>

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

Returns:



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/reek/smell_detectors/feature_envy.rb', line 45

def sniff
  return [] if context.singleton_method? || context.module_function?
  return [] unless context.references_self?

  envious_receivers.map do |name, lines|
    smell_warning(
      lines: lines,
      message: "refers to '#{name}' more than self (maybe move it to another class?)",
      parameters: { name: name.to_s })
  end
end