Sha256: 50c9ec0474d117bfab207da6e4c826c87058db7264419e54e250c0965db64514

Contents?: true

Size: 1.49 KB

Versions: 17

Compression:

Stored size: 1.49 KB

Contents

# frozen_string_literal: true

require_relative 'base_detector'

module Reek
  module SmellDetectors
    #
    # A Large Class is a class or module that has a large number of
    # instance variables, methods, constants or lines of code.
    #
    # +TooManyConstants' reports classes having more than a
    # configurable number of constants.
    #
    # See {file:docs/Too-Many-Constants.md} for details.
    class TooManyConstants < BaseDetector
      # The name of the config field that sets the maximum number
      # of constants permitted in a class.
      MAX_ALLOWED_CONSTANTS_KEY = 'max_constants'
      DEFAULT_MAX_CONSTANTS = 5
      IGNORED_NODES = [:module, :class].freeze

      def self.contexts
        [:class, :module]
      end

      def self.default_config
        super.merge(
          MAX_ALLOWED_CONSTANTS_KEY => DEFAULT_MAX_CONSTANTS,
          EXCLUDE_KEY => [])
      end

      #
      # Checks +klass+ for too many constants.
      #
      # @return [Array<SmellWarning>]
      #
      def sniff
        count = context.local_nodes(:casgn).count { |it| !it.defines_module? }

        return [] if count <= max_allowed_constants

        build_smell_warning(count)
      end

      private

      def max_allowed_constants
        value(MAX_ALLOWED_CONSTANTS_KEY, context)
      end

      def build_smell_warning(count)
        [smell_warning(
          lines: [source_line],
          message: "has #{count} constants",
          parameters: { count: count })]
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
reek-6.3.0 lib/reek/smell_detectors/too_many_constants.rb
reek-6.2.0 lib/reek/smell_detectors/too_many_constants.rb
reek-6.1.4 lib/reek/smell_detectors/too_many_constants.rb
reek-6.1.3 lib/reek/smell_detectors/too_many_constants.rb
reek-6.1.2 lib/reek/smell_detectors/too_many_constants.rb
reek-6.1.1 lib/reek/smell_detectors/too_many_constants.rb
reek-6.1.0 lib/reek/smell_detectors/too_many_constants.rb
reek-6.0.6 lib/reek/smell_detectors/too_many_constants.rb
reek-6.0.5 lib/reek/smell_detectors/too_many_constants.rb
reek-6.0.4 lib/reek/smell_detectors/too_many_constants.rb
reek-6.0.3 lib/reek/smell_detectors/too_many_constants.rb
reek-6.0.2 lib/reek/smell_detectors/too_many_constants.rb
reek-6.0.1 lib/reek/smell_detectors/too_many_constants.rb
reek-6.0.0 lib/reek/smell_detectors/too_many_constants.rb
reek-5.6.0 lib/reek/smell_detectors/too_many_constants.rb
reek-5.5.0 lib/reek/smell_detectors/too_many_constants.rb
reek-5.4.1 lib/reek/smell_detectors/too_many_constants.rb