class Brakeman::Constants

Public Class Methods

constant_as_array(exp) click to toggle source
# File lib/brakeman/tracker/constants.rb, line 132
def self.constant_as_array exp
  res = []
  while exp
    if exp.is_a? Sexp
      case exp.node_type
      when :const
        res << exp.value
        exp = nil
      when :colon3
        res << exp.value << :""
        exp = nil
      when :colon2
        res << exp.last
        exp = exp[1]
      else
        res << exp
        exp = nil
      end
    else
      res << exp
      exp = nil
    end
  end

  res.reverse!
  res
end
get_constant_base_name(exp) click to toggle source
# File lib/brakeman/tracker/constants.rb, line 160
def self.get_constant_base_name exp
  return exp unless exp.is_a? Sexp

  case exp.node_type
  when :const, :colon3
    exp.value
  when :colon2
    exp.last
  else
    exp
  end
end
new() click to toggle source
# File lib/brakeman/tracker/constants.rb, line 54
def initialize
  @constants = {}
end

Public Instance Methods

[](exp) click to toggle source
# File lib/brakeman/tracker/constants.rb, line 62
def [] exp
  return unless constant? exp
  match = find_constant exp

  if match
    match.value
  else
    nil
  end
end
add(name, value, context = nil) click to toggle source
# File lib/brakeman/tracker/constants.rb, line 103
def add name, value, context = nil
  if call? value and value.method == :freeze
    value = value.target
  end

  base_name = Constants.get_constant_base_name(name)
  @constants[base_name] ||= []
  @constants[base_name] << Constant.new(name, value, context)
end
each() { |constant| ... } click to toggle source
# File lib/brakeman/tracker/constants.rb, line 124
def each
  @constants.each do |name, values|
    values.each do |constant|
      yield constant
    end
  end
end
find_all(exp) click to toggle source
# File lib/brakeman/tracker/constants.rb, line 98
def find_all exp
  base_name = Constants.get_constant_base_name(exp)
  @constants[base_name]
end
find_constant(exp) click to toggle source
# File lib/brakeman/tracker/constants.rb, line 73
def find_constant exp
  base_name = Constants.get_constant_base_name(exp)

  if @constants.key? base_name
    @constants[base_name].find do |c|
      if c.match? exp
        return c
      end
    end

    name_array = Constants.constant_as_array(exp)

    # Avoid losing info about dynamic constant values
    return unless name_array.all? { |n| constant? n or n.is_a? Symbol }

    @constants[base_name].find do |c|
      if c.match? name_array
        return c
      end
    end
  end

  nil
end
get_simple_value(name) click to toggle source

Returns constant values that are not too complicated. Right now that means literal values (string, array, etc.) or calls on Dir.glob(..).whatever.

# File lib/brakeman/tracker/constants.rb, line 116
def get_simple_value name
  if x = self[name] and (literal? x or dir_glob? x)
    x
  else
    nil
  end
end
size() click to toggle source
# File lib/brakeman/tracker/constants.rb, line 58
def size
  @constants.length
end