Sha256: 90a133bad4ea327136640cf39467a1881b288343426752be0e4afe61d543989f

Contents?: true

Size: 1.11 KB

Versions: 16

Compression:

Stored size: 1.11 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 %<method>s is too high. ' \
              '[%<complexity>.4g/%<max>.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

16 entries across 16 versions & 2 rubygems

Version Path
rubocop-0.59.2 lib/rubocop/cop/metrics/abc_size.rb
rubocop-0.59.1 lib/rubocop/cop/metrics/abc_size.rb
rubocop-0.59.0 lib/rubocop/cop/metrics/abc_size.rb
rubocop-0.58.2 lib/rubocop/cop/metrics/abc_size.rb
rubocop-0.58.1 lib/rubocop/cop/metrics/abc_size.rb
rubocop-0.58.0 lib/rubocop/cop/metrics/abc_size.rb
vagrant-packet-0.1.1 vendor/bundle/ruby/2.4.0/gems/rubocop-0.57.2/lib/rubocop/cop/metrics/abc_size.rb
rubocop-0.57.2 lib/rubocop/cop/metrics/abc_size.rb
rubocop-0.57.1 lib/rubocop/cop/metrics/abc_size.rb
rubocop-0.57.0 lib/rubocop/cop/metrics/abc_size.rb
rubocop-0.56.0 lib/rubocop/cop/metrics/abc_size.rb
rubocop-0.55.0 lib/rubocop/cop/metrics/abc_size.rb
rubocop-0.54.0 lib/rubocop/cop/metrics/abc_size.rb
rubocop-0.53.0 lib/rubocop/cop/metrics/abc_size.rb
rubocop-0.52.1 lib/rubocop/cop/metrics/abc_size.rb
rubocop-0.52.0 lib/rubocop/cop/metrics/abc_size.rb