Sha256: 244fb86b92a15b7c3f3788f509305967b7c488bc35d208b3ec67a230eaa8aa2d

Contents?: true

Size: 1.48 KB

Versions: 13

Compression:

Stored size: 1.48 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).reject(&:defines_module?).length

        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

13 entries across 11 versions & 2 rubygems

Version Path
reek-5.4.0 lib/reek/smell_detectors/too_many_constants.rb
reek-5.3.2 lib/reek/smell_detectors/too_many_constants.rb
reek-5.3.1 lib/reek/smell_detectors/too_many_constants.rb
reek-5.3.0 lib/reek/smell_detectors/too_many_constants.rb
config_gems_initialization_aim-0.1.4 vendor/bundle/ruby/2.5.0/gems/reek-5.2.0/lib/reek/smell_detectors/too_many_constants.rb
config_gems_initialization_aim-0.1.4 vendor/bundle/ruby/2.5.0/gems/config_gems_initialization_aim-0.1.1/vendor/bundle/ruby/2.5.0/gems/reek-5.2.0/lib/reek/smell_detectors/too_many_constants.rb
config_gems_initialization_aim-0.1.3 vendor/bundle/ruby/2.5.0/gems/config_gems_initialization_aim-0.1.1/vendor/bundle/ruby/2.5.0/gems/reek-5.2.0/lib/reek/smell_detectors/too_many_constants.rb
config_gems_initialization_aim-0.1.3 vendor/bundle/ruby/2.5.0/gems/reek-5.2.0/lib/reek/smell_detectors/too_many_constants.rb
reek-5.2.0 lib/reek/smell_detectors/too_many_constants.rb
reek-5.1.0 lib/reek/smell_detectors/too_many_constants.rb
reek-5.0.2 lib/reek/smell_detectors/too_many_constants.rb
reek-5.0.1 lib/reek/smell_detectors/too_many_constants.rb
reek-5.0.0 lib/reek/smell_detectors/too_many_constants.rb