Sha256: cc76c96c3f30859045d2a5f7b013a4965e55693b6ab7d26c0e73543f3fcb85b2

Contents?: true

Size: 1.09 KB

Versions: 14

Compression:

Stored size: 1.09 KB

Contents

# encoding: utf-8
# 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 = [:send].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

14 entries across 14 versions & 2 rubygems

Version Path
fluent-plugin-detect-memb-exceptions-0.0.2 vendor/bundle/ruby/2.0.0/gems/rubocop-0.42.0/lib/rubocop/cop/metrics/abc_size.rb
fluent-plugin-detect-memb-exceptions-0.0.1 vendor/bundle/ruby/2.0.0/gems/rubocop-0.42.0/lib/rubocop/cop/metrics/abc_size.rb
rubocop-0.43.0 lib/rubocop/cop/metrics/abc_size.rb
rubocop-0.42.0 lib/rubocop/cop/metrics/abc_size.rb
rubocop-0.41.2 lib/rubocop/cop/metrics/abc_size.rb
rubocop-0.41.1 lib/rubocop/cop/metrics/abc_size.rb
rubocop-0.41.0 lib/rubocop/cop/metrics/abc_size.rb
rubocop-0.40.0 lib/rubocop/cop/metrics/abc_size.rb
rubocop-0.39.0 lib/rubocop/cop/metrics/abc_size.rb
rubocop-0.38.0 lib/rubocop/cop/metrics/abc_size.rb
rubocop-0.37.2 lib/rubocop/cop/metrics/abc_size.rb
rubocop-0.37.1 lib/rubocop/cop/metrics/abc_size.rb
rubocop-0.37.0 lib/rubocop/cop/metrics/abc_size.rb
rubocop-0.36.0 lib/rubocop/cop/metrics/abc_size.rb