123456789_123456789_123456789_123456789_123456789_

Module: RuboCop::Cop::MinBranchesCount

Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Included In:
Defined in: lib/rubocop/cop/mixin/min_branches_count.rb

Overview

Common functionality for checking minimum branches count.

Instance Method Summary

Instance Method Details

#if_conditional_branches(node, branches = []) (private)

[ GitHub ]

  
# 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 (private)

[ GitHub ]

  
# 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) ⇒ Boolean (private)

[ GitHub ]

  
# 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