Sha256: 84cc143676a100e2de4686a3e3d72fb1a688d506956efba54bcbcf4f3b4dde77

Contents?: true

Size: 1.08 KB

Versions: 5

Compression:

Stored size: 1.08 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Metrics
      # This cop checks that the ABC size of methods is not higher than the
      # configured maximum. The ABC size is based on assignments, branches
      # (method calls), and conditions. See http://c2.com/cgi/wiki?AbcMetric
      class AbcSize < Cop
        include MethodComplexity

        MSG = 'Assignment Branch Condition size for %s is too high. ' \
              '[%.4g/%.4g]'.freeze
        BRANCH_NODES = %i[send csend].freeze
        CONDITION_NODES = CyclomaticComplexity::COUNTED_NODES.freeze

        private

        def complexity(node)
          assignment = 0
          branch = 0
          condition = 0

          node.each_node do |child|
            if child.assignment?
              assignment += 1
            elsif BRANCH_NODES.include?(child.type)
              branch += 1
            elsif CONDITION_NODES.include?(child.type)
              condition += 1
            end
          end

          Math.sqrt(assignment**2 + branch**2 + condition**2).round(2)
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
rubocop-0.51.0 lib/rubocop/cop/metrics/abc_size.rb
rubocop-0.50.0 lib/rubocop/cop/metrics/abc_size.rb
rubocop-0.49.1 lib/rubocop/cop/metrics/abc_size.rb
rubocop-0.49.0 lib/rubocop/cop/metrics/abc_size.rb
rubocop-0.48.1 lib/rubocop/cop/metrics/abc_size.rb