Sha256: 03cebbe1604cf6dfc231e6263ce567b44266261050e601460db928038d431423

Contents?: true

Size: 1.02 KB

Versions: 160

Compression:

Stored size: 1.02 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    # Common functionality for checking minimum branches count.
    module MinBranchesCount
      private

      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

      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

      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
    end
  end
end

Version data entries

160 entries across 159 versions & 17 rubygems

Version Path
rubocop-1.63.4 lib/rubocop/cop/mixin/min_branches_count.rb
rubocop-1.63.3 lib/rubocop/cop/mixin/min_branches_count.rb
rubocop-1.63.2 lib/rubocop/cop/mixin/min_branches_count.rb
harbr-2.8.1 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/mixin/min_branches_count.rb
rubocop-1.63.1 lib/rubocop/cop/mixin/min_branches_count.rb
rubocop-1.63.0 lib/rubocop/cop/mixin/min_branches_count.rb
bison-0.1.0 vendor/bundle/ruby/3.2.0/gems/rubocop-1.62.1/lib/rubocop/cop/mixin/min_branches_count.rb
rubocop-1.62.1 lib/rubocop/cop/mixin/min_branches_count.rb
rubocop-1.62.0 lib/rubocop/cop/mixin/min_branches_count.rb
rubocop-1.61.0 lib/rubocop/cop/mixin/min_branches_count.rb
mlh-rubocop-config-1.0.3 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.2/lib/rubocop/cop/mixin/min_branches_count.rb
rubocop-1.60.2 lib/rubocop/cop/mixin/min_branches_count.rb
study_line-0.2.7 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.0/lib/rubocop/cop/mixin/min_branches_count.rb
study_line-0.2.6 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.0/lib/rubocop/cop/mixin/min_branches_count.rb
study_line-0.2.5 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.0/lib/rubocop/cop/mixin/min_branches_count.rb
study_line-0.2.4 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.0/lib/rubocop/cop/mixin/min_branches_count.rb
study_line-0.2.3 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.0/lib/rubocop/cop/mixin/min_branches_count.rb
study_line-0.2.2 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.0/lib/rubocop/cop/mixin/min_branches_count.rb
rubocop-1.60.1 lib/rubocop/cop/mixin/min_branches_count.rb
study_line-0.2.1 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.0/lib/rubocop/cop/mixin/min_branches_count.rb