lib/reek/method_context.rb in reek-1.1.3 vs lib/reek/method_context.rb in reek-1.2.0

- old
+ new

@@ -1,26 +1,76 @@ require 'reek/name' require 'reek/code_context' require 'reek/object_refs' +class Array + def power_set + self.inject([[]]) { |cum, element| cum.cross(element) } + end + + def bounded_power_set(lower_bound) + power_set.select {|ps| ps.length > lower_bound} + end + + def cross(element) + result = [] + self.each do |set| + result << set + result << (set + [element]) + end + result + end + + def intersection + self.inject { |res, elem| elem & res } + end +end + module Reek + + module MethodParameters + def self.is_arg?(param) + return false if (Array === param and param[0] == :block) + return !(param.to_s =~ /^\&/) + end + + def names + return @names if @names + @names = self[1..-1].select {|arg| MethodParameters.is_arg?(arg)}.map {|arg| Name.new(arg)} + end + + def length + names.length + end + + def include?(name) + names.include?(name) + end + + def to_s + "[#{names.map{|nm| nm.to_s}.join(', ')}]" + end + end + class MethodContext < CodeContext attr_reader :parameters attr_reader :calls attr_reader :refs attr_reader :num_statements def initialize(outer, exp, record = true) super(outer, exp) - @parameters = [] + @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(@name) # TODO: should be children of outer? + @outer.record_method(self) end def count_statements(num) @num_statements += num end @@ -61,18 +111,10 @@ def record_local_variable(sym) @local_variables << Name.new(sym) end - def self.is_block_arg?(param) - (Array === param and param[0] == :block) or (param.to_s =~ /^\&/) - end - - def record_parameter(param) - @parameters << Name.new(param) unless MethodContext.is_block_arg?(param) - end - def outer_name "#{@outer.outer_name}#{@name}/" end def to_s @@ -83,9 +125,9 @@ return [] if @refs.self_is_max? @refs.max_keys end def variable_names - @parameters + @local_variables + @parameters.names + @local_variables end end end