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

Class Method Summary collapse

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.



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/reek/method_context.rb', line 12

def initialize(outer, exp, record = true)
  super(outer, exp)
  @parameters = []
  @local_variables = []
  @name = Name.new(exp[1])
  @num_statements = 0
  @calls = Hash.new(0)
  @depends_on_self = false
  @refs = ObjectRefs.new
  @outer.record_method(@name)    # TODO: should be children of outer?
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.



8
9
10
# File 'lib/reek/method_context.rb', line 8

def calls
  @calls
end

#num_statementsObject (readonly)

Returns the value of attribute num_statements.



10
11
12
# File 'lib/reek/method_context.rb', line 10

def num_statements
  @num_statements
end

#parametersObject (readonly)

Returns the value of attribute parameters.



7
8
9
# File 'lib/reek/method_context.rb', line 7

def parameters
  @parameters
end

#refsObject (readonly)

Returns the value of attribute refs.



9
10
11
# File 'lib/reek/method_context.rb', line 9

def refs
  @refs
end

Class Method Details

.is_block_arg?(param) ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/reek/method_context.rb', line 66

def self.is_block_arg?(param)
  (Array === param and param[0] == :block) or (param.to_s =~ /^\&/)
end

Instance Method Details

#count_statements(num) ⇒ Object



24
25
26
# File 'lib/reek/method_context.rb', line 24

def count_statements(num)
  @num_statements += num
end

#depends_on_instance?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/reek/method_context.rb', line 28

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

#envious_receiversObject



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

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

#has_parameter(sym) ⇒ Object



32
33
34
# File 'lib/reek/method_context.rb', line 32

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

#outer_nameObject



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

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

#record_call_to(exp) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/reek/method_context.rb', line 36

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



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

def record_depends_on_self
  @depends_on_self = true
end

#record_instance_variable(sym) ⇒ Object



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

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

#record_local_variable(sym) ⇒ Object



62
63
64
# File 'lib/reek/method_context.rb', line 62

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

#record_parameter(param) ⇒ Object



70
71
72
# File 'lib/reek/method_context.rb', line 70

def record_parameter(param)
  @parameters << Name.new(param) unless MethodContext.is_block_arg?(param)
end

#record_use_of_selfObject



48
49
50
51
# File 'lib/reek/method_context.rb', line 48

def record_use_of_self
  record_depends_on_self
  @refs.record_reference_to_self
end

#to_sObject



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

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

#variable_namesObject



87
88
89
# File 'lib/reek/method_context.rb', line 87

def variable_names
  @parameters + @local_variables
end