Sha256: 2551ca3ff0e9f56ef1660976f1d1dca37643fcd855bf60939a4beb9f405d6921

Contents?: true

Size: 1.85 KB

Versions: 15

Compression:

Stored size: 1.85 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    # @api private
    #
    # This module handles measurement and reporting of complexity in methods.
    module MethodComplexity
      include AllowedMethods
      include AllowedPattern
      include Metrics::Utils::RepeatedCsendDiscount
      extend NodePattern::Macros
      extend ExcludeLimit

      exclude_limit 'Max'

      def on_def(node)
        return if allowed_method?(node.method_name) || matches_allowed_pattern?(node.method_name)

        check_complexity(node, node.method_name)
      end
      alias on_defs on_def

      def on_block(node)
        define_method?(node) do |name|
          return if allowed_method?(name) || matches_allowed_pattern?(name)

          check_complexity(node, name)
        end
      end

      alias on_numblock on_block

      private

      # @!method define_method?(node)
      def_node_matcher :define_method?, <<~PATTERN
        ({block numblock}
         (send nil? :define_method ({sym str} $_)) _ _)
      PATTERN

      def check_complexity(node, method_name)
        # Accepts empty methods always.
        return unless node.body

        max = cop_config['Max']
        reset_repeated_csend
        complexity, abc_vector = complexity(node.body)

        return unless complexity > max

        msg = format(self.class::MSG,
                     method: method_name,
                     complexity: complexity,
                     abc_vector: abc_vector,
                     max: max)

        add_offense(node, message: msg) { self.max = complexity.ceil }
      end

      def complexity(body)
        body.each_node(:lvasgn, *self.class::COUNTED_NODES).reduce(1) do |score, node|
          if node.lvasgn_type?
            reset_on_lvasgn(node)
            next score
          end
          score + complexity_score_for(node)
        end
      end
    end
  end
end

Version data entries

15 entries across 13 versions & 3 rubygems

Version Path
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/mixin/method_complexity.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/mixin/method_complexity.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/mixin/method_complexity.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.35.1/lib/rubocop/cop/mixin/method_complexity.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.36.0/lib/rubocop/cop/mixin/method_complexity.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.35.1/lib/rubocop/cop/mixin/method_complexity.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.36.0/lib/rubocop/cop/mixin/method_complexity.rb
rubocop-1.40.0 lib/rubocop/cop/mixin/method_complexity.rb
rubocop-1.39.0 lib/rubocop/cop/mixin/method_complexity.rb
rubocop-1.38.0 lib/rubocop/cop/mixin/method_complexity.rb
rubocop-1.37.1 lib/rubocop/cop/mixin/method_complexity.rb
rubocop-1.37.0 lib/rubocop/cop/mixin/method_complexity.rb
rubocop-1.36.0 lib/rubocop/cop/mixin/method_complexity.rb
rubocop-1.35.1 lib/rubocop/cop/mixin/method_complexity.rb
rubocop-1.35.0 lib/rubocop/cop/mixin/method_complexity.rb