class RuboCop::Cop::Style::FetchEnvVar


.some_method # (e.g. ‘.nil?`)
!ENV[’X’]
# also good
x = ENV.fetch(‘X’)
ENV.fetch(‘X’)
# good
x = ENV[‘X’]
ENV[‘X’]
# bad
@example
specified default value.
On the other hand, ‘ENV.fetch` raises KeyError or returns the explicitly
which may cause unexpected behaviors when the developer forgets to set it.
`ENV[]` silently fails and returns `nil` when the environment variable is unset,
Suggests `ENV.fetch` for the replacement of `ENV[]`.

def allowable_use?(node)

- `ENV['key']` is the LHS of a `||`.
- `ENV['key']` assigned by logical AND/OR assignment.
- Receiving a message with dot syntax, e.g. `ENV['X'].nil?`.
it simply checks whether the variable is set.
- Used as a flag (e.g., `if ENV['X']` or `!ENV['X']`) because

The following are allowed cases:
def allowable_use?(node)
  used_as_flag?(node) || message_chained_with_dot?(node) || assigned?(node) || or_lhs?(node)
end

def allowed_var?(node)

def allowed_var?(node)
  env_key_node = node.children.last
  env_key_node.str_type? && cop_config['AllowedVars'].include?(env_key_node.value)
end

def assigned?(node)

- `ENV['key']` is a receiver of `&&=`, e.g. `ENV['X'] &&= y`.
- `ENV['key']` is a receiver of `||=`, e.g. `ENV['X'] ||= y`.

The following are allowed cases:
def assigned?(node)
  return false unless (parent = node.parent)&.assignment?
  lhs, _method, _rhs = *parent
  node == lhs
end

def message_chained_with_dot?(node)

Check if the node is a receiver and receives a message with dot syntax.
def message_chained_with_dot?(node)
  return false if node.root?
  parent = node.parent
  return false if !parent.call_type? || parent.children.first != node
  parent.dot? || parent.safe_navigation?
end

def new_code(name_node)

def new_code(name_node)
  "ENV.fetch(#{name_node.source}, nil)"
end

def offensive?(node)

def offensive?(node)
  !(allowed_var?(node) || allowable_use?(node))
end

def on_send(node)

def on_send(node)
  env_with_bracket?(node) do |name_node|
    break unless offensive?(node)
    message = format(MSG, key: name_node.source)
    add_offense(node, message: message) do |corrector|
      corrector.replace(node, new_code(name_node))
    end
  end
end

def or_lhs?(node)

def or_lhs?(node)
  return false unless (parent = node.parent)&.or_type?
  parent.lhs == node || parent.parent&.or_type?
end

def partial_matched?(node, condition)

`ENV['key'] if ENV['key'] = x`
Avoid offending in the following cases:
def partial_matched?(node, condition)
  node.child_nodes == node.child_nodes & condition.child_nodes
end

def used_as_flag?(node)

def used_as_flag?(node)
  return false if node.root?
  return true if used_if_condition_in_body(node)
  node.parent.send_type? && (node.parent.prefix_bang? || node.parent.comparison_method?)
end

def used_if_condition_in_body(node)

def used_if_condition_in_body(node)
  if_node = node.ancestors.find(&:if_type?)
  return false unless (condition = if_node&.condition)
  return true if condition.send_type? && (condition.child_nodes == node.child_nodes)
  used_in_condition?(node, condition)
end

def used_in_condition?(node, condition)

def used_in_condition?(node, condition)
  if condition.send_type?
    return true if condition.assignment_method? && partial_matched?(node, condition)
    return false if !condition.comparison_method? && !condition.predicate_method?
  end
  condition.child_nodes.any?(node)
end