Class: Reek::MethodContext

Inherits:
CodeContext show all
Defined in:
lib/reek/method_context.rb

Direct Known Subclasses

SingletonMethodContext

Instance Attribute Summary collapse

Attributes inherited from CodeContext

#name

Instance Method Summary collapse

Methods inherited from CodeContext

#matches?, #method_missing, #num_methods

Constructor Details

#initialize(outer, exp, record = true) ⇒ MethodContext

Returns a new instance of MethodContext.



60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/reek/method_context.rb', line 60

def initialize(outer, exp, record = true)
  super(outer, exp)
  @parameters = exp[exp[0] == :defn ? 2 : 3]  # SMELL: SimulatedPolymorphism
  @parameters ||= []
  @parameters.extend(MethodParameters)
  @local_variables = []
  @name = Name.new(exp[1])
  @num_statements = 0
  @calls = Hash.new(0)
  @depends_on_self = false
  @refs = ObjectRefs.new
  @outer.record_method(self)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Reek::CodeContext

Instance Attribute Details

#callsObject (readonly)

Returns the value of attribute calls.



56
57
58
# File 'lib/reek/method_context.rb', line 56

def calls
  @calls
end

#num_statementsObject (readonly)

Returns the value of attribute num_statements.



58
59
60
# File 'lib/reek/method_context.rb', line 58

def num_statements
  @num_statements
end

#parametersObject (readonly)

Returns the value of attribute parameters.



55
56
57
# File 'lib/reek/method_context.rb', line 55

def parameters
  @parameters
end

#refsObject (readonly)

Returns the value of attribute refs.



57
58
59
# File 'lib/reek/method_context.rb', line 57

def refs
  @refs
end

Instance Method Details

#count_statements(num) ⇒ Object



74
75
76
# File 'lib/reek/method_context.rb', line 74

def count_statements(num)
  @num_statements += num
end

#depends_on_instance?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/reek/method_context.rb', line 78

def depends_on_instance?
  @depends_on_self || is_overriding_method?(@name)
end

#envious_receiversObject



124
125
126
127
# File 'lib/reek/method_context.rb', line 124

def envious_receivers
  return [] if @refs.self_is_max?
  @refs.max_keys
end

#has_parameter(sym) ⇒ Object



82
83
84
# File 'lib/reek/method_context.rb', line 82

def has_parameter(sym)
  @parameters.include?(sym.to_s)
end

#outer_nameObject



116
117
118
# File 'lib/reek/method_context.rb', line 116

def outer_name
  "#{@outer.outer_name}#{@name}/"
end

#record_call_to(exp) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
# File 'lib/reek/method_context.rb', line 86

def record_call_to(exp)
  @calls[exp] += 1
  receiver, meth = exp[1..2]
  receiver ||= [:self]
  case receiver[0]
  when :lvar
    @refs.record_ref(receiver) unless meth == :new
  when :self
    record_use_of_self
  end
end

#record_depends_on_selfObject



108
109
110
# File 'lib/reek/method_context.rb', line 108

def record_depends_on_self
  @depends_on_self = true
end

#record_instance_variable(sym) ⇒ Object



103
104
105
106
# File 'lib/reek/method_context.rb', line 103

def record_instance_variable(sym)
  record_use_of_self
  @outer.record_instance_variable(sym)
end

#record_local_variable(sym) ⇒ Object



112
113
114
# File 'lib/reek/method_context.rb', line 112

def record_local_variable(sym)
  @local_variables << Name.new(sym)
end

#record_use_of_selfObject



98
99
100
101
# File 'lib/reek/method_context.rb', line 98

def record_use_of_self
  record_depends_on_self
  @refs.record_reference_to_self
end

#to_sObject



120
121
122
# File 'lib/reek/method_context.rb', line 120

def to_s
  "#{@outer.outer_name}#{@name}"
end

#variable_namesObject



129
130
131
# File 'lib/reek/method_context.rb', line 129

def variable_names
  @parameters.names + @local_variables
end