module RuboCop::Cop::MinBranchesCount

Common functionality for checking minimum branches count.

Private Instance Methods

if_conditional_branches(node, branches = []) click to toggle source
# File lib/rubocop/cop/mixin/min_branches_count.rb, line 29
def if_conditional_branches(node, branches = [])
  return [] if node.nil? || !node.if_type?

  branches << node.if_branch

  else_branch = node.else_branch
  if_conditional_branches(else_branch, branches) if else_branch&.if_type?
  branches
end
min_branches_count() click to toggle source
# File lib/rubocop/cop/mixin/min_branches_count.rb, line 22
def min_branches_count
  length = cop_config['MinBranchesCount'] || 3
  return length if length.is_a?(Integer) && length.positive?

  raise 'MinBranchesCount needs to be a positive integer!'
end
min_branches_count?(node) click to toggle source
# File lib/rubocop/cop/mixin/min_branches_count.rb, line 9
def min_branches_count?(node)
  branches =
    if node.case_type?
      node.when_branches
    elsif node.if_type?
      if_conditional_branches(node)
    else
      raise ArgumentError, "Unsupported #{node.type.inspect} node type"
    end

  branches.size >= min_branches_count
end