forked from troessner/reek
-
Notifications
You must be signed in to change notification settings - Fork 0
/
feature_envy.rb
57 lines (52 loc) · 1.78 KB
/
feature_envy.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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