Sha256: ba9bbd0f07eef2226311a13196a1fd83ab9d423eea223cedded66f19e14e158e

Contents?: true

Size: 1.5 KB

Versions: 12

Compression:

Stored size: 1.5 KB

Contents

require 'reek/smells/smell_detector'
require 'reek/smell_warning'

module Reek
  module Smells
    #
    # A Large Class is a class or module that has a large number of
    # instance variables, methods or lines of code.
    #
    # +TooManyInstanceVariables' reports classes having more than a
    # configurable number of instance variables.
    #
    class TooManyInstanceVariables < SmellDetector
      # The name of the config field that sets the maximum number of instance
      # variables permitted in a class.
      MAX_ALLOWED_IVARS_KEY = 'max_instance_variables'
      DEFAULT_MAX_IVARS = 9

      def self.smell_category
        'LargeClass'
      end

      def self.contexts      # :nodoc:
        [:class]
      end

      def self.default_config
        super.merge(
          MAX_ALLOWED_IVARS_KEY => DEFAULT_MAX_IVARS,
          EXCLUDE_KEY => []
        )
      end

      #
      # Checks +klass+ for too many instance variables.
      #
      # @return [Array<SmellWarning>]
      #
      def examine_context(ctx)
        @max_allowed_ivars = value(MAX_ALLOWED_IVARS_KEY, ctx, DEFAULT_MAX_IVARS)
        count = ctx.local_nodes(:ivasgn).map { |ivasgn| ivasgn[1] }.uniq.length
        return [] if count <= @max_allowed_ivars
        [SmellWarning.new(self,
                          context: ctx.full_name,
                          lines: [ctx.exp.line],
                          message: "has at least #{count} instance variables",
                          parameters: { count: count })]
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
reek-2.0.4 lib/reek/smells/too_many_instance_variables.rb
reek-2.0.3 lib/reek/smells/too_many_instance_variables.rb
reek-2.0.2 lib/reek/smells/too_many_instance_variables.rb
reek-2.0.1 lib/reek/smells/too_many_instance_variables.rb
reek-2.0.0 lib/reek/smells/too_many_instance_variables.rb
reek-1.6.6 lib/reek/smells/too_many_instance_variables.rb
reek-1.6.5 lib/reek/smells/too_many_instance_variables.rb
reek-1.6.4 lib/reek/smells/too_many_instance_variables.rb
reek-1.6.3 lib/reek/smells/too_many_instance_variables.rb
reek-1.6.2 lib/reek/smells/too_many_instance_variables.rb
reek-1.6.1 lib/reek/smells/too_many_instance_variables.rb
reek-1.6.0 lib/reek/smells/too_many_instance_variables.rb